void * lwm2m_connect_server(uint16_t secObjInstID, void * userData) { client_data_t * dataP; char * uri; char * host; char * port; connection_t * newConnP = NULL; dataP = (client_data_t *)userData; uri = get_server_uri(dataP->securityObjP, secObjInstID); if (uri == NULL) return NULL; // parse uri in the form "coaps://[host]:[port]" if (0==strncmp(uri, "coaps://", strlen("coaps://"))) { host = uri+strlen("coaps://"); } else if (0==strncmp(uri, "coap://", strlen("coap://"))) { host = uri+strlen("coap://"); } else { goto exit; } port = strrchr(host, ':'); if (port == NULL) goto exit; // remove brackets if (host[0] == '[') { host++; if (*(port - 1) == ']') { *(port - 1) = 0; } else goto exit; } // split strings *port = 0; port++; fprintf(stderr, "Opening connection to server at %s:%s\r\n", host, port); newConnP = connection_create(dataP->connList, dataP->sock, host, port, dataP->addressFamily); if (newConnP == NULL) { fprintf(stderr, "Connection creation failed.\r\n"); } else { dataP->connList = newConnP; } exit: lwm2m_free(uri); return (void *)newConnP; }
static void * prv_connect_server(uint16_t secObjInstID, void * userData) { client_data_t * dataP; char * uri; char * host; char * portStr; int port; char * ptr; connection_t * newConnP = NULL; dataP = (client_data_t *)userData; uri = get_server_uri(dataP->securityObjP, secObjInstID); if (uri == NULL) return NULL; // parse uri in the form "coaps://[host]:[port]" if (0==strncmp(uri, "coaps://", strlen("coaps://"))) { host = uri+strlen("coaps://"); } else if (0==strncmp(uri, "coap://", strlen("coap://"))) { host = uri+strlen("coap://"); } else { goto exit; } portStr = strchr(host, ':'); if (portStr == NULL) goto exit; // split strings *portStr = 0; portStr++; port = strtol(portStr, &ptr, 10); if (*ptr != 0) { goto exit; } fprintf(stderr, "Trying to connect to LWM2M Server at %s:%d\r\n", host, port); newConnP = connection_create(dataP->connList, dataP->sock, host, port); if (newConnP == NULL) { fprintf(stderr, "Connection creation failed.\r\n"); } else { dataP->connList = newConnP; } exit: lwm2m_free(uri); return (void *)newConnP; }
/* create a new connection to a server */ static void * prv_connect_server(uint16_t serverID, void * userData) { char * host; char * portStr; char * ptr; int port; connection_t * connP = NULL; bool dtls; printf("Create connection for server %d\n",serverID); // need to created a connection to the server identified by server ID char* uri = get_server_uri(securityObjP, serverID); if (uri == NULL) { printf("server %d not found in security object\n", serverID); return NULL; } printf("URI: %s\n", uri); // parse uri in the form "coaps://[host]:[port]" if (0==strncmp(uri, "coaps://", strlen("coaps://"))) { host = uri+strlen("coaps://"); dtls = true; } else if (0==strncmp(uri, "coap://", strlen("coap://"))) { host = uri+strlen("coap://"); dtls = false; } else { goto exit; } portStr = strchr(host, ':'); if (portStr == NULL) goto exit; // split strings *portStr = 0; portStr++; port = strtol(portStr, &ptr, 10); if (*ptr != 0) { goto exit; } printf("Trying to connect to LWM2M Server at %s:%d\r\n", host, port); // create a connection connP = (connection_t *)malloc(sizeof(connection_t)); if (connP == NULL) { printf("Connection creation fail (malloc)\n"); goto exit; } else { connP->port = port; connP->host = strdup(host); connP->next = connList; connP->ep.set_address(connP->host, port); if (dtls) { // create a tinydtls session connP->dtlsSession = (session_t *)malloc(sizeof(session_t)); connP->dtlsSession->addr = connP->host; connP->dtlsSession->port = port; // nah wait a bit :D dtls_connect(dtls_context,connP->dtlsSession); } else { connP->dtlsSession = NULL; } connList = connP; printf("udp connection created\n"); } exit: free(uri); return connP; }
char * remote_display_host_file (RemoteDisplayHost *host, const char *uri, GError **error) { RemoteDisplayHostPrivate *priv; RemoteDisplayHostFile *file; GChecksum *checksum; const char *str; char *path, *scheme, *ret; GFile *gfile; g_return_val_if_fail (REMOTE_DISPLAY_IS_HOST (host), FALSE); g_return_val_if_fail (uri != NULL, FALSE); priv = GET_PRIVATE (host); scheme = g_uri_parse_scheme (uri); if (g_strcmp0 (scheme, "http") == 0 || g_strcmp0 (scheme, "https") == 0) { g_debug ("Not serving %s", uri); g_free (scheme); return g_strdup (uri); } g_free (scheme); gfile = g_file_new_for_uri (uri); path = g_file_get_path (gfile); g_object_unref (gfile); if (!path) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Cannot serve URI '%s'", uri); return NULL; } if (!priv->server_started) { GSocketAddress *addr; addr = g_inet_socket_address_new (priv->local_address, 0); if (!soup_server_listen (priv->server, addr, 0, error)) { g_clear_object (&addr); g_free (path); return FALSE; } g_object_unref (addr); priv->server_started = TRUE; } checksum = g_checksum_new (G_CHECKSUM_SHA256); g_checksum_update (checksum, (const guchar *) uri, strlen (uri)); str = g_checksum_get_string (checksum); file = g_new0 (RemoteDisplayHostFile, 1); file->uri = g_strdup (uri); file->path = path; file->mime_type = g_content_type_guess (file->path, NULL, 0, NULL); g_hash_table_insert (priv->files, g_strdup (str), file); ret = get_server_uri (priv->server, str); g_checksum_free (checksum); return ret; }
void *lwm2m_connect_server(uint16_t secObjInstID, void *userData) { client_data_t *dataP; char *uri; char *host; char *port; connection_t *newConnP = NULL; coap_protocol_t proto = COAP_UDP; dataP = (client_data_t *)userData; uri = get_server_uri(dataP->securityObjP, secObjInstID); if (uri == NULL) { return NULL; } // parse uri in the form "coaps://[host]:[port]" proto = coap_get_protocol_from_uri(uri); if (proto >= COAP_PROTOCOL_MAX) { fprintf(stderr, "Not supported protocol : %d\n", proto); goto exit; } else { /* move pointer to address field */ host = uri + strlen(coap_uri_prefix[proto]); } port = strrchr(host, ':'); if (port == NULL) { goto exit; } // remove brackets if (host[0] == '[') { host++; if (*(port - 1) == ']') { *(port - 1) = 0; } else { goto exit; } } // split strings *port = 0; port++; fprintf(stdout, "Opening connection to server at %s:%s\r\n", host, port); fprintf(stdout, "Connection protocol type : %d\r\n", proto); newConnP = connection_create(proto, dataP->connP, dataP->sock, host, port, dataP->addressFamily); if (newConnP == NULL) { fprintf(stderr, "Connection creation failed.\r\n"); return NULL; } else { memcpy(&dataP->server_addr, &newConnP->addr, newConnP->addrLen); dataP->server_addrLen = newConnP->addrLen; dataP->connP = newConnP; #ifdef WITH_MBEDTLS if (proto == COAP_TCP_TLS || proto == COAP_UDP_DTLS) { newConnP->session = TLSSession(newConnP->sock, dataP->tls_context, dataP->tls_opt); if (newConnP->session == NULL) { fprintf(stderr, "Failed to create secure session. \r\n"); connection_free(newConnP); newConnP = NULL; goto exit; } dataP->sock = newConnP->session->net.fd; fprintf(stdout, "successfully create secure session. \r\n"); } #endif } exit: lwm2m_free(uri); return (void *)newConnP; }