/** * @fn tnet_startup * This is probably the most important function. You MUST call this function to initialize the network stack before calling any <b>tnet_*</b> function. * You MUST call @ref tnet_cleanup to cleanup the network stack. * * @sa @ref tnet_cleanup. * @return Zero if succeed and error code otherwise. **/ int tnet_startup() { int err = 0; short word = 0x4321; if(__tnet_started){ goto bail; } // rand() srand((unsigned int) tsk_time_epoch()); // endianness tnet_isBigEndian = ((*(int8_t *)&word) != 0x21); #if TNET_UNDER_WINDOWS if(tnet_isBigEndian){ TSK_DEBUG_ERROR("Big endian on Windows machine. Is it right?"); } #endif #if TNET_UNDER_WINDOWS { WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { TSK_DEBUG_FATAL("WSAStartup failed with error: %d\n", err); return -1; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2){ TSK_DEBUG_FATAL("Could not find a usable version of Winsock.dll\n"); tnet_cleanup(); return -2; } else{ __tnet_started = tsk_true; TSK_DEBUG_INFO("The Winsock 2.2 dll was found okay\n"); } } #else __tnet_started = tsk_true; #endif /* TNET_UNDER_WINDOWS */ bail: return err; }
/* * Runnable interface implementation. */ static void* TSK_STDCALL run(void* self) { int ret = 0; tsk_list_item_t *curr; tnet_transport_t *transport = self; TSK_DEBUG_INFO("Transport::run() - enter"); /* create main thread */ if((ret = tsk_thread_create(transport->mainThreadId, tnet_transport_mainthread, transport))){ /* More important than "tsk_runnable_start" ==> start it first. */ TSK_FREE(transport->context); /* Otherwise (tsk_thread_create is ok) will be freed when mainthread exit. */ TSK_DEBUG_FATAL("Failed to create main thread [%d]", ret); return tsk_null; } /* set thread priority iOS and OSX: no incoming pkts (STUN, rtp, dtls...) when thread priority is changed -> to be checked */ #if !TNET_UNDER_APPLE ret = tsk_thread_set_priority(transport->mainThreadId[0], TSK_THREAD_PRIORITY_TIME_CRITICAL); #endif TSK_RUNNABLE_RUN_BEGIN(transport); if((curr = TSK_RUNNABLE_POP_FIRST_SAFE(TSK_RUNNABLE(transport)))){ const tnet_transport_event_t *e = (const tnet_transport_event_t*)curr->data; if (transport->callback) { transport->callback(e); } tsk_object_unref(curr); } TSK_RUNNABLE_RUN_END(transport); TSK_DEBUG_INFO("Transport::run(%s) - exit", transport->description); return tsk_null; }
/**@ingroup txcap_stack_group * Creates new XCAP stack. * @param callback Poiner to the callback function to call when new messages come to the transport layer. * Can be set to Null if you don't want to be alerted. * @param xui user's id as per RFC 4825 subclause 4. Also used to fill @b "X-3GPP-Intended-Identity" header. * This paramter is mandatory and must not be null. If for any reason you'd like to update the user's id, then use @ref TXCAP_STACK_SET_XUI(). * @param password user's password used to authenticate to the XDMS. * This parameter is not mandatory. If for any reason you'd like to update the password, then use @ref TXCAP_STACK_SET_PASSWORD(). * @param xcap_root xcap-root URI as per RFC 4825 subclause 6.1, used to build all request-uris. * This parameter is not mandatory and must be a valid HTPP/HTTPS URL. * @param ... User configuration. You must use @a TXCAP_STACK_SET_*() macros to set these options. * The list of options must always end with @ref TXCAP_STACK_SET_NULL() even if these is no option to pass to the stack. * @retval A Pointer to the newly created stack if succeed and @a Null otherwise. * A stack is a well-defined object. * * @code int test_stack_callback(const thttp_event_t *httpevent); txcap_stack_handle_t* stack = txcap_stack_create(test_stack_callback, "sip:[email protected]", "mysecret", "http://doubango.org:8080/services", // stack-level options TXCAP_STACK_SET_OPTION(TXCAP_STACK_OPTION_TIMEOUT, "6000"), // stack-level headers TXCAP_STACK_SET_HEADER("Connection", "Keep-Alive"), TXCAP_STACK_SET_HEADER("User-Agent", "XDM-client/OMA1.1"), TXCAP_STACK_SET_HEADER("X-3GPP-Intended-Identity", XUI), TXCAP_STACK_SET_NULL()); * @endcode * * @sa @ref txcap_stack_set */ txcap_stack_handle_t* txcap_stack_create(thttp_stack_callback_f callback, const char* xui, const char* password, const char* xcap_root, ...) { txcap_stack_t* ret = tsk_null; if(!xui || !xcap_root){ TSK_DEBUG_ERROR("Both xui and xcap_root are mandatory and should be non-null"); goto bail; } /* check url validity */ if(!thttp_url_isvalid(xcap_root)){ TSK_DEBUG_ERROR("%s is not a valid HTTP/HTTPS url", xcap_root); goto bail; } if(!(ret = tsk_object_new(txcap_stack_def_t, callback, xui, password, xcap_root))){ TSK_DEBUG_FATAL("Failed to create the XCAP stack"); goto bail; } else{ /* set parameters */ va_list ap; va_start(ap, xcap_root); __txcap_stack_set(ret, &ap); va_end(ap); /* credendials */ tsk_strupdate(&ret->xui, xui); tsk_strupdate(&ret->password, password); if(ret->http_session){ thttp_session_set(ret->http_session, THTTP_SESSION_SET_CRED(ret->xui, ret->password), THTTP_SESSION_SET_NULL()); } } bail: return ret; }
/**@ingroup tsk_string_group * Converts string chars into hexadecimal bytes. * * @param str If non-null, the string. * @param size The size. * @param hex If non-null, the hexadecimal. **/ void tsk_str_to_hex(const char *str, tsk_size_t size, uint8_t* hex) { // to avoid SIGBUS error when memory is misaligned do not use sscanf("%2x") TSK_DEBUG_FATAL("Not implemented."); }