NiceSocket * nice_pseudossl_socket_new (NiceSocket *base_socket) { PseudoSSLPriv *priv; NiceSocket *sock = g_slice_new0 (NiceSocket); sock->priv = priv = g_slice_new0 (PseudoSSLPriv); priv->handshaken = FALSE; priv->base_socket = base_socket; sock->type = NICE_SOCKET_TYPE_PSEUDOSSL; sock->fileno = priv->base_socket->fileno; sock->addr = priv->base_socket->addr; sock->send = socket_send; sock->recv = socket_recv; sock->is_reliable = socket_is_reliable; sock->close = socket_close; sock->attach = NULL; /* We send 'to' NULL because it will always be to an already connected * TCP base socket, which ignores the destination */ nice_socket_send (priv->base_socket, NULL, sizeof(SSL_CLIENT_HANDSHAKE), SSL_CLIENT_HANDSHAKE); return sock; }
static gboolean socket_send (NiceSocket *sock, const NiceAddress *to, guint len, const gchar *buf) { PseudoSSLPriv *priv = sock->priv; if (priv->handshaken) { if (priv->base_socket) return nice_socket_send (priv->base_socket, to, len, buf); else return FALSE; } else { add_to_be_sent (sock, to, buf, len); } return TRUE; }
int main (void) { NiceAgent *agent; NiceAddress addr; guint stream; nice_address_init (&addr); g_type_init (); g_thread_init(NULL); loop = g_main_loop_new (NULL, FALSE); agent = nice_agent_new (g_main_loop_get_context (loop), NICE_COMPATIBILITY_RFC5245); nice_address_set_ipv4 (&addr, 0x7f000001); nice_agent_add_local_address (agent, &addr); stream = nice_agent_add_stream (agent, 1); nice_agent_gather_candidates (agent, stream); // attach to default main context nice_agent_attach_recv (agent, stream, NICE_COMPONENT_TYPE_RTP, g_main_loop_get_context (loop), recv_cb, GUINT_TO_POINTER (42)); { NiceCandidate *candidate; GSList *candidates, *i; candidates = nice_agent_get_local_candidates (agent, 1, 1); candidate = candidates->data; nice_socket_send (candidate->sockptr, &(candidate->addr), 6, "\x80hello"); for (i = candidates; i; i = i->next) nice_candidate_free ((NiceCandidate *) i->data); g_slist_free (candidates); } g_main_loop_run (loop); g_object_unref (agent); return 0; }
static gint socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf) { PseudoSSLPriv *priv = sock->priv; if (priv->handshaken) { if (priv->base_socket) return nice_socket_recv (priv->base_socket, from, len, buf); } else { gchar data[sizeof(SSL_SERVER_HANDSHAKE)]; gint ret = -1; if (priv->base_socket) ret = nice_socket_recv (priv->base_socket, from, sizeof(data), data); if (ret <= 0) { return ret; } else if ((guint) ret == sizeof(SSL_SERVER_HANDSHAKE) && memcmp(SSL_SERVER_HANDSHAKE, data, sizeof(SSL_SERVER_HANDSHAKE)) == 0) { struct to_be_sent *tbs = NULL; priv->handshaken = TRUE; while ((tbs = g_queue_pop_head (&priv->send_queue))) { nice_socket_send (priv->base_socket, &tbs->to, tbs->length, tbs->buf); g_free (tbs->buf); g_slice_free (struct to_be_sent, tbs); } } else { if (priv->base_socket) nice_socket_free (priv->base_socket); priv->base_socket = NULL; return -1; } } return 0; }