/*---------------------------------------------------------------------------*/
PROCESS_THREAD(servreg_hack_process, ev, data)
{
  static struct etimer periodic;
  static struct uip_udp_conn *outconn, *inconn;
  PROCESS_BEGIN();

  /* Create outbound UDP connection. */
  outconn = udp_broadcast_new(UIP_HTONS(UDP_PORT), NULL);
  udp_bind(outconn, UIP_HTONS(UDP_PORT));

  /* Create inbound UDP connection. */
  inconn = udp_new(NULL, UIP_HTONS(UDP_PORT), NULL);
  udp_bind(inconn, UIP_HTONS(UDP_PORT));

  etimer_set(&periodic, PERIOD_TIME);
  etimer_set(&sendtimer, random_rand() % (PERIOD_TIME));
  while(1) {
    PROCESS_WAIT_EVENT();
    if(ev == PROCESS_EVENT_TIMER && data == &periodic) {
      etimer_reset(&periodic);
      etimer_set(&sendtimer, random_rand() % (PERIOD_TIME));
    } else if(ev == PROCESS_EVENT_TIMER && data == &sendtimer) {
      send_udp_packet(outconn);
    } else if(ev == tcpip_event) {
      parse_incoming_packet(uip_appdata, uip_datalen());
    }
  }
  PROCESS_END();
}
Beispiel #2
0
void *worker_thread_entry(void *void_worker_config) {
	WorkerConfig *worker_config = (WorkerConfig *)void_worker_config;
	StanzaQueue *queue = &config.reader_thread.queue;
	int allocated_buffer_size = config.worker.buffer;

	StanzaEntry *stanza_entry;
	BufferPtr stanza_entry_buffer;
	LocalBufferStorage lbs;
	RouterChunk router_chunk;

	router_chunk.send.proc = send_packet;
	router_chunk.send.data = &lbs;
	router_chunk.hostname.data = config.component.hostname;
	router_chunk.hostname.size = strlen(config.component.hostname);

	lbs.buffer.data = malloc(allocated_buffer_size);
	lbs.buffer.end = lbs.buffer.data + allocated_buffer_size;
	lbs.ringbuffer = &config.writer_thread.ringbuffer;
	lbs.packet = &router_chunk.egress;

	LINFO("started");
	sighelper_sigblockall(0);

	while (worker_config->enabled) {
		if (allocated_buffer_size != config.worker.buffer) {
			lbs.buffer.data = realloc(lbs.buffer.data,
					allocated_buffer_size = config.worker.buffer);
			lbs.buffer.end = lbs.buffer.data + allocated_buffer_size;
		}

		stanza_entry = queue_pop_data(queue);
		if (stanza_entry->buffer && stanza_entry->data_size) {
			stanza_entry_buffer.data = stanza_entry->buffer;
			stanza_entry_buffer.end = stanza_entry->buffer + stanza_entry->data_size;

			if (parse_incoming_packet(&stanza_entry_buffer, &router_chunk.ingress)) {
				router_process(&router_chunk);
			}
		}

		queue_push_free(queue, stanza_entry);
	}

	LINFO("terminated");
	pthread_exit(0);
}
Beispiel #3
0
int main(int argc, char** argv) {
  int           ret, n, sinlen;
  int           data_len, datagramme_len;
  struct        sockaddr_in bindPort, sin;
  unsigned char packet[BUFSIZE];
  unsigned char *data, *datagramme;
  
  progname = argv[0];
  
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <port number>\n", progname);
    exit (1); 
  }
    
  fprintf(stderr, "%s:Creating a socket\n", progname);
  // Open a divert socket
  fd = socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT);
  
  if (fd == -1) {
    fprintf(stderr, "%s:We could not open a divert socket\n", progname);
    exit (1);
  }
  
  bindPort.sin_family = AF_INET;
  bindPort.sin_port = htons(atol(argv[1]));
  bindPort.sin_addr.s_addr = 0;
  
  fprintf(stderr, "%s:Binding a socket\n", progname);
  ret = bind(fd, (struct sockaddr *)&bindPort, sizeof(struct sockaddr_in));
  
  if (ret != 0) {
    close(fd);
    fprintf(stderr, "%s: Error bind(): %s", progname, strerror(ret));
    exit (2);
  }
    
  sinlen = sizeof(struct sockaddr_in);
  
  set_server_ip("96.56.208.212");
  initialize_client_ip();
  
  while(1) {
    n = recvfrom(fd, packet, BUFSIZE, 0, (struct sockaddr *)&sin, (socklen_t *)&sinlen);
    
    // Identify the packet components. Ignore the tcp and udp headers.
    data        = packet + sizeof(struct ip) + sizeof(struct udphdr);
    datagramme  = data + 8;
    
    // Store their respective sizes
    data_len        = n - sizeof(struct ip) - sizeof(struct udphdr);
    datagramme_len  = data_len- 8;
    
    if (data_len > 4) // Ensure that the packet is not an acknowledgment
    {
      collect_client_ip_from_packet(packet, &sin);
      collect_client_udp_port_from_packet(packet, &sin);
            
      if (sin.sin_addr.s_addr == 0)  // Outgoing packet
      {
        // write(0, ">", 1);
        #ifdef DEBUG
        printf("____ OUTGOING ____\n");
        debug_show_ip_header(packet);
        printf("------------------\n");
        #endif
        if (parse_outgoing_packet(data, data_len) == 1)
          craft_outgoing_packet(data, data_len);
      }
      else                            // Incoming packet
      {
        // write(0, "<", 1);
        #ifdef DEBUG
        printf("____ INCOMING ____\n");
        debug_show_ip_header(packet);
        printf("------------------\n");
        #endif
        if (parse_incoming_packet(data, data_len) == 1)
          craft_incoming_packet(data, data_len);
      }
    }
    else
    {
      reinject_paket(packet, n, &sin, sinlen);
    }
  }
}