2 * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
3 * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
4 * Contact: http://philzimmermann.com
5 * For licensing and other legal details, see the file zrtp_legal.c.
7 * Viktor Krykun <v.krikun at zfoneproject.com>
12 #define _ZTU_ "zrtp dengine"
15 #if (defined(ZRTP_BUILD_FOR_CSD) && (ZRTP_BUILD_FOR_CSD == 1))
17 extern zrtp_status_t
_zrtp_machine_process_hello(zrtp_stream_t
* stream
, zrtp_rtp_info_t
* packet
);
18 extern zrtp_status_t
start_send_and_resend_hello(zrtp_stream_t
* stream
);
19 extern zrtp_status_t
start_initiating_secure(zrtp_stream_t
*stream
);
20 extern zrtp_status_t
_zrtp_machine_start_send_and_resend_hello(zrtp_stream_t
* stream
);
23 /*----------------------------------------------------------------------------*/
24 void zrtp_driven_stream_start(zrtp_stream_t
* stream
, zrtp_statemachine_type_t role
)
27 ZRTP_LOG(3,(_ZTU_
,"START Driven %s Stream ID=%u mode=%s state=%s.",
28 (ZRTP_STATEMACHINE_INITIATOR
== role
)?"INITIATOR":"RESPONDER",
29 stream
->id
, zrtp_log_mode2str(stream
->mode
), zrtp_log_state2str(stream
->state
)));
31 /* This function can be called in parallel to the main processing loop protect internal stream data. */
32 zrtp_mutex_lock(stream
->stream_protector
);
34 if ( (ZRTP_STATE_ACTIVE
!= stream
->state
) &&
35 (ZRTP_STATE_ERROR
!= stream
->state
) &&
36 (ZRTP_STATE_NO_ZRTP
!= stream
->state
))
38 ZRTP_LOG(1,(_ZTU_
,"ERROR! can't start stream ID=%u from state %d.", stream
->id
, stream
->state
));
42 if (ZRTP_STATEMACHINE_INITIATOR
== role
) {
43 _zrtp_change_state(stream
, ZRTP_STATE_DRIVEN_INITIATOR
);
44 _zrtp_machine_start_send_and_resend_hello(stream
);
45 } else if (ZRTP_STATEMACHINE_RESPONDER
== role
) {
46 _zrtp_change_state(stream
, ZRTP_STATE_DRIVEN_RESPONDER
);
50 zrtp_mutex_unlock(stream
->stream_protector
);
53 /*---------------------------------------------------------------------------*/
54 zrtp_status_t
_zrtp_machine_process_while_in_driven_initiator( zrtp_stream_t
* stream
,
55 zrtp_rtp_info_t
* packet
)
57 zrtp_status_t s
= zrtp_status_ok
;
62 s
= _zrtp_machine_process_hello(stream
, packet
);
63 if (zrtp_status_ok
!= s
) {
64 ZRTP_LOG(1,(_ZTU_
,"ERROR! _zrtp_machine_process_hello()4 failed with status=%d. ID=%u",s
, stream
->id
));
65 break; /* Just stay in DRIVEN_INITIATOR state. */
68 /* Now we have ZIDs for both sides and can upload secrets from the cache */
69 s
= _zrtp_prepare_secrets(stream
->session
);
70 if (zrtp_status_ok
!= s
) {
71 ZRTP_LOG(1,(_ZTU_
,"ERROR! _zrtp_prepare_secrets()3 failed with status=%d. ID=%u",s
, stream
->id
));
72 break; /* Just stay in START state. */
75 // TODO: handle autosecure and licensing modes there
76 _zrtp_cancel_send_packet_later(stream
, ZRTP_HELLO
);
77 stream
->mode
= _zrtp_define_stream_mode(stream
);
78 s
= _zrtp_machine_enter_initiatingsecure(stream
);
88 zrtp_status_t
_zrtp_machine_process_while_in_driven_responder( zrtp_stream_t
* stream
,
89 zrtp_rtp_info_t
* packet
)
91 zrtp_status_t s
= zrtp_status_ok
;
96 s
= _zrtp_machine_process_hello(stream
, packet
);
97 if (zrtp_status_ok
!= s
) {
98 ZRTP_LOG(1,(_ZTU_
,"ERROR! _zrtp_machine_process_hello()5 failed with status=%d. ID=%u", s
, stream
->id
));
99 break; /* Just stay in DRIVEN_INITIATOR state. */
102 /* Now we have ZIDs for both sides and can upload secrets from the cache */
103 s
= _zrtp_prepare_secrets(stream
->session
);
104 if (zrtp_status_ok
!= s
) {
105 ZRTP_LOG(1,(_ZTU_
,"ERROR! _zrtp_prepare_secrets()4 failed with status=%d. ID=%u", s
, stream
->id
));
106 break; /* Just stay in START state. */
109 // TODO: handle autosecure and licensing modes there
110 s
= _zrtp_packet_send_message(stream
, ZRTP_HELLO
, &stream
->messages
.hello
);
111 if (zrtp_status_ok
== s
) {
112 _zrtp_change_state(stream
, ZRTP_STATE_DRIVEN_PENDING
);
123 zrtp_status_t
_zrtp_machine_process_while_in_driven_pending( zrtp_stream_t
* stream
,
124 zrtp_rtp_info_t
* packet
)
126 zrtp_status_t s
= zrtp_status_ok
;
128 switch (packet
->type
)
131 s
= _zrtp_packet_send_message(stream
, ZRTP_HELLO
, &stream
->messages
.hello
);
135 zrtp_statemachine_type_t role
= _zrtp_machine_preparse_commit(stream
, packet
);
136 if (ZRTP_STATEMACHINE_RESPONDER
== role
) {
137 s
= _zrtp_machine_enter_pendingsecure(stream
, packet
);
138 } else if (ZRTP_STATEMACHINE_INITIATOR
== role
) {
139 s
= _zrtp_machine_start_initiating_secure(stream
);
141 s
= zrtp_status_fail
;
152 #endif /* ZRTP_BUILD_FOR_CSD */