gboolean xdmcp_client_start (XDMCPClient *client) { if (client->priv->socket) return TRUE; g_autoptr(GError) error = NULL; client->priv->socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &error); if (error) g_warning ("Error creating XDMCP socket: %s", error->message); if (!client->priv->socket) return FALSE; GSocketConnectable *address = g_network_address_new (client->priv->host, client->priv->port); GSocketAddressEnumerator *enumerator = g_socket_connectable_enumerate (address); while (TRUE) { g_autoptr(GError) e = NULL; g_autoptr(GSocketAddress) socket_address = g_socket_address_enumerator_next (enumerator, NULL, &e); if (e) g_warning ("Failed to get socket address: %s", e->message); if (!socket_address) return FALSE; if (!g_socket_connect (client->priv->socket, socket_address, NULL, &e)) { g_warning ("Unable to connect XDMCP socket: %s", error->message); continue; } g_io_add_watch (g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)), G_IO_IN, xdmcp_data_cb, client); return TRUE; } }
static int remote_connect(const char* host, unsigned port, void** connectiondata) { g_message("connecting to remote: %s on port %u", host, port); GSocket* sock = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL); GSocketConnectable* addr = g_network_address_new(host, port); GSocketAddressEnumerator* enumerator = g_socket_connectable_enumerate(addr); int ret = EMCUETITI_PORT_REMOTE_TRYAGAIN; GSocketAddress* sockaddr; while ((sockaddr = g_socket_address_enumerator_next(enumerator, NULL, NULL))) { g_message("connecting..."); GError* error = NULL; if (g_socket_connect(sock, sockaddr, NULL, &error)) { g_message("connected"); g_socket_set_blocking(sock, FALSE); remote_wireupsocketcallback(sock); *connectiondata = sock; ret = EMCUETITI_PORT_REMOTE_OK; break; } else { g_message("failed to connect %d:%s", error->code, error->message); } } if (ret != EMCUETITI_PORT_REMOTE_OK) g_object_unref(sock); g_object_unref(addr); g_object_unref(enumerator); return ret; }
/* TODO: could use dbus, but for portability maybe not..... */ static void do_connect(SpiceVDAgent *agent) { GError *error = NULL; GSocketAddressEnumerator *enumerator; GSocketAddress *address; #ifdef G_OS_UNIX agent->socket = g_socket_new(G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, 0, &error); agent->connectable = G_SOCKET_CONNECTABLE(g_unix_socket_address_new(vdagentd_socket)); #endif enumerator = g_socket_connectable_enumerate (agent->connectable); while (TRUE) { address = g_socket_address_enumerator_next (enumerator, agent->cancellable, NULL); if (!address) { g_error("can't connect to socket"); exit(1); } if (g_socket_connect(agent->socket, address, agent->cancellable, &error)) break; g_debug("Connection failed, trying next"); g_clear_error (&error); g_object_unref (address); } g_object_unref(enumerator); agent->connection = G_IO_STREAM(g_socket_connection_factory_create_connection(agent->socket)); g_debug("Connected to %s %p", vdagentd_socket, agent->connection); send_xorg_config(agent); }
static GSocket * open_snapd_socket (GCancellable *cancellable, GError **error) { GSocket *socket; g_autoptr(GSocketAddress) address = NULL; g_autoptr(GError) error_local = NULL; socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error_local); if (!socket) { g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_NOT_SUPPORTED, "Unable to open snapd socket: %s", error_local->message); return NULL; } address = g_unix_socket_address_new (SNAPD_SOCKET); if (!g_socket_connect (socket, address, cancellable, &error_local)) { g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_NOT_SUPPORTED, "Unable to connect snapd socket: %s", error_local->message); g_object_unref (socket); return NULL; } return socket; }
gint main (void) { gboolean ret; GSocket *socket = NULL; GSocketAddress *address = NULL; GError *error = NULL; gsize wrote; const gchar *buffer = "ping\n"; const gchar *socket_filename = "/tmp/pk-self-test.socket"; GSource *source; GMainLoop *loop; g_type_init (); loop = g_main_loop_new (NULL, FALSE); /* create socket */ socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (socket == NULL) { g_warning ("failed to create socket: %s", error->message); g_error_free (error); goto out; } g_socket_set_blocking (socket, FALSE); g_socket_set_keepalive (socket, TRUE); /* connect to it */ address = g_unix_socket_address_new (socket_filename); ret = g_socket_connect (socket, address, NULL, &error); if (!ret) { g_warning ("failed to connect to socket: %s", error->message); g_error_free (error); goto out; } /* socket has data */ source = g_socket_create_source (socket, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL); g_source_set_callback (source, (GSourceFunc) pk_socket_example_accept_connection_cb, loop, NULL); g_source_attach (source, NULL); /* send some data */ wrote = g_socket_send (socket, buffer, 5, NULL, &error); if (wrote != 5) { g_warning ("failed to write 5 bytes"); goto out; } /* run main loop */ g_debug ("running main loop"); g_main_loop_run (loop); out: if (loop != NULL) g_main_loop_unref (loop); if (socket != NULL) g_object_unref (socket); if (address != NULL) g_object_unref (address); return 0; }
/*! * Create a new \ref clr_oci_vm_conn and connect to hypervisor to * perform initial welcome negotiation. * * \param socket_path Full path to named socket. * \param pid Process ID of running hypervisor. * * \return \ref clr_oci_vm_conn on success, else \c NULL. */ static struct clr_oci_vm_conn * clr_oci_vm_conn_new (const gchar *socket_path, GPid pid) { struct clr_oci_vm_conn *conn = NULL; GError *error = NULL; gboolean ret = false; g_assert (socket_path); g_assert (pid); conn = g_new0 (struct clr_oci_vm_conn, 1); if (! conn) { return NULL; } g_strlcpy (conn->socket_path, socket_path, sizeof (conn->socket_path)); conn->socket_addr = g_unix_socket_address_new (socket_path); if (! conn->socket_addr) { g_critical ("socket path does not exist: %s", socket_path); goto err; } conn->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (! conn->socket) { g_critical ("failed to create socket: %s", error->message); g_error_free (error); goto err; } ret = g_socket_connect (conn->socket, conn->socket_addr, NULL, &error); if (! ret) { g_critical ("failed to connect to socket: %s", error->message); g_error_free (error); goto err; } g_debug ("connected to socket path %s", socket_path); ret = clr_oci_qmp_check_welcome (conn->socket); if (! ret) { goto err; } return conn; err: clr_oci_vm_conn_free (conn); return NULL; }
//Bfoadcast ivy msgs to clients void broadcast_to_clients () { int i; if (uTCP) { //broadcast using tcp connection GError *error = NULL; for (i = 0; i < MAXCLIENT; i++) { if (ConnectedClients[i].used > 0) { GOutputStream * ostream = g_io_stream_get_output_stream (ConnectedClients[i].ClientTcpData); g_output_stream_write(ostream, ivybuffer, strlen(ivybuffer), NULL, &error); } } return; } i=0; for (i = 0; i < MAXCLIENT; i++) { if (ConnectedClients[i].used > 0) { //Send data GInetAddress *udpAddress; GSocketAddress *udpSocketAddress; GSocket *udpSocket; udpAddress = g_inet_address_new_from_string(ConnectedClients[i].client_ip); udpSocketAddress = g_inet_socket_address_new(udpAddress, udp_port); udpSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL); if (g_socket_connect(udpSocket, udpSocketAddress, NULL, NULL) == FALSE) { printf("App Server: stg wrong with socket connect\n"); fflush(stdout); } if (g_socket_send(udpSocket, ivybuffer, strlen(ivybuffer) , NULL, NULL) < 0) { printf("App Server: stg wrong with send func\n"); fflush(stdout); } if (g_socket_close(udpSocket, NULL) == FALSE) { printf("App Server: stg wrong with socket close\n"); fflush(stdout); } //Unref objects g_object_unref(udpAddress); g_object_unref(udpSocketAddress); g_object_unref(udpSocket); } } }
void xg_socket_connect (GSocket *socket, GSocketAddress *address) { gboolean ret; GError *error = NULL; ret = g_socket_connect(socket, address, NULL, &error); if (ret == FALSE) { g_critical ("g_socket_connect returned FALSE: %s", error->message); } }
gint interface_createAddress(struct node *n) //GInetAddress *addr) { GError * e = NULL; GInetAddress *lo = g_inet_address_new_loopback( G_SOCKET_FAMILY_IPV6); GSocketAddress *sa = g_inet_socket_address_new( lo, 42); GSocket *s = n->ubnetd; if( s != NULL ){ g_socket_close(s,NULL); g_object_unref(s); } s = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &e); if( e != NULL ){ syslog(LOG_ERR,"interface_pushAddress: Error while creating socket: %s\n", e->message); g_error_free(e); return -1; } g_socket_connect(s, sa, NULL, &e); if( e != NULL ){ syslog(LOG_ERR,"interface_pushAddress: Error while connecting: %s\n", e->message); g_error_free(e); return -1; } g_socket_send(s,"A",1,NULL,NULL); gchar *bytes = (gchar*)g_inet_address_to_bytes(n->netadr); g_socket_send(s,bytes,16,NULL,NULL); n->ubnetd = s; //gchar buf[1]; //g_socket_receive(s,buf,1,NULL,NULL); return 0; }
KmsSCTPResult kms_sctp_connection_connect (KmsSCTPConnection * conn, GCancellable * cancellable, GError ** err) { g_return_val_if_fail (conn != NULL, KMS_SCTP_ERROR); g_return_val_if_fail (conn->socket != NULL, KMS_SCTP_ERROR); g_return_val_if_fail (conn->saddr != NULL, KMS_SCTP_ERROR); if (g_socket_is_connected (conn->socket)) return KMS_SCTP_OK; /* connect to server */ if (!g_socket_connect (conn->socket, conn->saddr, cancellable, err)) return KMS_SCTP_ERROR; if (G_UNLIKELY (GST_LEVEL_DEBUG <= _gst_debug_min)) { #if defined (SCTP_INITMSG) struct sctp_initmsg initmsg; socklen_t optlen; if (getsockopt (g_socket_get_fd (conn->socket), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, &optlen) < 0) GST_WARNING ("Could not get SCTP configuration: %s (%d)", g_strerror (errno), errno); else GST_DEBUG ("SCTP client socket: ostreams %u, instreams %u", initmsg.sinit_num_ostreams, initmsg.sinit_num_ostreams); #else GST_WARNING ("don't know how to get the configuration of the " "SCTP initiation structure on this OS."); #endif } GST_DEBUG ("connected sctp socket"); return KMS_SCTP_OK; }
//Bfoadcast ivy msgs to clients void broadcast_to_clients () { int i; for (i = 0; i < MAXCLIENT; i++) { if (ConnectedClients[i].used > 0) { //Send data GInetAddress *udpAddress; GSocketAddress *udpSocketAddress; GSocket *udpSocket; udpAddress = g_inet_address_new_from_string(ConnectedClients[i].client_ip); udpSocketAddress = g_inet_socket_address_new(udpAddress, udp_port); udpSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL); if (g_socket_connect(udpSocket, udpSocketAddress, NULL, NULL) == FALSE) { printf("App Server: stg wrong with socket connect\n"); fflush(stdout); } if (g_socket_send(udpSocket, ivybuffer, strlen(ivybuffer) , NULL, NULL) < 0) { printf("App Server: stg wrong with send func\n"); fflush(stdout); } if (g_socket_close(udpSocket, NULL) == FALSE) { printf("App Server: stg wrong with socket close\n"); fflush(stdout); } //Unref objects g_object_unref(udpAddress); g_object_unref(udpSocketAddress); g_object_unref(udpSocket); } } }
/* create a socket for sending to remote machine */ static void gst_sctp_base_sink_create_socket (GstSCTPBaseSink * self) { GSocketAddress *saddr; GResolver *resolver; GInetAddress *addr; GError *err = NULL; if (GST_OBJECT_FLAG_IS_SET (self, GST_SCTP_BASE_SINK_OPEN)) return; /* look up name if we need to */ addr = g_inet_address_new_from_string (self->priv->host); if (addr == NULL) { GList *results; resolver = g_resolver_get_default (); results = g_resolver_lookup_by_name (resolver, self->priv->host, self->priv->cancellable, &err); if (results == NULL) goto name_resolve; addr = G_INET_ADDRESS (g_object_ref (results->data)); g_resolver_free_addresses (results); g_object_unref (resolver); } if (G_UNLIKELY (GST_LEVEL_DEBUG <= _gst_debug_min)) { gchar *ip = g_inet_address_to_string (addr); GST_DEBUG_OBJECT (self, "IP address for host %s is %s", self->priv->host, ip); g_free (ip); } saddr = g_inet_socket_address_new (addr, self->priv->port); g_object_unref (addr); /* create sending client socket */ GST_DEBUG_OBJECT (self, "opening sending client socket to %s:%d", self->priv->host, self->priv->port); self->priv->socket = g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_SCTP, &err); if (self->priv->socket == NULL) goto no_socket; #if defined (SCTP_INITMSG) { struct sctp_initmsg initmsg; memset (&initmsg, 0, sizeof (initmsg)); initmsg.sinit_num_ostreams = self->priv->num_ostreams; initmsg.sinit_max_instreams = self->priv->max_istreams; if (setsockopt (g_socket_get_fd (self->priv->socket), IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof (initmsg)) < 0) GST_ELEMENT_WARNING (self, RESOURCE, SETTINGS, (NULL), ("Could not configure SCTP socket: %s (%d)", g_strerror (errno), errno)); } #else GST_WARNING_OBJECT (self, "don't know how to configure the SCTP initiation " "parameters on this OS."); #endif GST_DEBUG_OBJECT (self, "opened sending client socket"); /* connect to server */ if (!g_socket_connect (self->priv->socket, saddr, self->priv->cancellable, &err)) goto connect_failed; g_object_unref (saddr); GST_OBJECT_FLAG_SET (self, GST_SCTP_BASE_SINK_OPEN); GST_DEBUG ("Created sctp socket"); return; no_socket: { GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL), ("Failed to create socket: %s", err->message)); g_clear_error (&err); g_object_unref (saddr); return; } name_resolve: { if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { GST_DEBUG_OBJECT (self, "Cancelled name resolval"); } else { GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL), ("Failed to resolve host '%s': %s", self->priv->host, err->message)); } g_clear_error (&err); g_object_unref (resolver); return; } connect_failed: { if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { GST_DEBUG_OBJECT (self, "Cancelled connecting"); } else { GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL), ("Failed to connect to host '%s:%d': %s", self->priv->host, self->priv->port, err->message)); } g_clear_error (&err); g_object_unref (saddr); /* pretend we opened ok for proper cleanup to happen */ GST_OBJECT_FLAG_SET (self, GST_SCTP_BASE_SINK_OPEN); gst_sctp_base_sink_destroy_socket (self); return; } }
NiceSocket * nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr) { union { struct sockaddr_storage storage; struct sockaddr addr; } name; NiceSocket *sock; TcpPriv *priv; GSocket *gsock = NULL; GError *gerr = NULL; gboolean gret = FALSE; GSocketAddress *gaddr; if (addr == NULL) { /* We can't connect a tcp socket with no destination address */ return NULL; } sock = g_slice_new0 (NiceSocket); nice_address_copy_to_sockaddr (addr, &name.addr); if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) { gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL); name.storage.ss_family = AF_INET; #ifdef HAVE_SA_LEN name.storage.ss_len = sizeof (struct sockaddr_in); #endif } else if (name.storage.ss_family == AF_INET6) { gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL); name.storage.ss_family = AF_INET6; #ifdef HAVE_SA_LEN name.storage.ss_len = sizeof (struct sockaddr_in6); #endif } if (gsock == NULL) { g_slice_free (NiceSocket, sock); return NULL; } gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name)); if (gaddr == NULL) { g_object_unref (gsock); g_slice_free (NiceSocket, sock); return NULL; } /* GSocket: All socket file descriptors are set to be close-on-exec. */ g_socket_set_blocking (gsock, false); gret = g_socket_connect (gsock, gaddr, NULL, &gerr); g_object_unref (gaddr); if (gret == FALSE) { if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_PENDING) == FALSE) { g_error_free (gerr); g_socket_close (gsock, NULL); g_object_unref (gsock); g_slice_free (NiceSocket, sock); return NULL; } g_error_free (gerr); } gaddr = g_socket_get_local_address (gsock, NULL); if (gaddr == NULL || !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) { g_slice_free (NiceSocket, sock); g_socket_close (gsock, NULL); g_object_unref (gsock); return NULL; } g_object_unref (gaddr); nice_address_set_from_sockaddr (&sock->addr, &name.addr); sock->priv = priv = g_slice_new0 (TcpPriv); if (ctx == NULL) ctx = g_main_context_default (); priv->context = g_main_context_ref (ctx); priv->server_addr = *addr; priv->error = FALSE; sock->fileno = gsock; sock->send_messages = socket_send_messages; sock->recv_messages = socket_recv_messages; sock->is_reliable = socket_is_reliable; sock->close = socket_close; return sock; }
static gboolean make_connection (const char *argument, GTlsCertificate *certificate, GCancellable *cancellable, GSocket **socket, GSocketAddress **address, GIOStream **connection, GInputStream **istream, GOutputStream **ostream, GError **error) { GSocketType socket_type; GSocketFamily socket_family; GSocketAddressEnumerator *enumerator; GSocketConnectable *connectable; GSocketAddress *src_address; GTlsInteraction *interaction; GError *err = NULL; if (use_udp) socket_type = G_SOCKET_TYPE_DATAGRAM; else socket_type = G_SOCKET_TYPE_STREAM; if (unix_socket) socket_family = G_SOCKET_FAMILY_UNIX; else socket_family = G_SOCKET_FAMILY_IPV4; *socket = g_socket_new (socket_family, socket_type, 0, error); if (*socket == NULL) return FALSE; if (read_timeout) g_socket_set_timeout (*socket, read_timeout); if (unix_socket) { GSocketAddress *addr; addr = socket_address_from_string (argument); if (addr == NULL) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Could not parse '%s' as unix socket name", argument); return FALSE; } connectable = G_SOCKET_CONNECTABLE (addr); } else { connectable = g_network_address_parse (argument, 7777, error); if (connectable == NULL) return FALSE; } enumerator = g_socket_connectable_enumerate (connectable); while (TRUE) { *address = g_socket_address_enumerator_next (enumerator, cancellable, error); if (*address == NULL) { if (error != NULL && *error == NULL) g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No more addresses to try"); return FALSE; } if (g_socket_connect (*socket, *address, cancellable, &err)) break; g_message ("Connection to %s failed: %s, trying next", socket_address_to_string (*address), err->message); g_clear_error (&err); g_object_unref (*address); } g_object_unref (enumerator); g_print ("Connected to %s\n", socket_address_to_string (*address)); src_address = g_socket_get_local_address (*socket, error); if (!src_address) { g_prefix_error (error, "Error getting local address: "); return FALSE; } g_print ("local address: %s\n", socket_address_to_string (src_address)); g_object_unref (src_address); if (use_udp) { *connection = NULL; *istream = NULL; *ostream = NULL; } else *connection = G_IO_STREAM (g_socket_connection_factory_create_connection (*socket)); if (tls) { GIOStream *tls_conn; tls_conn = g_tls_client_connection_new (*connection, connectable, error); if (!tls_conn) { g_prefix_error (error, "Could not create TLS connection: "); return FALSE; } g_signal_connect (tls_conn, "accept-certificate", G_CALLBACK (accept_certificate), NULL); interaction = g_tls_console_interaction_new (); g_tls_connection_set_interaction (G_TLS_CONNECTION (tls_conn), interaction); g_object_unref (interaction); if (certificate) g_tls_connection_set_certificate (G_TLS_CONNECTION (tls_conn), certificate); g_object_unref (*connection); *connection = G_IO_STREAM (tls_conn); if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn), cancellable, error)) { g_prefix_error (error, "Error during TLS handshake: "); return FALSE; } } g_object_unref (connectable); if (*connection) { *istream = g_io_stream_get_input_stream (*connection); *ostream = g_io_stream_get_output_stream (*connection); } return TRUE; }
int moloch_http_connect(MolochConn_t *conn, char *name, int defaultport, int blocking) { GError *error = 0; GSocketConnectable *connectable; GSocketAddressEnumerator *enumerator; GSocketAddress *sockaddr; struct timeval startTime; struct timeval stopTime; gettimeofday(&startTime, NULL); connectable = g_network_address_parse(name, defaultport, &error); if (error) { LOG("%p: Couldn't parse connect string of %s", (void*)conn, name); exit(0); } conn->name = name; enumerator = g_socket_connectable_enumerate (connectable); g_object_unref(connectable); while (!conn->conn && (sockaddr = g_socket_address_enumerator_next (enumerator, NULL, &error))) { conn->conn = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &error); if (!error) { GValue value = G_VALUE_INIT; g_value_init (&value, G_TYPE_BOOLEAN); g_value_set_boolean (&value, blocking); g_object_set_property(G_OBJECT(conn->conn), "blocking", &value); g_socket_connect(conn->conn, sockaddr, NULL, &error); } if (error && error->code != G_IO_ERROR_PENDING) { g_object_unref (conn->conn); conn->conn = NULL; } else { struct sockaddr_in localAddress, remoteAddress; socklen_t addressLength = sizeof(localAddress); getsockname(g_socket_get_fd(conn->conn), (struct sockaddr*)&localAddress, &addressLength); g_socket_address_to_native(sockaddr, &remoteAddress, addressLength, NULL); char sessionId[MOLOCH_SESSIONID_LEN]; moloch_session_id(sessionId, localAddress.sin_addr.s_addr, localAddress.sin_port, remoteAddress.sin_addr.s_addr, remoteAddress.sin_port); DEBUGCONN("AAA connected %s %p %s", conn->server->names[0], conn, moloch_friendly_session_id(17, localAddress.sin_addr.s_addr, htons(localAddress.sin_port), remoteAddress.sin_addr.s_addr, htons(remoteAddress.sin_port))); HASH_ADD(h_, connections, sessionId, conn); memcpy(&conn->sessionIda, sessionId, 8); memcpy(&conn->sessionIdb, sessionId+8, 4); conn->server->numConns++; } g_object_unref (sockaddr); } g_object_unref (enumerator); if (conn->conn) { if (error) { g_error_free(error); error = 0; } } else if (error) { LOG("%p: Error: %s", (void*)conn, error->message); } if (error || !conn->conn) { if (config.logESRequests) LOG("Connecting %p %s %d %d FAIL", (void*)conn, name, defaultport, blocking); conn->server->lastFailedConnect = time(0); return 1; } //g_object_ref (conn->conn); g_socket_set_keepalive(conn->conn, TRUE); int fd = g_socket_get_fd(conn->conn); conn->readWatch = moloch_watch_fd(fd, MOLOCH_GIO_READ_COND, moloch_http_read_cb, conn); if (!blocking) { conn->writeWatch = moloch_watch_fd(fd, G_IO_OUT, moloch_http_conn_cb, conn); DEBUGCONN("AAA connwritewatch %s %p fd:%d ww:%d", conn->server->names[0], conn, fd, conn->writeWatch); } int sendbuff = 0; socklen_t optlen = sizeof(sendbuff); int res = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen); if(res != -1 && sendbuff < 300000) { sendbuff = 300000; setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); } #ifdef TCP_KEEPIDLE res = getsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &sendbuff, &optlen); if(res != -1 && sendbuff > 60*8) { sendbuff = 60*8; setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &sendbuff, sizeof(sendbuff)); } #endif gettimeofday(&stopTime, NULL); if (config.logESRequests) LOG("Connecting %p %s %d %d %ldms", (void*)conn, name, defaultport, blocking, (stopTime.tv_sec - startTime.tv_sec)*1000 + (stopTime.tv_usec - startTime.tv_usec)/1000); return 0; }
static void _channel_accepted (TpChannel *channel, const GValue *addressv, const GError *in_error, gpointer user_data, GObject *obj) { TpStreamTubeChannel *self = (TpStreamTubeChannel *) obj; GSocketAddress *remote_address; GError *error = NULL; if (in_error != NULL) { DEBUG ("Failed to Accept Stream Tube: %s", in_error->message); operation_failed (self, in_error); return; } tp_cli_channel_type_stream_tube_connect_to_new_local_connection ( TP_CHANNEL (self), new_local_connection_cb, NULL, NULL, G_OBJECT (self), &error); if (error != NULL) { DEBUG ("Failed to connect to NewLocalConnection signal"); operation_failed (self, error); g_error_free (error); return; } remote_address = tp_g_socket_address_from_variant (self->priv->socket_type, addressv, &error); if (error != NULL) { DEBUG ("Failed to convert address: %s", error->message); operation_failed (self, error); g_error_free (error); return; } /* Connect to CM */ g_socket_set_blocking (self->priv->client_socket, FALSE); g_socket_connect (self->priv->client_socket, remote_address, NULL, &error); if (error == NULL) { /* Socket is connected */ client_socket_connected (self); goto out; } else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_PENDING)) { /* We have to wait that the socket is connected */ GSource *source; source = g_socket_create_source (self->priv->client_socket, G_IO_OUT, NULL); g_source_attach (source, g_main_context_get_thread_default ()); g_source_set_callback (source, (GSourceFunc) client_socket_cb, self, NULL); g_error_free (error); g_source_unref (source); } else { DEBUG ("Failed to connect to CM: %s", error->message); operation_failed (self, error); g_error_free (error); } out: g_object_unref (remote_address); }
int main (int argc, char *argv[]) { GSocket *sock = NULL; gchar *addr = NULL; GInetAddress *iaddr = NULL; GSocketAddress *saddr = NULL; gboolean pass = FALSE; guint8 data[4] = { 0, 2, 0 , 0 }; GOptionEntry entries[] = { { "addr", 'a', 0, G_OPTION_ARG_STRING, &addr, "Phone IP address", NULL }, { NULL } }; GOptionContext *context = NULL; GError *error = NULL; g_type_init (); context = g_option_context_new ("- htc nat"); g_option_context_add_main_entries (context, entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_critical ("Option parsing failed: %s", error->message); g_clear_error (&error); goto option_fail; } if (!addr) { gchar *help = g_option_context_get_help (context, TRUE, NULL); g_fprintf (stderr, "%s", help); g_free (help); goto addr_fail; } g_option_context_free (context); sock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (!sock) { g_critical ("Create socket failed: %s", error->message); g_clear_error (&error); goto sock_fail; } g_socket_set_ttl (sock, 128); iaddr = g_inet_address_new_from_string (addr); saddr = g_inet_socket_address_new (iaddr, 6000); pass = g_socket_connect (sock, saddr, NULL, &error); g_object_unref (iaddr); g_object_unref (saddr); if (!pass) { g_critical ("Connect to server failed: %s", error->message); g_clear_error (&error); goto connect_fail; } pass = g_socket_send (sock, data, 4, NULL, &error); if (!pass) { g_critical ("Send data failed: %s", error->message); g_clear_error (&error); goto send_fail; } g_message ("Sent!"); g_socket_close (sock, NULL); g_object_unref (sock); g_free (addr); return 0; send_fail: connect_fail: g_socket_close (sock, NULL); g_object_unref (sock); sock_fail: g_free (addr); addr_fail: g_option_context_free (context); option_fail: return -1; }
int moloch_http_connect(MolochConn_t *conn, char *name, int defaultport, int blocking) { GError *error = 0; GSocketConnectable *connectable; GSocketAddressEnumerator *enumerator; GSocketAddress *sockaddr; if (config.logESRequests) LOG("Connecting %p %s", (void*)conn, name); connectable = g_network_address_parse(name, defaultport, &error); if (error) { LOG("%p: Couldn't parse connect string of %s", (void*)conn, name); exit(0); } conn->name = name; enumerator = g_socket_connectable_enumerate (connectable); g_object_unref(connectable); while (!conn->conn && (sockaddr = g_socket_address_enumerator_next (enumerator, NULL, &error))) { conn->conn = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &error); if (!error) { GValue value = G_VALUE_INIT; g_value_init (&value, G_TYPE_BOOLEAN); g_value_set_boolean (&value, blocking); g_object_set_property(G_OBJECT(conn->conn), "blocking", &value); g_socket_connect(conn->conn, sockaddr, NULL, &error); } if (error && error->code != G_IO_ERROR_PENDING) { g_object_unref (conn->conn); conn->conn = NULL; } g_object_unref (sockaddr); } g_object_unref (enumerator); if (conn->conn) { if (error) { g_error_free(error); error = 0; } } else if (error) { LOG("%p: Error: %s", (void*)conn, error->message); } if (error || !conn->conn) { conn->server->lastFailedConnect = time(0); return 1; } //g_object_ref (conn->conn); g_socket_set_keepalive(conn->conn, TRUE); int fd = g_socket_get_fd(conn->conn); moloch_watch_fd(fd, MOLOCH_GIO_READ_COND, moloch_http_read_cb, conn); int sendbuff = 0; socklen_t optlen = sizeof(sendbuff); int res = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen); if(res != -1 && sendbuff < 300000) { sendbuff = 300000; setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); } #ifdef TCP_KEEPIDLE res = getsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &sendbuff, &optlen); if(res != -1 && sendbuff > 60*8) { sendbuff = 60*8; setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &sendbuff, sizeof(sendbuff)); } #endif return 0; }
NiceSocket * nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *local_addr, NiceAddress *remote_addr, gboolean reliable) { union { struct sockaddr_storage storage; struct sockaddr addr; } name; NiceSocket *sock; GSocket *gsock = NULL; GError *gerr = NULL; gboolean gret = FALSE; GSocketAddress *gaddr; if (remote_addr == NULL) { /* We can't connect a tcp socket with no destination address */ return NULL; } nice_address_copy_to_sockaddr (remote_addr, &name.addr); if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) { gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL); name.storage.ss_family = AF_INET; #ifdef HAVE_SA_LEN name.storage.ss_len = sizeof (struct sockaddr_in); #endif } else if (name.storage.ss_family == AF_INET6) { gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL); name.storage.ss_family = AF_INET6; #ifdef HAVE_SA_LEN name.storage.ss_len = sizeof (struct sockaddr_in6); #endif } if (gsock == NULL) { return NULL; } gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name)); if (gaddr == NULL) { g_object_unref (gsock); return NULL; } /* GSocket: All socket file descriptors are set to be close-on-exec. */ g_socket_set_blocking (gsock, false); gret = g_socket_connect (gsock, gaddr, NULL, &gerr); g_object_unref (gaddr); if (gret == FALSE) { if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_PENDING) == FALSE) { g_error_free (gerr); g_socket_close (gsock, NULL); g_object_unref (gsock); return NULL; } g_error_free (gerr); } nice_address_copy_to_sockaddr (local_addr, &name.addr); gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name)); if (gaddr == NULL) { g_socket_close (gsock, NULL); g_object_unref (gsock); return NULL; } g_socket_bind (gsock, gaddr, FALSE, NULL); g_object_unref (gaddr); sock = nice_tcp_bsd_socket_new_from_gsock (ctx, gsock, local_addr, remote_addr, reliable); g_object_unref (gsock); return sock; }
int main (int argc, char *argv[]) { GSocket *socket; GSocketAddress *src_address; GSocketAddress *address; GSocketType socket_type; GSocketFamily socket_family; GError *error = NULL; GOptionContext *context; GCancellable *cancellable; GSocketAddressEnumerator *enumerator; GSocketConnectable *connectable; g_thread_init (NULL); g_type_init (); context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff"); g_option_context_add_main_entries (context, cmd_entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("%s: %s\n", argv[0], error->message); return 1; } if (argc != 2) { g_printerr ("%s: %s\n", argv[0], "Need to specify hostname / unix socket name"); return 1; } if (cancel_timeout) { cancellable = g_cancellable_new (); g_thread_create (cancel_thread, cancellable, FALSE, NULL); } else { cancellable = NULL; } loop = g_main_loop_new (NULL, FALSE); if (use_udp) socket_type = G_SOCKET_TYPE_DATAGRAM; else socket_type = G_SOCKET_TYPE_STREAM; if (unix_socket) socket_family = G_SOCKET_FAMILY_UNIX; else socket_family = G_SOCKET_FAMILY_IPV4; socket = g_socket_new (socket_family, socket_type, 0, &error); if (socket == NULL) { g_printerr ("%s: %s\n", argv[0], error->message); return 1; } if (read_timeout) g_socket_set_timeout (socket, read_timeout); if (unix_socket) { GSocketAddress *addr; addr = socket_address_from_string (argv[1]); if (addr == NULL) { g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]); return 1; } connectable = G_SOCKET_CONNECTABLE (addr); } else { connectable = g_network_address_parse (argv[1], 7777, &error); if (connectable == NULL) { g_printerr ("%s: %s\n", argv[0], error->message); return 1; } } enumerator = g_socket_connectable_enumerate (connectable); while (TRUE) { address = g_socket_address_enumerator_next (enumerator, cancellable, &error); if (address == NULL) { if (error == NULL) g_printerr ("%s: No more addresses to try\n", argv[0]); else g_printerr ("%s: %s\n", argv[0], error->message); return 1; } if (g_socket_connect (socket, address, cancellable, &error)) break; g_printerr ("%s: Connection to %s failed: %s, trying next\n", argv[0], socket_address_to_string (address), error->message); g_error_free (error); error = NULL; g_object_unref (address); } g_object_unref (enumerator); g_object_unref (connectable); g_print ("Connected to %s\n", socket_address_to_string (address)); /* TODO: Test non-blocking connect */ if (non_blocking) g_socket_set_blocking (socket, FALSE); src_address = g_socket_get_local_address (socket, &error); if (!src_address) { g_printerr ("Error getting local address: %s\n", error->message); return 1; } g_print ("local address: %s\n", socket_address_to_string (src_address)); g_object_unref (src_address); while (TRUE) { gchar buffer[4096]; gssize size; gsize to_send; if (fgets (buffer, sizeof buffer, stdin) == NULL) break; to_send = strlen (buffer); while (to_send > 0) { ensure_condition (socket, "send", cancellable, G_IO_OUT); if (use_udp) size = g_socket_send_to (socket, address, buffer, to_send, cancellable, &error); else size = g_socket_send (socket, buffer, to_send, cancellable, &error); if (size < 0) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { g_print ("socket send would block, handling\n"); g_error_free (error); error = NULL; continue; } else { g_printerr ("Error sending to socket: %s\n", error->message); return 1; } } g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size); if (size == 0) { g_printerr ("Unexpected short write\n"); return 1; } to_send -= size; } ensure_condition (socket, "receive", cancellable, G_IO_IN); if (use_udp) size = g_socket_receive_from (socket, &src_address, buffer, sizeof buffer, cancellable, &error); else size = g_socket_receive (socket, buffer, sizeof buffer, cancellable, &error); if (size < 0) { g_printerr ("Error receiving from socket: %s\n", error->message); return 1; } if (size == 0) break; g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size); if (use_udp) g_print (" from %s", socket_address_to_string (src_address)); g_print ("\n"); if (verbose) g_print ("-------------------------\n" "%.*s" "-------------------------\n", (int)size, buffer); } g_print ("closing socket\n"); if (!g_socket_close (socket, &error)) { g_printerr ("Error closing master socket: %s\n", error->message); return 1; } g_object_unref (G_OBJECT (socket)); g_object_unref (G_OBJECT (address)); return 0; }