Example #1
0
void gnet_xmlrpc_client_delete(GNetXmlRpcClient *_client)
{
  GNetXmlRpcClientPrivate *client = (GNetXmlRpcClientPrivate*)_client;

  g_string_free(client->xmlrpc_string, TRUE);
  gnet_tcp_socket_delete (client->socket);
  g_free(client);
}
Example #2
0
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;
}
Example #3
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);
    }

}
Example #4
0
void ntl_net_free(ntl_Net* n)
{
    gnet_tcp_socket_delete(n->sock);
    g_free(n);
}
Example #5
0
gboolean network_receive(GIOChannel *source, GIOCondition condition, gpointer data)
{
	/* Primarily, we'll only be reading in a small amount of info */
	if(log_level == 0)
	{
		if((condition&G_IO_HUP)==G_IO_HUP)
		{
			return FALSE;
		}
		if((condition&G_IO_IN) == G_IO_IN)
		{
			GIOError error = G_IO_ERROR_NONE;
			char data = 0;
			unsigned int offset = 0;
			char message[1024] = { '\0' };
			gsize n = 1;
			while(data!='\n')
			{
				error = gnet_io_channel_readn(msn_channel, &data, n, &n); 
				if(n == 0)
				{
					return FALSE;
				}
				if (error != G_IO_ERROR_NONE) 
				{
					error_dialog("Error receiving network data!");
					return FALSE;
				}
				message[offset++] = data;
			}
			printf("Received: %s\n",message);
			/*
				TODO: action to take
			*/
			if(strncmp(message, "VER",3)==0)
			{
				/* Got a response from VER, time to send CVR */
				char *tmp_message = g_strdup_printf("CVR 2 0x0c0a winnt 5.1 i386 MSNMSGR 8.0.0792 msmsgs %s\r\n",uemail);
				net_send(tmp_message);
				g_free(tmp_message);
			}
			else if(strncmp(message, "CVR", 3) == 0)
			{
				/* Got a response from CVR, time to send USR */
				char *tmp_message = g_strdup_printf("USR 3 TWN I %s\r\n",uemail);
				net_send(tmp_message);
				g_free(tmp_message);
			}
			else if(strncmp(message, "XFR", 3) == 0)
			{
				/* The tricky part... */
				log_level = 1;

				/*
					Now we close our socket, and point it at the ip given to us in the XFR message

					Sample specimen

					XFR 3 NS 207.46.108.91:1863 0 65.54.239.210:1863

				*/

				char *ip_start = strstr(message, "NS ");
				/* Skip past the "NS " */
				ip_start+=3;

				/* Find the splitter, " 0 " */
				char *ip_end = strstr(ip_start, " 0 ");
				ip_end[0] = '\0';

				char *ip_port_split = strstr(ip_start, ":");
				ip_port_split[0] = '\0';

				/* Voila */
				printf("IP: %s\n",ip_start);
				gnet_tcp_socket_delete(msn_socket);
				
				GInetAddr* addr;
				addr = gnet_inetaddr_new(ip_start, 1863);
				if(addr)
				{
					msn_socket = gnet_tcp_socket_new(addr);
					gnet_inetaddr_delete(addr);
				}
				else
				{
					error_dialog("Failed to resolve messenger.msn.com");
					return;
				}
	
				msn_channel = gnet_tcp_socket_get_io_channel(msn_socket);
				g_io_channel_set_buffer_size(msn_channel,128);
				g_io_channel_set_buffered(msn_channel,1);
				g_io_channel_set_encoding(msn_channel, NULL, NULL);
				g_io_add_watch(msn_channel,G_IO_IN|G_IO_HUP|G_IO_ERR,(GIOFunc)network_receive,NULL);
				printf("Transfered\n");
				net_send("VER 0 MSNP8 MYPROTOCOL CVR0 \r\n");
				return FALSE;
			}
		}
	}
	/* When the primary login stage (jumping connection) is done log_level = 1 */
	else if(log_level == 1)
	{
		if((condition&G_IO_HUP)==G_IO_HUP)
		{
			return FALSE;
		}
		if((condition&G_IO_IN) == G_IO_IN)
		{
			GIOError error = G_IO_ERROR_NONE;
			char data = 0;
			unsigned int offset = 0;
			char message[1024] = { '\0' };
			gsize n = 1;
			while(data!='\n')
			{
				error = gnet_io_channel_readn(msn_channel, &data, n, &n); 
				if(n == 0)
				{
					return FALSE;
				}
				if (error != G_IO_ERROR_NONE) 
				{
					error_dialog("Error receiving network data!");
					return FALSE;
				}
				message[offset++] = data;
			}
			printf("Received: %s\n",message);
			/*
				TODO: action to take
			*/
			if(strncmp(message, "VER",3)==0)
			{
				/* Got a response from VER, time to send CVR */
				char *tmp_message = g_strdup_printf("CVR 2 0x0c0a winnt 5.1 i386 MSNMSGR 8.0.0792 msmsgs %s\r\n",uemail);
				net_send(tmp_message);
				g_free(tmp_message);
			}
			else if(strncmp(message, "CVR", 3) == 0)
			{
				/* Got a response from CVR, time to send USR */
				char *tmp_message = g_strdup_printf("USR 3 TWN I %s\r\n",uemail);
				net_send(tmp_message);
				g_free(tmp_message);
			}
			else if(strncmp(message, "USR 3", 5) == 0)
			{
				/* 
					REALLY Tricky part now. OpenSSL - woohoo :( 

					Basically, the MSN protocol uses SSL to autheticate clients; so what happens now is, the MSN server sends us a 'challenge'
					for the SSL login page that we must send to it. So we need to extract that data, and send it to MSN to grab our golden 'ticket'
					which is used in the final login period.
				*/
				
				/* Example: USR 3 TWN S ct=1202475836,rver=5.0.3270.0,wp=FS_40SEC_0_COMPACT,lc=1033,id=507,ru=http:%2F%2Fmessenger.msn.com,tw=0,kpp=1,kv=4,ver=2.1.6000.1,rn=1lgjBfIL,tpf=b0735e3a873dfb5e75054465196398e0 */
				char *ticket = strstr(message, " ct=")+1;
				printf("Ticket: %s\n",ticket);
				
				char *return_ticket = net_ssl_auth(ticket);
				printf("Got ticket: %s\n",return_ticket);
				char *tmp_message = g_strdup_printf("USR 4 TWN S %s\r\n", return_ticket);
				net_send(tmp_message);
				g_free(tmp_message);
			}
			else if(strncmp(message, "USR 4", 5) == 0)
			{
				/* Woot I'm in! - We just got our email (useless) AND OUR NICKNAME! DAMN RIGHT BITCHES! */

				char *nick = split(message, ' ', 4);
				char *email = split(message, ' ', 3);
				globals_email = email;
				mwin_change_nick(nick);

				g_free(nick);
				char *tmp_message = g_strdup_printf("SYN 5 0\r\n");
				net_send(tmp_message);
				g_free(tmp_message);
				tmp_message = g_strdup_printf("CHG %d NLN 0\r\n",msg_num++);
				net_send(tmp_message);
				g_free(tmp_message);
				
			}
			else if(strncmp(message, "CHL", 3) == 0)
			{
				/* Got a challenge from MSN, and who am I to refuse? */
				char *challenge = message + 6;
				char *end = strstr(message, "\r\n");
				end[0] = '\0';
				char *chal_string = g_strdup_printf("%s%s",challenge,"Q1P7W2E4J9R8U3S5");
				GMD5* chal_md5 = gnet_md5_new(chal_string,strlen(chal_string));
				g_free(chal_string);
				
				gchar *hash = gnet_md5_get_string(chal_md5);
				gnet_md5_delete(chal_md5);
				
				printf("Challenge reply: %s\n",hash);
				
				char *tmp_message = g_strdup_printf("QRY %d [email protected] 32\r\n%s",msg_num++, hash);
				net_send(tmp_message);
				g_free(tmp_message);	
			}
			else if(strncmp(message,"RNG",3) == 0)
			{
				/* Got an incoming message! */
				printf("Got an incoming message, someone hast double clicked upon us!\n");
				printf("MESSAGE: %s\n",message);
				char *phost = split(message,' ', 2);
				char *host = split(phost,':',0);
				free(phost);
				printf("Host: %s\n",host);
				
				char *session = split(message,' ', 1);
				printf("Session: %s\n",session);
				
				char *auth_str = split(message,' ',4);
				char *email = split(message, ' ', 5);
				char *nick = split(message, ' ', 6);
				nick[strlen(nick)-2]='\0';
			
				printf("Auth: %s\n",auth_str);
				printf("Email: %s\n",email);
				printf("Nick: %s\n", nick);

				conversation_new(host, session, auth_str, email, nick, uemail);
			}
			else if(strncmp(message,"MSG",3) == 0)
			{
				// Got a message, most likely it's gonna be from Hotmail
				// Important thing is, MSG's have a payload size!
				char *nick = split(message,' ',2);
				char *len = split(message,' ',3);
				len[strlen(len)-2] = '\0';
				int length = atoi(len);
				printf("Inbound message length: %d\n",length);

				error = G_IO_ERROR_NONE;
				char incoming_msg[length+1];
				memset(incoming_msg, '\0',length+1);
				gsize n = length;
				error = gnet_io_channel_readn(msn_channel, incoming_msg, n, &n);
				if(error != G_IO_ERROR_NONE)
				{
				   fprintf(stderr, "Error occurred reading message\n");
				   return FALSE;
				}
				printf("Message: %s\n",incoming_msg);
			}
		}
	}
	return TRUE;
}