void ntl_net_send(ntl_Net* n, const char* pkt) { if ( n && n->sock ) { GIOChannel* chan = gnet_tcp_socket_get_io_channel(n->sock); gchar* data = g_strdup_printf("%s\n", pkt); gsize wrote = 0; gnet_io_channel_writen(chan, data, strlen(data), &wrote); g_free(data); } }
void handle_client(GTcpSocket *client) { RequestHeader *request_header; GIOChannel *ioclient; GInetAddr *addr; GIOError error; GPtrArray *request_query; gchar* name; gint port; gchar read_buffer[1024]; gchar write_buffer[1024]; gsize read_buffer_n; gsize write_buffer_n; ioclient = gnet_tcp_socket_get_io_channel(client); g_assert (ioclient); addr = gnet_tcp_socket_get_remote_inetaddr(client); g_assert (addr); name = gnet_inetaddr_get_canonical_name (addr); g_assert (name); port = gnet_inetaddr_get_port (addr); g_print ("Accepted connection from %s:%d\n", name, port); g_free(name); request_query = g_ptr_array_new(); while ((error = gnet_io_channel_readline(ioclient, read_buffer, sizeof(read_buffer), &read_buffer_n)) == G_IO_ERROR_NONE && (read_buffer_n > 0)) { if(g_strcmp0(read_buffer, BLANK_LINE) == 0) { break; }else { g_ptr_array_add(request_query, g_strdup(read_buffer)); } } request_header = voy_request_header_initialize(); voy_request_header_create(request_query, request_header); voy_request_header_print(request_header); g_stpcpy(write_buffer, "HTTP/1.0 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n<h1>Hello from server!</h1>"); write_buffer_n = strlen(write_buffer); gnet_io_channel_writen(ioclient, write_buffer, write_buffer_n, &write_buffer_n); g_ptr_array_free(request_query, TRUE); gnet_inetaddr_delete (addr); voy_request_header_free(request_header); }
void net_send(unsigned char *data) { printf("Sending: %s\n",data); GIOError error = G_IO_ERROR_NONE; gsize n = strlen(data); error = gnet_io_channel_writen(msn_channel, data, n, &n); if (error != G_IO_ERROR_NONE) { error_dialog("Data couldn't be sent"); return; } }
void net_close() { if(msn_channel == NULL) return; printf("Logging out\n"); GIOError error = G_IO_ERROR_NONE; gsize n = 5; error = gnet_io_channel_writen(msn_channel, "OUT\r\n", n, &n); if (error != G_IO_ERROR_NONE) { error_dialog("Couldn't log out."); return; } }
static void normal_echoserver(gchar *path, gboolean abstract) { GUnixSocket *client = NULL; gchar buffer[1024]; gsize n; GIOChannel *ioclient = NULL; GIOError e; g_assert(path != NULL); /* Create the server */ if (abstract) server = gnet_unix_socket_server_new_abstract(path); else server = gnet_unix_socket_server_new(path); g_assert(server != NULL); while ((client = gnet_unix_socket_server_accept(server)) != NULL) { ioclient = gnet_unix_socket_get_io_channel(client); g_assert(ioclient != NULL); while ((e = gnet_io_channel_readline(ioclient, buffer, sizeof(buffer), &n)) == G_IO_ERROR_NONE && (n > 0)) { e = gnet_io_channel_writen(ioclient, buffer, n, &n); if (e != G_IO_ERROR_NONE) break; if (fwrite(buffer, n, 1, stdout) != 1) { fprintf (stderr, "Error: fwrite to stdout failed: %s\n", g_strerror (errno)); } } if (e != G_IO_ERROR_NONE) fprintf(stderr, "\nRecieved error %d (closing socket).\n", e); gnet_unix_socket_delete (client); } }
int main(int argc, char** argv) { gchar* hostname; gint port; GInetAddr* addr; GTcpSocket* socket; GIOChannel* iochannel; GIOError error = G_IO_ERROR_NONE; gchar buffer[1024]; gsize n; gnet_init (); /* Parse args */ if (argc != 3) { g_print ("usage: %s <server> <port>\n", argv[0]); exit(EXIT_FAILURE); } hostname = argv[1]; port = atoi(argv[2]); /* Create the address */ addr = gnet_inetaddr_new (hostname, port); if (!addr) { fprintf (stderr, "Error: Name lookup for %s failed\n", hostname); exit (EXIT_FAILURE); } /* Create the socket */ socket = gnet_tcp_socket_new (addr); gnet_inetaddr_delete (addr); if (!socket) { fprintf (stderr, "Error: Could not connect to %s:%d\n", hostname, port); exit (EXIT_FAILURE); } #if 0 { gchar* cname; /* Print local address */ addr = gnet_tcp_socket_get_local_inetaddr (socket); g_assert (addr); cname = gnet_inetaddr_get_canonical_name (addr); g_assert (cname); g_print ("Local address: %s:%d\n", cname, gnet_inetaddr_get_port(addr)); g_free (cname); gnet_inetaddr_delete (addr); /* Print remote address */ addr = gnet_tcp_socket_get_remote_inetaddr (socket); g_assert (addr); cname = gnet_inetaddr_get_canonical_name (addr); g_assert (cname); g_print ("Remote address: %s:%d\n", cname, gnet_inetaddr_get_port(addr)); g_free (cname); gnet_inetaddr_delete (addr); } #endif /* Get the IOChannel */ iochannel = gnet_tcp_socket_get_io_channel (socket); g_assert (iochannel != NULL); while (fgets(buffer, sizeof(buffer), stdin) != 0) { n = strlen(buffer); error = gnet_io_channel_writen (iochannel, buffer, n, &n); if (error != G_IO_ERROR_NONE) break; error = gnet_io_channel_readn (iochannel, buffer, n, &n); if (error != G_IO_ERROR_NONE) break; if (fwrite(buffer, n, 1, stdout) != 1) { fprintf (stderr, "Error: fwrite to stdout failed: %s\n", g_strerror (errno)); } } if (error != G_IO_ERROR_NONE) fprintf (stderr, "Error: IO error (%d)\n", error); gnet_tcp_socket_delete (socket); return 0; }
gpointer sim_connect_send_alarm(gpointer data) { int i; if (!config) { if (data) { config = (SimConfig*) data; } } SimEvent* event = NULL; GTcpSocket* socket = NULL; GIOChannel* iochannel = NULL; GIOError error; GIOCondition conds; gchar *buffer = NULL; gchar *aux = NULL; gsize n; GList *notifies = NULL; gint risk; gchar *ip_src = NULL; gchar *ip_dst = NULL; //gchar time[TIMEBUF_SIZE]; gchar *timestamp; gchar * aux_time; //timestamp = time; gchar *hostname; gint port; GInetAddr* addr = NULL; hostname = g_strdup(config->framework.host); port = config->framework.port; gint iter = 0; void* old_action; for (;;) //Pop events for ever { GString *st; int inx = 0; event = (SimEvent*) sim_container_pop_ar_event(ossim.container); if (!event) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s: No event", __FUNCTION__); continue; } base64_param base64_params[N_TEXT_FIELDS]; for (i = 0; i < N_TEXT_FIELDS; i++) { if (event->textfields[i] != NULL) { base64_params[i].key = g_strdup(sim_text_field_get_name(i)); base64_params[i].base64data = g_strdup(event->textfields[i]); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s:%d %s=\"%s\"", __FILE__, __LINE__, sim_text_field_get_name(i), event->textfields[i]); } else { base64_params[i].key = '\0'; base64_params[i].base64data = '\0'; } } // Send max risk // i.e., to avoid risk=0 when destination is 0.0.0.0 if (event->risk_a > event->risk_c) { risk = event->risk_a; } else { risk = event->risk_c; } /* String to be sent */ if (event->time_str) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: event->time_str %s", event->time_str); aux_time = g_strdup(event->time_str); timestamp = aux_time; } if (event->time) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: event->time %d", event->time); timestamp = g_new0(gchar, 26); strftime (timestamp, TIMEBUF_SIZE, "%Y-%m-%d %H:%M:%S", gmtime ((time_t *) &event->time)); } if (event->src_ia) { ip_src = gnet_inetaddr_get_canonical_name(event->src_ia); } else { ip_src = g_strdup_printf("0.0.0.0"); } if (event->dst_ia) { ip_dst = gnet_inetaddr_get_canonical_name(event->dst_ia); } else { ip_dst = g_strdup_printf("0.0.0.0"); } //FIXME? In a future, Policy will substitute this and this won't be neccesary. Also is needed to check //if this funcionality is really interesting // if (event->policy) { aux = g_strdup_printf( "event date=\"%s\" plugin_id=\"%d\" plugin_sid=\"%d\" risk=\"%d\" priority=\"%d\" reliability=\"%d\" event_id=\"%d\" backlog_id=\"%d\" src_ip=\"%s\" src_port=\"%d\" dst_ip=\"%s\" dst_port=\"%d\" protocol=\"%d\" sensor=\"%s\" actions=\"%d\" policy_id=\"%d\"", timestamp, event->plugin_id, event->plugin_sid, risk, event->priority, event->reliability, event->id, event->backlog_id, ip_src, event->src_port, ip_dst, event->dst_port, event->protocol, event->sensor, sim_policy_get_has_actions(event->policy), sim_policy_get_id( event->policy)); } else { //If there aren't any policy associated, the policy and the action number will be 0 aux = g_strdup_printf( "event date=\"%s\" plugin_id=\"%d\" plugin_sid=\"%d\" risk=\"%d\" priority=\"%d\" reliability=\"%d\" event_id=\"%d\" backlog_id=\"%d\" src_ip=\"%s\" src_port=\"%d\" dst_ip=\"%s\" dst_port=\"%d\" protocol=\"%d\" sensor=\"%s\" actions=\"%d\" policy_id=\"%d\"", timestamp, event->plugin_id, event->plugin_sid, risk, event->priority, event->reliability, event->id, event->backlog_id, ip_src, event->src_port, ip_dst, event->dst_port, event->protocol, event->sensor, 0, 0); } g_free(ip_src); g_free(ip_dst); g_free(timestamp); st = g_string_new(aux); for (inx = 0; inx < G_N_ELEMENTS(base64_params); inx++) { if (base64_params[inx].base64data) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s: %u:%s %p", __FUNCTION__, inx, base64_params[inx].base64data, base64_params[inx].base64data); g_string_append_printf( st, " %s=\"%s\"", base64_params[inx].key, base64_params[inx].base64data != NULL ? base64_params[inx].base64data : ""); g_free(base64_params[inx].base64data); /* we dont't need the data, anymore, so free it*/ } }//end for nelements g_string_append(st, "\n"); buffer = g_string_free(st, FALSE); if (!buffer) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: message error"); g_free(aux); continue; } g_free(aux); aux = NULL; //old way was creating a new socket and giochannel for each alarm. //now a persistent giochannel is used. //iochannel = gnet_tcp_socket_get_io_channel (socket); if (iochannel) { conds = g_io_channel_get_buffer_condition(iochannel); } if (!iochannel || sigpipe_received || (conds & G_IO_HUP) || (conds & G_IO_ERR)) { //Loop to get a connection do { if (sigpipe_received) { if (socket) { gnet_tcp_socket_delete(socket); } sigpipe_received = FALSE; iochannel = FALSE; } // if not, create socket and iochannel from config and store to get a persistent connection. g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: invalid iochannel.(%d)", iter); g_log( G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: trying to create a new iochannel.(%d)", iter); if (!hostname) { //FIXME: may be that this host hasn't got any frameworkd. If the event is forwarded to other server, it will be sended to the //other server framework (supposed it has a defined one). g_log( G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: Hostname error, reconnecting in 3secs (%d)", iter); hostname = g_strdup(config->framework.host); sleep(3); continue; } if (addr) { g_free(addr); } addr = gnet_inetaddr_new_nonblock(hostname, port); if (!addr) { g_log( G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: Error creating the address, trying in 3secs(%d)", iter); sleep(3); continue; } socket = gnet_tcp_socket_new(addr); if (!socket) { g_log( G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: Error creating socket(1), reconnecting in 3 secs..(%d)", iter); iochannel = NULL; socket = NULL; sleep(3); continue; } else { iochannel = gnet_tcp_socket_get_io_channel(socket); if (!iochannel) { g_log( G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: Error creating iochannel, reconnecting in 3 secs..(%d)", iter); if (socket) { gnet_tcp_socket_delete(socket); } socket = NULL; iochannel = NULL; sleep(3); continue; } else { sigpipe_received = FALSE; g_log( G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: new iochannel created. Returning %x (%d)", iochannel, iter); } } iter++; } while (!iochannel); } //g_assert (iochannel != NULL); n = strlen(buffer); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: Message to send: %s, (len=%d)", buffer, n); //signals actually not used // old_action=signal(SIGPIPE, pipe_handler); sim_util_block_signal(SIGPIPE); error = gnet_io_channel_writen(iochannel, buffer, n, &n); sim_util_unblock_signal(SIGPIPE); //error = gnet_io_channel_readn (iochannel, buffer, n, &n); //fwrite(buffer, n, 1, stdout); if (error != G_IO_ERROR_NONE) { //back to the queue so we dont loose the action/response g_object_ref(event); sim_container_push_ar_event(ossim.container, event); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: message could not be sent.. reseting"); /* if(buffer) g_free (buffer); g_free (aux); */ gnet_tcp_socket_delete(socket); iochannel = NULL; } else g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "sim_connect_send_alarm: message sent succesfully: %s", buffer); //Cose conn if (buffer) g_free(buffer); if (aux) g_free(aux); buffer = NULL; aux = NULL; //gnet_tcp_socket_delete (socket); //iochannel=NULL; if (event) g_object_unref(event); } }
int gnet_xmlrpc_client_call(GNetXmlRpcClient *_client, const gchar *method, const gchar *param, // output gchar **reply) { GNetXmlRpcClientPrivate *client = (GNetXmlRpcClientPrivate*)_client; gchar *xmlrpc_message = build_xmlrpc_message(method, param); gchar *msg_start, *msg_end; gchar *p; GString *xmlrpc_string = g_string_new(""); GString *reply_string = g_string_new(""); gsize n; gchar buffer[1024]; gint error; n = strlen(xmlrpc_message); // printf("Writing...\n"); fflush(stdout); error = gnet_io_channel_writen (client->client.gnet_channel, xmlrpc_message, n, &n); // printf("error = %d\n", error); fflush(stdout); if (error != G_IO_ERROR_NONE) return -1; // fprintf(stderr, "entering while loop\n"); while (gnet_io_channel_readline (client->client.gnet_channel, buffer, sizeof(buffer), &n) == G_IO_ERROR_NONE) { if (client->in_header) { // Check for a valid response if (client->header_lineno == 0) { // If we don't have HTTP we've got a problem if (g_strstr_len(buffer, 5, "HTTP") == NULL) { return -1; } } else if (n==1) { client->in_header = FALSE; } else { // Look for the content-length string case independant. char *p; // Lower case buf p = buffer; while(*p) { *p = g_ascii_tolower(*p); p++; } // Search for string if ((p = g_strstr_len(buffer, n-1, "content-length:")) != NULL) { p += strlen("Content-length:"); client->message_size = atoi(p); } } client->header_lineno++; } // If we are not in the header then append the line to the xmlrpc string. else { g_string_append_len(xmlrpc_string, buffer, n-1); g_string_append_c(xmlrpc_string, '\n'); } // Check if we are finished if (xmlrpc_string->len && xmlrpc_string->len >= client->message_size) { // Quit and reset parsing for next message client->in_header = 1; client->header_lineno = 0; break; } } // Extract the response. Should be exchanged to some more robust // XML parsing. msg_start = g_strstr_len(xmlrpc_string->str, xmlrpc_string->len, "<value><string>"); if (msg_start == NULL) return -1; msg_start += strlen("<value><string>"); msg_end = g_strrstr(msg_start, "</string></value>"); // Decode the response p = msg_start; while(*p != '<') { gchar c = *p++; if (c == '&') { if (g_strstr_len(p, 4, "amp;") == p) { g_string_append_c(reply_string, '&'); p+= 4; } else if (g_strstr_len(p, 3, "lt;") == p) { g_string_append_c(reply_string, '<'); p+= 3; } else if (g_strstr_len(p, 3, "gt;") == p) { g_string_append_c(reply_string, '>'); p+= 3; } else { // Don't know what to do. Just add the ampersand.. g_string_append_c(reply_string, '&'); } } else g_string_append_c(reply_string, c); } *reply = reply_string->str; g_string_free(reply_string, FALSE); g_string_free(xmlrpc_string, TRUE); return 0; }