35
35
#include <unistd.h>
36
36
#include <string.h>
37
37
#include <stdlib.h>
38
+ #include <stdbool.h>
38
39
39
40
#define same (x , y ) strcmp((const char*) x, (const char*) y) == 0
40
41
@@ -628,7 +629,8 @@ void t_randomstr ( uint8_t *str, size_t size )
628
629
629
630
630
631
typedef enum {
631
- error_deadcall = 1 , /* has call id but it's from old call */
632
+ error_none ,
633
+ error_deadcall , /* has call id but it's from old call */
632
634
error_id_mismatch , /* non-existing call */
633
635
634
636
error_no_callid , /* not having call id */
@@ -820,9 +822,9 @@ MSICall *find_call ( MSISession *session, uint8_t *call_id )
820
822
* @param errid The id.
821
823
* @param to Where to?
822
824
* @return int
823
- * @retval 0 It's always success.
825
+ * @retval -1/ 0 It's usually always success.
824
826
*/
825
- int handle_error ( MSISession * session , MSICall * call , MSICallError errid , uint32_t to )
827
+ int send_error ( MSISession * session , MSICall * call , MSICallError errid , uint32_t to )
826
828
{
827
829
if (!call ) {
828
830
LOGGER_WARNING ("Cannot handle error on 'null' call" );
@@ -842,38 +844,12 @@ int handle_error ( MSISession *session, MSICall *call, MSICallError errid, uint3
842
844
session -> last_error_id = errid ;
843
845
session -> last_error_str = stringify_error ( errid );
844
846
845
- invoke_callback (call -> call_idx , MSI_OnError );
847
+ /* invoke_callback(call->call_idx, MSI_OnError); */
846
848
847
849
return 0 ;
848
850
}
849
851
850
852
851
- /**
852
- * @brief Determine the error if any.
853
- *
854
- * @param session Control session.
855
- * @param msg The message.
856
- * @return int
857
- * @retval -1 No error.
858
- * @retval 0 Error occurred and response sent.
859
- */
860
- int has_call_error ( MSISession * session , MSICall * call , MSIMessage * msg )
861
- {
862
- if ( !msg -> callid .header_value ) {
863
- return handle_error ( session , call , error_no_callid , msg -> friend_id );
864
-
865
- } else if ( !call ) {
866
- LOGGER_WARNING ("Handling message while no call!" );
867
- return 0 ;
868
-
869
- } else if ( memcmp ( call -> id , msg -> callid .header_value , CALL_ID_LEN ) != 0 ) {
870
- return handle_error ( session , call , error_id_mismatch , msg -> friend_id );
871
-
872
- }
873
-
874
- return -1 ;
875
- }
876
-
877
853
878
854
/**
879
855
* @brief Add peer to peer list.
@@ -1083,7 +1059,8 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1083
1059
}
1084
1060
1085
1061
} else {
1086
- handle_error ( session , call , error_busy , msg -> friend_id ); /* TODO: Ugh*/
1062
+ send_error ( session , call , error_busy , msg -> friend_id ); /* TODO: Ugh*/
1063
+ terminate_call (session , call );
1087
1064
pthread_mutex_unlock (& session -> mutex );
1088
1065
return 0 ;
1089
1066
}
@@ -1098,7 +1075,8 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1098
1075
}
1099
1076
1100
1077
if ( !msg -> callid .header_value ) {
1101
- handle_error ( session , call , error_no_callid , msg -> friend_id );
1078
+ send_error ( session , call , error_no_callid , msg -> friend_id );
1079
+ terminate_call (session , call );
1102
1080
pthread_mutex_unlock (& session -> mutex );
1103
1081
return 0 ;
1104
1082
}
@@ -1122,17 +1100,18 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1122
1100
}
1123
1101
int handle_recv_start ( MSISession * session , MSICall * call , MSIMessage * msg )
1124
1102
{
1103
+ if ( !call ) {
1104
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1105
+ return 0 ;
1106
+ }
1107
+
1125
1108
LOGGER_DEBUG ("Session: %p Handling 'start' on call: %s, friend id: %d" , session , call -> id , msg -> friend_id );
1126
1109
1127
1110
pthread_mutex_lock (& session -> mutex );
1128
1111
1129
- if ( has_call_error ( session , call , msg ) == 0 ) {
1130
- pthread_mutex_unlock (& session -> mutex );
1131
- return -1 ;
1132
- }
1133
-
1134
1112
if ( !msg -> cryptokey .header_value ) {
1135
- int rc = handle_error ( session , call , error_no_crypto_key , msg -> friend_id );
1113
+ int rc = send_error ( session , call , error_no_crypto_key , msg -> friend_id );
1114
+ terminate_call (session , call );
1136
1115
pthread_mutex_unlock (& session -> mutex );
1137
1116
return rc ;
1138
1117
}
@@ -1154,15 +1133,14 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
1154
1133
}
1155
1134
int handle_recv_reject ( MSISession * session , MSICall * call , MSIMessage * msg )
1156
1135
{
1157
- LOGGER_DEBUG ("Session: %p Handling 'reject' on call: %s" , session , call -> id );
1158
-
1159
- pthread_mutex_lock (& session -> mutex );
1160
-
1161
- if ( has_call_error ( session , call , msg ) == 0 ) {
1162
- pthread_mutex_unlock (& session -> mutex );
1136
+ if ( !call ) {
1137
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1163
1138
return 0 ;
1164
1139
}
1165
1140
1141
+ LOGGER_DEBUG ("Session: %p Handling 'reject' on call: %s" , session , call -> id );
1142
+
1143
+ pthread_mutex_lock (& session -> mutex );
1166
1144
1167
1145
MSIMessage * _msg_ending = msi_new_message ( TYPE_RESPONSE , stringify_response ( ending ) );
1168
1146
send_message ( session , call , _msg_ending , msg -> friend_id );
@@ -1182,20 +1160,16 @@ int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
1182
1160
}
1183
1161
int handle_recv_cancel ( MSISession * session , MSICall * call , MSIMessage * msg )
1184
1162
{
1163
+ if ( !call ) {
1164
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1165
+ return 0 ;
1166
+ }
1167
+
1185
1168
LOGGER_DEBUG ("Session: %p Handling 'cancel' on call: %s" , session , call -> id );
1186
1169
1187
1170
pthread_mutex_lock (& session -> mutex );
1188
1171
1189
- if ( has_call_error ( session , call , msg ) == 0 ) {
1190
- pthread_mutex_unlock (& session -> mutex );
1191
- return 0 ;
1192
- }
1193
-
1194
1172
/* Act as end message */
1195
- /*
1196
- MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
1197
- send_message ( session, call, _msg_ending, msg->friend_id );
1198
- free_message ( _msg_ending );*/
1199
1173
1200
1174
pthread_mutex_unlock (& session -> mutex );
1201
1175
invoke_callback (call -> call_idx , MSI_OnCancel );
@@ -1205,15 +1179,15 @@ int handle_recv_cancel ( MSISession *session, MSICall *call, MSIMessage *msg )
1205
1179
}
1206
1180
int handle_recv_end ( MSISession * session , MSICall * call , MSIMessage * msg )
1207
1181
{
1182
+ if ( !call ) {
1183
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1184
+ return 0 ;
1185
+ }
1186
+
1208
1187
LOGGER_DEBUG ("Session: %p Handling 'end' on call: %s" , session , call -> id );
1209
1188
1210
1189
pthread_mutex_lock (& session -> mutex );
1211
1190
1212
- if ( has_call_error ( session , call , msg ) == 0 ) {
1213
- pthread_mutex_unlock (& session -> mutex );
1214
- return 0 ;
1215
- }
1216
-
1217
1191
MSIMessage * _msg_ending = msi_new_message ( TYPE_RESPONSE , stringify_response ( ending ) );
1218
1192
send_message ( session , call , _msg_ending , msg -> friend_id );
1219
1193
free_message ( _msg_ending );
@@ -1229,13 +1203,13 @@ int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
1229
1203
/********** Response handlers **********/
1230
1204
int handle_recv_ringing ( MSISession * session , MSICall * call , MSIMessage * msg )
1231
1205
{
1232
- pthread_mutex_lock (& session -> mutex );
1233
-
1234
- if ( has_call_error ( session , call , msg ) == 0 ) {
1235
- pthread_mutex_unlock (& session -> mutex );
1206
+ if ( !call ) {
1207
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1236
1208
return 0 ;
1237
1209
}
1238
1210
1211
+ pthread_mutex_lock (& session -> mutex );
1212
+
1239
1213
LOGGER_DEBUG ("Session: %p Handling 'ringing' on call: %s" , session , call -> id );
1240
1214
1241
1215
call -> ringing_timer_id = event .timer_alloc ( handle_timeout , call , call -> ringing_tout_ms );
@@ -1247,18 +1221,19 @@ int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
1247
1221
}
1248
1222
int handle_recv_starting ( MSISession * session , MSICall * call , MSIMessage * msg )
1249
1223
{
1250
- pthread_mutex_lock (& session -> mutex );
1251
-
1252
- if ( has_call_error ( session , call , msg ) == 0 ) {
1253
- pthread_mutex_unlock (& session -> mutex );
1224
+ if ( !call ) {
1225
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1254
1226
return 0 ;
1255
1227
}
1256
1228
1229
+ pthread_mutex_lock (& session -> mutex );
1230
+
1257
1231
LOGGER_DEBUG ("Session: %p Handling 'starting' on call: %s" , session , call -> id );
1258
1232
1259
1233
1260
1234
if ( !msg -> cryptokey .header_value ) {
1261
- int rc = handle_error ( session , call , error_no_crypto_key , msg -> friend_id );
1235
+ int rc = send_error ( session , call , error_no_crypto_key , msg -> friend_id );
1236
+ terminate_call (session , call );
1262
1237
pthread_mutex_unlock (& session -> mutex );
1263
1238
return rc ;
1264
1239
}
@@ -1296,13 +1271,13 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
1296
1271
}
1297
1272
int handle_recv_ending ( MSISession * session , MSICall * call , MSIMessage * msg )
1298
1273
{
1299
- pthread_mutex_lock (& session -> mutex );
1300
-
1301
- if ( has_call_error ( session , call , msg ) == 0 ) {
1302
- pthread_mutex_unlock (& session -> mutex );
1274
+ if ( !call ) {
1275
+ LOGGER_WARNING ("Session: %p Handling 'start' on no call" );
1303
1276
return 0 ;
1304
1277
}
1305
1278
1279
+ pthread_mutex_lock (& session -> mutex );
1280
+
1306
1281
LOGGER_DEBUG ("Session: %p Handling 'ending' on call: %s" , session , call -> id );
1307
1282
1308
1283
/* Stop timer */
0 commit comments