📄 wtls.c
字号:
return "unknown state"; }}/* * Feed an event to a WTP responder state machine. Handle all errors yourself, * do not report them to the caller. Note: Do not put {}s of the else block * inside the macro definition. */static void wtls_event_handle(WTLSMachine *wtls_machine, WAPEvent *event){ debug("wap.wtls", 0, "WTLS: wtls_machine %ld, state %s, event %s.", wtls_machine->mid, name_wtls_state(wtls_machine->state), wap_event_name(event->type)); /* for T_Unitdata_Ind PDUs */ if(event->type == T_Unitdata_Ind) { /* if encryption: decrypt all pdus in the list */ if( wtls_machine->encrypted ) { wtls_decrypt_pdu_list(wtls_machine, event->u.T_Unitdata_Ind.pdu_list); } /* add all handshake data to wtls_machine->handshake_data */ //add_all_handshake_data(wtls_machine, event->u.T_Unitdata_Ind.pdu_list); } #define STATE_NAME(state) #define ROW(wtls_state, event_type, condition, action, next_state) \ if (wtls_machine->state == wtls_state && \ event->type == event_type && \ (condition)) { \ action \ wtls_machine->state = next_state; \ debug("wap.wtls", 0, "WTLS %ld: New state %s", wtls_machine->mid, #next_state); \ } else #include "wtls_state-decl.h" { error(0, "WTLS: handle_event: unhandled event!"); debug("wap.wtls", 0, "WTLS: handle_event: Unhandled event was:"); wap_event_destroy(event); return; } if (event != NULL) { wap_event_destroy(event); } if (wtls_machine->state == NULL_STATE) wtls_machine_destroy(wtls_machine);}/* * Checks whether wtls machines data structure includes a specific machine. * The machine in question is identified with with source and destination * address and port. */static WTLSMachine *wtls_machine_find_or_create(WAPEvent *event) { WTLSMachine *wtls_machine = NULL; long mid; WAPAddrTuple *tuple; tuple = NULL; mid = -1; debug("wap.wtls",0, "event->type = %d", event->type); /* Get the address that this PDU came in from */ switch (event->type) { case T_Unitdata_Ind: case T_DUnitdata_Ind: tuple = event->u.T_Unitdata_Ind.addr_tuple; break; case SEC_Create_Request_Req: case SEC_Terminate_Req: case SEC_Exception_Req: case SEC_Create_Res: case SEC_Exchange_Req: case SEC_Commit_Req: case SEC_Unitdata_Req: tuple = event->u.T_Unitdata_Ind.addr_tuple; break; default: debug("wap.wtls", 0, "WTLS: wtls_machine_find_or_create:" "unhandled event (1)"); wap_event_dump(event); return NULL; } /* Either the address or the machine id must be available at this point */ gw_assert(tuple != NULL || mid != -1); /* Look for the machine owning this address */ wtls_machine = wtls_machine_find(tuple, mid); /* Oh well, we didn't find one. We'll create one instead, provided it meets certain criteria */ if (wtls_machine == NULL){ switch (event->type){ case SEC_Create_Request_Req: /* State NULL, case 1 */ debug("wap.wtls",0,"WTLS: received a SEC_Create_Request_Req, and don't know what to do with it..."); /* Create and dispatch a T_Unitdata_Req containing a HelloRequest */ /* And there's no need to do anything else, 'cause we return to state NULL */ break; case T_Unitdata_Ind: case T_DUnitdata_Ind: /* State NULL, case 3 *//* if (wtls_event_type(event) == Alert_No_Renegotiation) { */ /* Create and dispatch a SEC_Exception_Ind event *//* debug("wap.wtls",0,"WTLS: received an Alert_no_Renegotiation; just dropped it."); */ /* And there's no need to do anything else, 'cause we return to state NULL *//* break; *//* } else *//* if (event->u.T_Unitdata_Ind == ClientHello) { */ /* State NULL, case 2 */ wtls_machine = wtls_machine_create(tuple); /* And stick said event into machine, which should push us into state CREATING after a SEC_Create_Ind *//* } */ break; default: error(0, "WTLS: wtls_machine_find_or_create:" " unhandled event (2)"); wap_event_dump(event); break; } } return wtls_machine;}static int is_wanted_wtls_machine(void *a, void *b) { machine_pattern *pat; WTLSMachine *m; m = a; pat = b; if (m->mid == pat->mid) return 1; if (pat->mid != -1) return 0; return wap_addr_tuple_same(m->addr_tuple, pat->tuple);}static WTLSMachine *wtls_machine_find(WAPAddrTuple *tuple, long mid) { machine_pattern pat; WTLSMachine *m; pat.tuple = tuple; pat.mid = mid; m = list_search(wtls_machines, &pat, is_wanted_wtls_machine); return m;}static WTLSMachine *wtls_machine_create(WAPAddrTuple *tuple) { WTLSMachine *wtls_machine; wtls_machine = gw_malloc(sizeof(WTLSMachine)); #define MACHINE(field) field #define ENUM(name) wtls_machine->name = NULL_STATE; #define ADDRTUPLE(name) wtls_machine->name = NULL; #define INTEGER(name) wtls_machine->name = 0; #define OCTSTR(name) wtls_machine->name = NULL; #define PDULIST(name) wtls_machine->name = NULL; #include "wtls_machine-decl.h" list_append(wtls_machines, wtls_machine); wtls_machine->mid = counter_increase(wtls_machine_id_counter); wtls_machine->addr_tuple = wap_addr_tuple_duplicate(tuple); wtls_machine->handshake_data = octstr_create(""); debug("wap.wtls", 0, "WTLS: Created WTLSMachine %p (%ld)", (void *) wtls_machine, wtls_machine->mid); return wtls_machine;}/* * Destroys a WTLSMachine. Assumes it is safe to do so. Assumes it has * already been deleted from the machines list. */static void wtls_machine_destroy(void * p) { WTLSMachine *wtls_machine; wtls_machine = p; debug("wap.wtls", 0, "WTLS: Destroying WTLSMachine %p (%ld)", (void *) wtls_machine, wtls_machine->mid); list_delete_equal(wtls_machines, wtls_machine); #define MACHINE(field) field #define ENUM(name) wtls_machine->name = NULL_STATE; #define ADDRTUPLE(name) wap_addr_tuple_destroy(wtls_machine->name); #define INTEGER(name) wtls_machine->name = 0; #define OCTSTR(name) octstr_destroy(wtls_machine->name); #define PDULIST(name) wtls_machine->name = NULL; #include "wtls_machine-decl.h" gw_free(wtls_machine);}/* * Create a TR-Invoke.ind event.static WAPEvent *create_tr_invoke_ind(WTPRespMachine *sm, Octstr *user_data) { WAPEvent *event; event = wap_event_create(TR_Invoke_Ind); event->u.TR_Invoke_Ind.ack_type = sm->u_ack; event->u.TR_Invoke_Ind.user_data = octstr_duplicate(user_data); event->u.TR_Invoke_Ind.tcl = sm->tcl; event->u.TR_Invoke_Ind.addr_tuple = wap_addr_tuple_duplicate(sm->addr_tuple); event->u.TR_Invoke_Ind.handle = sm->mid; return event;}*//* * Create a TR-Result.cnf event.static WAPEvent *create_tr_result_cnf(WTPRespMachine *sm) { WAPEvent *event; event = wap_event_create(TR_Result_Cnf); event->u.TR_Result_Cnf.addr_tuple = wap_addr_tuple_duplicate(sm->addr_tuple); event->u.TR_Result_Cnf.handle = sm->mid; return event;}*//* * Creates TR-Abort.ind event from a responder state machine. static WAPEvent *create_tr_abort_ind(WTPRespMachine *sm, long abort_reason) { WAPEvent *event; event = wap_event_create(TR_Abort_Ind); event->u.TR_Abort_Ind.abort_code = abort_reason; event->u.TR_Abort_Ind.addr_tuple = wap_addr_tuple_duplicate(sm->addr_tuple); event->u.TR_Abort_Ind.handle = sm->mid; return event;}*/static int wtls_machine_has_mid(void *a, void *b) { WTLSMachine *sm; long mid; sm = a; mid = *(long *) b; return sm->mid == mid;}static WTLSMachine *find_wtls_machine_using_mid(long mid) { return list_search(wtls_machines, &mid, wtls_machine_has_mid);}/* Used for list searches */static int match_handshake_type(void* item, void* pattern){ wtls_Payload* matchingPayload; int type; int retrievedType; matchingPayload = (wtls_Payload*) item; type = (int) pattern; retrievedType = octstr_get_char(matchingPayload->data, 0); if (matchingPayload->type == Handshake_PDU && retrievedType == type) { return 1; } else { return 0; } }static int match_pdu_type(void* item, void* pattern){ wtls_Payload* matchingPayload; int type; matchingPayload = (wtls_Payload*) item; type = (int) pattern; if (matchingPayload->type == type) { return 1; } else { return 0; } }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -