Exemplo n.º 1
0
void send_keys(struct network_status *net_stat, struct keypair *keypair)
  /*@ requires [?f0]world(?pub, ?key_clsfy) &*&
               keypair(keypair, ?attacker_id, ?id, ?info, pub) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(attacker_id, ?count1) &*&
               true == bad(attacker_id); @*/
  /*@ ensures  [f0]world(pub, key_clsfy) &*&
               keypair(keypair, attacker_id, id, info, pub) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(attacker_id, ?count2); @*/
{
  //@ close key_request(attacker_id, 0);
  struct item *key_sym = create_symmetric_key();
  struct item *key_priv = keypair_get_private_key(keypair);
  struct item *key_pub = keypair_get_public_key(keypair);
  //@ assert item(key_sym, ?k_sym, pub);
  //@ assert item(key_pub, ?k_pub, pub);
  //@ assert item(key_priv, ?k_priv, pub);
  //@ open proof_obligations(pub);
  //@ assert is_public_symmetric_key(?proof_sym, pub);
  //@ assert is_public_public_key(?proof_pub, pub);
  //@ assert is_public_private_key(?proof_priv, pub);
  //@ proof_sym(k_sym);
  //@ proof_pub(k_pub);
  //@ proof_priv(k_priv);
  //@ close proof_obligations(pub);
  network_send(net_stat, key_sym);
  network_send(net_stat, key_pub);
  network_send(net_stat, key_priv);
  item_free(key_sym);
  item_free(key_pub);
  item_free(key_priv);
}
Exemplo n.º 2
0
void send_pair_decomposed(struct network_status *net_stat)
  /*@ requires [?f0]world(?pub, ?key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(?principal, ?count1) &*&
               true == bad(principal); @*/
  /*@ ensures  [f0]world(pub, key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(principal, ?count2); @*/
{
  struct item *pair = network_receive(net_stat);
  //@ assert item(pair, ?p, pub);
  if (is_pair(pair))
  {
    struct item *first = pair_get_first(pair);
    struct item *second = pair_get_second(pair);
    //@ open proof_obligations(pub);
    //@ assert is_public_pair_decompose(?proof1, pub);
    //@ assert is_public_collision(?proof2, pub);
    //@ proof1(p);
    //@ assert item(first, ?f, pub);
    //@ if (col) proof2(f);
    //@ assert item(second, ?s, pub);
    //@ if (col) proof2(s);
    //@ close proof_obligations(pub);
    network_send(net_stat, first);
    network_send(net_stat, second);
    item_free(first);
    item_free(second);
  }
  item_free(pair);
}
Exemplo n.º 3
0
/* ***************************************************
 * Function: send_syn_ack
 * ***************************************************
 * Passive node calls this to send SYN_ACK and wait for
 * the final ACK packet. It tries a total of 6 times before
 * giving up.
 */
static bool send_syn_ack(mysocket_t sd, context_t *ctx, struct tcphdr* syn_packet)
{
	struct tcphdr* ack_packet = (struct tcphdr *)calloc(1,(TH_MAX_OFFSET*sizeof(uint32_t))+STCP_MSS);
	unsigned int event;
	bool success = false;
	int num_tries = 1;
	struct timeval tstart;
	gettimeofday(&tstart, NULL); /*Start the timer */
	network_send(sd, syn_packet, (TH_MIN_OFFSET*sizeof(uint32_t)));
	while((num_tries < MAX_RT_TRIES) && !success){
		event = wait_for_event(sd, NETWORK_DATA, ctx, tstart);
		if (event & NETWORK_DATA) {
			network_recv(sd, ack_packet);
			our_dprintf("Passive (ACK waiting): Ack: %d, Seq:%d, Next Seq:%d, Last Ack Sent: %d\n", 
						 ack_packet->th_ack, ack_packet->th_seq, ctx->nxt_seq_num, ctx->last_ack_sent);
			success =  ((ack_packet->th_flags == TH_ACK) &&
						(ack_packet->th_ack == ctx->nxt_seq_num));
		}
		else if (event==TIMEOUT){
			our_dprintf("Passive Final INIT ACK not Received TIMEOUT!\n");	
			num_tries+=1;
			gettimeofday(&tstart, NULL);
			our_dprintf("PASSIVE - SEND SYN_ACK Packet with seq: %d and ack: %d \n",
						 syn_packet->th_seq, syn_packet->th_ack);
			network_send(sd, syn_packet, (TH_MIN_OFFSET*sizeof(uint32_t)));
		}
	}
	if (success){
		our_dprintf("PASSIVE - RCVD Last Ack Packet has seq: %d and ack: %d \n",
					 ack_packet->th_seq, ack_packet->th_ack);
   		ctx->recv_win = MIN(syn_packet->th_win, CWIN);
	}	
	free(ack_packet);
	return success;
}
Exemplo n.º 4
0
int ps4link_request_read(void *packet) 
{
	struct { unsigned int number; unsigned short length; int fd; int size; } PACKED *request = packet;
	int result = -1, size = -1; 
	char buffer[65536], *bigbuffer;

	// If a big read is requested...
	if (ntohl(request->size) > sizeof(buffer)) 
	{
		// Allocate the bigbuffer.
		bigbuffer = malloc(ntohl(request->size));

		// Perform the request.
		result = size = read(ntohl(request->fd), bigbuffer, ntohl(request->size));

		// Send the response.
		ps4link_response_read(result, size);

		// Send the response data.
		network_send(request_socket, bigbuffer, size);

		// Free the bigbuffer.
		free(bigbuffer);

		// Else, a normal read is requested...
	} 
	else 
	{

		// Perform the request.
		size = read(ntohl(request->fd), buffer, ntohl(request->size));
		//int error=errno ;
		//debugNetPrintf(DEBUG,"Error reading file: %s %s\n", strerror( error ),buffer);
		result=size;
		debugNetPrintf(DEBUG,"read %d bytes of file descritor %d\n",result,ntohl(request->fd));

		// Send the response.
		ps4link_response_read(result, size);

		// Send the response data.
		network_send(request_socket, buffer, size);

	}

	// End function.
	return 0;

}
Exemplo n.º 5
0
void TCPIP_GotNewPacket(void)
{
	uip_input();																// Call the TCP/IP stack with the new packet
																					
	if (uip_len > 0)															// If the above function invocation resulted in data that should be sent out
	  network_send(IP);														// on the network, the global variable uip_len is set to a value > 0.
}
Exemplo n.º 6
0
/* ***************************************************
 * Function: handle_app_data
 * ***************************************************
 * The application has new data to be sent out.  First make sure that
 * there is some space in the window.  Get maximum amount of data from 
 * the application, added it to the sender's buffer, and send it across
 * the network.
 */
static void handle_app_data(mysocket_t sd, context_t *ctx)
{
   size_t bytes_in_transit = ctx->nxt_seq_num - ctx->seq_base;
   size_t win_space = ctx->recv_win - bytes_in_transit;
   if (win_space>0){
       char *pkt = (char *)calloc(1,(TH_MIN_OFFSET*sizeof(uint32_t)) + STCP_MSS);
       struct tcphdr* hdr = (struct tcphdr*)pkt;
       hdr->th_off = TH_MIN_OFFSET;
       hdr->th_seq = ctx->nxt_seq_num;
       hdr->th_flags = 0;
       hdr->th_win = RWIN;
       /* Pick the minimum between space in the window and MSS */
       size_t data_max = MIN(STCP_MSS, win_space);	           
       size_t len = stcp_app_recv(sd, pkt + TCP_DATA_START(hdr), data_max); 	           
       ctx->nxt_seq_num += len; /*add len to obtain nxt_seq_num to be used */
       add_to_send_buffer(ctx,  pkt, (TH_MIN_OFFSET*sizeof(uint32_t)) + len);
 	   our_dprintf("Sending packet with seq: %d of size: %d\n", 
 	   				hdr->th_seq,(TH_MIN_OFFSET*sizeof(uint32_t)) + len);
  	   network_send(sd, pkt, (TH_MIN_OFFSET*sizeof(uint32_t)) + len);
       free(pkt);
   }
   else {
   		our_dprintf(".");           	
   }	
}
Exemplo n.º 7
0
void udp_send(uint16_t length) {
    
    uint16_t tmp;
    
    // IP protocol packet length
    tmp = IP_LEN_HEADER + UDP_LEN_HEADER + length;
    buffer_out[IP_PTR_LENGTH_H] = tmp >> 8;
    buffer_out[IP_PTR_LENGTH_L] = tmp & 0xFF;
    
    // UDP protocol packet length
    tmp = UDP_LEN_HEADER + length;
    buffer_out[UDP_PTR_LENGTH_H] = tmp >> 8;
    buffer_out[UDP_PTR_LENGTH_L] = tmp & 0xFF;
    
    // Calculate checksum IP header
    tmp = checksum(&buffer_out[IP_PTR], IP_LEN_HEADER, CHK_IP);
    buffer_out[IP_PTR_CHECKSUM_H] = tmp >> 8;
    buffer_out[IP_PTR_CHECKSUM_L] = tmp & 0xFF;
    
    // Calculate checksum UDP data
    tmp = checksum(&buffer_out[IP_PTR_SRC], 16 + length, CHK_UDP);
    buffer_out[UDP_PTR_CHECKSUM_H] = tmp >> 8;
    buffer_out[UDP_PTR_CHECKSUM_L] = tmp & 0xFF;
    
#ifdef UTILS_WERKTI_MORE
    // Update werkti udp out
    werkti_udp_out += ETH_LEN_HEADER + IP_LEN_HEADER + UDP_LEN_HEADER + length;
#endif // UTILS_WERKTI_MORE
    
    // Send packet to chip
    network_send(ETH_LEN_HEADER + IP_LEN_HEADER + UDP_LEN_HEADER + length);
}
Exemplo n.º 8
0
/* ***************************************************
 * Function: handle_timeout
 * ***************************************************
 * This function is called whenever there is a timeout. Unless we
 * are in the TIME_WAIT state, I retransmit all the packets starting
 * from the beginning of the window.  
 * The RTO is also doubled when there is a retransmission as described
 * in K&R. 
 */
static void handle_timeout(mysocket_t sd, context_t *ctx)
{	
	if (ctx->connection_state==CSTATE_TIME_WAIT){
		ctx->connection_state = CSTATE_CLOSED;
		ctx->done = true;
		return;
	}
	our_dprintf("TIMEOUT waiting for seq:%d\n", ctx->seq_base);	
	assert(ctx->sbuffer.num_entries>0 || !DEBUG);
	assert(ctx->sbuffer.start->next!=NULL || !DEBUG);
	struct p_entry *curr = ctx->sbuffer.start->next;
	ctx->rto= MIN(ctx->rto*2, MAX_RTO);
	/* Retransmit all packets */
	while (curr){
			struct tcphdr *hdr = (struct tcphdr*)curr->packet;
			if (curr->num_transmits>=MAX_RT_TRIES){
				our_dprintf("MAX NUMBER OF TRIES REACHED KILLING CONNECTION !!!!\n\n");
				ctx->done = true;
				errno = ETIMEDOUT;
				return;
			}
			hdr->th_ack = ctx->last_ack_sent;
			curr->num_transmits+=1; /* increase counter */
			gettimeofday(&(curr->tstart),NULL);
			network_send(sd, hdr, curr->packet_size);
			curr = curr->next;
			our_dprintf("Retransmitting Seq:%d with Len:%d New RTO:%f\n",
				 hdr->th_seq, ctx->sbuffer.start->next->packet_size, ctx->rto);
	}
}
Exemplo n.º 9
0
void send_data(struct network_status *net_stat)
  /*@ requires [?f0]world(?pub, ?key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(?principal, ?count1) &*&
               true == bad(principal); @*/
  /*@ ensures  [f0]world(pub, key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(principal, ?count2); @*/
{
  int data_size = random_int_();
  if (data_size > MIN_RANDOM_SIZE)
  {
    char* data = malloc((int) data_size);
    if (data == 0) abort_crypto_lib("malloc failed");
    random_buffer_(data, data_size);
    struct item *item = create_data_item(data, data_size);
    //@ assert item(item, ?i, pub) &*& i == data_item(?d);
    free(data);
    //@ open proof_obligations(pub);
    //@ assert is_public_data(?proof, pub);
    //@ proof(i);
    //@ close proof_obligations(pub);
    network_send(net_stat, item);
    item_free(item);
  }
}
Exemplo n.º 10
0
void send_pair_composed(struct network_status *net_stat)
  /*@ requires [?f0]world(?pub, ?key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(?principal, ?count1) &*&
               true == bad(principal); @*/
  /*@ ensures  [f0]world(pub, key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(principal, ?count2); @*/
{
  struct item *first = network_receive(net_stat);
  struct item *second = network_receive(net_stat);
  struct item * pair = create_pair(first, second);
  //@ assert item(first, ?f, pub);
  //@ assert item(second, ?s, pub);
  //@ assert item(pair, pair_item(f, s), pub);

  //@ open proof_obligations(pub);
  //@ assert is_public_pair_compose(?proof, pub);
  //@ proof(f, s);
  //@ close proof_obligations(pub);
  network_send(net_stat, pair);
  item_free(pair);
  item_free(first);
  item_free(second);
}
Exemplo n.º 11
0
void increment_and_send_nonce(struct network_status *net_stat)
  /*@ requires [?f0]world(?pub, ?key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(?principal, ?count1) &*&
               true == bad(principal); @*/
  /*@ ensures  [f0]world(pub, key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(principal, ?count2); @*/
{
  struct item *nonce = network_receive(net_stat);
  //@ assert item(nonce, ?n, pub);
  if (is_nonce(nonce))
  {
    increment_nonce(nonce);
    //@ assert item(nonce, ?n_inc, pub);
    //@ open proof_obligations(pub);
    /*@ if (col)
        {
          assert is_public_collision(?proof, pub);
          proof(n_inc);
        }
        else
        {
          assert is_public_incremented_nonce(?proof, pub);
          proof(n, n_inc);
        }
    @*/
    //@ close proof_obligations(pub);
    network_send(net_stat, nonce);
  }
  item_free(nonce);
}
Exemplo n.º 12
0
void sample_device(FILE *f, FILE *m, int test)
{
	struct buffer b;
	size_t read = 0;
	char *packet = NULL;
	char count[24] = {0}; // Never gonna fill up
	int i = 0;
	size_t sent = 0;

	while (!feof(f)) {
		size_t rc = fread(&b, sizeof(struct buffer), 1, f);
		char* rcc = fgets(&count[0], 23, m);
		missed_count = atoi(&count[0]) - initial_missed;
		if (!test && network_send(b, missed_count, &sent)) {
			fprintf(stderr, "error:  Could not send batch from buffer:  %s\n", strerror(errno));
			break;
		}
		if (debug)
			debug_out(b);
		if (test)
			break;
		outbytes += sent;
		if (kbytes > 0 && outbytes / 1000 > kbytes) {
			fprintf(stdout, "%zu kb requested, %zu bytes sent.\n", kbytes, outbytes);
			break;
		}
	}
}
Exemplo n.º 13
0
/* ***************************************************
 * Function: active_init
 * ***************************************************
 * 	Initiate the active side connection.  Send the SYN packet,
 * wait for the SYN_ACK and then send the final SYN. 
 */
static bool active_init(mysocket_t sd, context_t *ctx)
{
	
	struct tcphdr* syn_packet = (struct tcphdr *)calloc(1,(TH_MAX_OFFSET*sizeof(uint32_t)) + STCP_MSS);	
	if (!sendrcv_syn(sd, ctx, syn_packet)) {
		return false;	/* Timeout waiting for SYN_ACK*/
	}
	
	ctx->connection_state = CSTATE_SYN_SENT;
	our_dprintf("Active - RVCD SYN_ACK Seq: %d, ACK: %d \n", syn_packet->th_seq, syn_packet->th_ack);
	ctx->recv_win = MIN(syn_packet->th_win, CWIN);

	/* Send final ACK ---- Prepare Headers */
	syn_packet->th_ack = syn_packet->th_seq + 1; 
	syn_packet->th_seq = ctx->nxt_seq_num;		 
	syn_packet->th_flags = TH_ACK;
	syn_packet->th_off = TH_MIN_OFFSET;
	syn_packet->th_win = RWIN;
	our_dprintf("Active - Sending Final ACK Seq: %d, ACK: %d \n", syn_packet->th_seq, syn_packet->th_ack);
	
	network_send(sd, syn_packet, (TH_MIN_OFFSET*sizeof(uint32_t)));
	
	/* Update connection state variables */
	ctx->seq_base = ctx->nxt_seq_num;
	ctx->last_ack_sent = syn_packet->th_ack;
	free(syn_packet);
	return true;
}
Exemplo n.º 14
0
void lobby_host_loop() {
	struct proto_join_packet packet;
	static int tick = 0;
	DARNIT_KEYS keys;
	
	keys = d_keys_get();
	d_keys_set(keys);
	
	d_render_tile_blit(config.menu_background, 0, 0, 0);
	
	if(!tick) {
		packet.type = PROTO_TYPE_JOIN;
		packet.player_id = -1;
		strcpy(packet.player_name, config.player_name);
		network_send(config.server.addr, &packet, sizeof(struct proto_join_packet));
		update_player_list(lobby_join.list_players, config.player.player);
	}
	server_loop();
	
	if(keys.select) {
		game_state(GAME_STATE_LOBBY);
	}
	if(keys.start) {
		game_state(GAME_STATE_GAME);
	}
	
	d_text_surface_draw(lobby_join.list_players);
	d_text_surface_draw(lobby_host.start_game);
	tick++;
	tick %= 3;
}
Exemplo n.º 15
0
void app_send(struct item *key, struct item *message)
  /*@ requires [?f0]world(ss_pub) &*&
               item(key, symmetric_key_item(?creator, ?id), ss_pub) &*& 
               item(message, ?msg, ss_pub) &*& [_]ss_pub(msg) &*&
               app_send_event(creator, msg) == true;
  @*/
  /*@ ensures  [f0]world(ss_pub) &*&
               item(key, symmetric_key_item(creator, id), ss_pub) &*&
               item(message, msg, ss_pub);
  @*/
{
    struct network_status *net_stat = 
                                 network_connect("localhost", APP_RECEIVE_PORT);
    
    struct item *hash = create_hmac(key, message);
    //@ assert item(hash, ?h, ss_pub);
    //@ get_info_for_item(h);
    //@ close ss_pub(h);
    //@ leak ss_pub(h);
    struct item *m = create_pair(hash, message);
    //@ assert item(m, ?pmessage, ss_pub);
    //@ get_info_for_item(pmessage);
    //@ close ss_pub(pmessage);
    //@ leak ss_pub(pmessage);
    network_send(net_stat, m);
    item_free(hash);
    item_free(m);
    
    network_disconnect(net_stat);
}
Exemplo n.º 16
0
// -----[ _net_iface_ptmp_send ]-------------------------------------
static net_error_t _net_iface_ptmp_send(net_iface_t * self,
					net_addr_t l2_addr,
					net_msg_t * msg)
{
  net_subnet_t * subnet;
  net_iface_t * dst_iface;
  net_error_t error;
  int reached= 0;

  assert(self->type == NET_IFACE_PTMP);
  
  subnet= self->dest.subnet;

  error= ip_opt_hook_msg_subnet(subnet, msg, &reached);
  if (error != ESUCCESS)
    return error;
  if (reached) {
    message_destroy(&msg);
    return ESUCCESS;
  }

  // Find destination node (based on "physical address")
  dst_iface= net_subnet_find_link(subnet, l2_addr);
  if (dst_iface == NULL)
    return ENET_HOST_UNREACH;

  // Forward along link from subnet -> node ...
  if (!net_iface_is_enabled(dst_iface))
    return ENET_LINK_DOWN;

  network_send(dst_iface, msg);
  return ESUCCESS;
}
Exemplo n.º 17
0
// -----[ _net_iface_ptp_send ]--------------------------------------
static int _net_iface_ptp_send(net_iface_t * self,
			       net_addr_t l2_addr,
			       net_msg_t * msg)
{
  assert(self->type == NET_IFACE_PTP);
  
  network_send(self->dest.iface, msg);
  return ESUCCESS;
}
Exemplo n.º 18
0
/**
 * @brief   Process Network
 * @param   None
 * @retval  None
 */
void Network_Process(void)
{
	static uint8_t arp_counter = 0;

	int i;
	for (i = 0; i < UIP_CONNS; i++)
	{
		uip_periodic(i);
		if (uip_len > 0)
		{
			uip_arp_out();
			enc28j60_send_packet((uint8_t *)uip_buf, uip_len);
		}
	}

	#if UIP_UDP
		for (i = 0; i < UIP_UDP_CONNS; i++)
		{
			uip_udp_periodic(i);
			if (uip_len > 0)
			{
				uip_arp_out();
				network_send();
			}
		}
	#endif /* UIP_UDP */

	if (++arp_counter >= 50)
	{
		arp_counter = 0;
		uip_arp_timer();
	}

	uip_len = enc28j60_recv_packet((uint8_t *)uip_buf, UIP_BUFSIZE);
	if (uip_len > 0)
	{
		if (((struct uip_eth_hdr *)&uip_buf[0])->type == htons(UIP_ETHTYPE_IP))
		{
			uip_arp_ipin();
			uip_input();
			if (uip_len > 0)
			{
				uip_arp_out();
				enc28j60_send_packet((uint8_t *)uip_buf, uip_len);
			}
		}
		else if (((struct uip_eth_hdr *)&uip_buf[0])->type == htons(UIP_ETHTYPE_ARP))
		{
			uip_arp_arpin();
			if (uip_len > 0)
			{
				enc28j60_send_packet((uint8_t *)uip_buf, uip_len);
			}
		}
	}
}
Exemplo n.º 19
0
void ethernet_process(void) {


	if(linkstate == 0) {
		Dm9161       *pDm = &gDm9161;
		if( DM9161_GetLinkSpeed(pDm, 1) != 0 ) {

			TRACE_INFO("P: Link detected\n\r");
			linkstate=1;
			LED2_ON();
			// Auto Negotiate
			if (!DM9161_AutoNegotiate(pDm)) {

				TRACE_INFO("P: Auto Negotiate ERROR!\n\r");
				return;
			}
		}
	} else {


		uip_len = network_read();

		if(uip_len > 0) {

			if(BUF->type == htons(UIP_ETHTYPE_IP)){
				uip_arp_ipin();
				uip_input();
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}

			} else if(BUF->type == htons(UIP_ETHTYPE_ARP)){

				uip_arp_arpin();
				if(uip_len > 0){
					network_send();
				}
			}
		}
	}

}
Exemplo n.º 20
0
static ssize_t write_all(const void *src, size_t len, int fd)
{
	uintptr_t ptr = (uintptr_t) src;
	while (len) {
		ssize_t ret = network_send(fd, (const void *) ptr, len, 0);
		ptr += ret;
		len -= ret;
	}
	return (ssize_t)(ptr - (uintptr_t) src);
}
Exemplo n.º 21
0
int l_disconnect(lua_State *l)
{
  if (network_is_connected(main_window->network)) {
    network_send(main_window->network, "BYE");
  } else {
    g_warning("tetris.client.disconnect: Already disconnected");
  }

  return 0;
}
Exemplo n.º 22
0
static void push_message(MSG msg, size_t msgLength){
	if(queue_nitems(msg_queue) < MAX_MSG_QUEUE_SIZE){
		//printf("Pushing a message for %d with length %d\n", msg.dest, msgLength);
		queue_add(msg_queue, &msg, msgLength);
	}
	if(queue_nitems(msg_queue) == MAX_MSG_QUEUE_SIZE){
		CNET_disable_application(nodeinfo.address);
	}
	network_send();
}
Exemplo n.º 23
0
void nethandler_periodic()
{
	int i;

	times(UIP_CONNS, i) {
		uip_periodic(i);
		if (uip_len > 0) {
			uip_arp_out();
			network_send();
		}
	}
Exemplo n.º 24
0
int l_send(lua_State *l)
{
  gchar *str;

  luaL_checktype(l, 1, LUA_TSTRING);
  str = g_strdup(lua_tostring(l, 1));

  network_send(NETWORK(main_window->network), str);
  g_free(str);
  return 0;
}
Exemplo n.º 25
0
int ps4link_response_rmdir(int result) 
{
	struct { unsigned int number; unsigned short length; int result; } PACKED response;

	// Build the response packet.
	response.number = htonl(PS4LINK_RMDIR_RLY);
	response.length = htons(sizeof(response));
	response.result = htonl(result);

	// Send the response packet.
	return network_send(request_socket, &response, sizeof(response));
}
Exemplo n.º 26
0
/* ***************************************************
 * Function: send_ack
 * ***************************************************
 * Small helper function for preparing an ACK packet and sending
 * it to the network.   
 */
static void send_ack(mysocket_t sd, context_t* ctx)
{
	struct tcphdr *ack_pkt = (struct tcphdr *)calloc(1, (TH_MIN_OFFSET*sizeof(uint32_t)));
	ack_pkt->th_flags = TH_ACK;
	ack_pkt->th_ack = ctx->last_ack_sent;
	ack_pkt->th_seq = ctx->nxt_seq_num;
	our_dprintf("Sending ack: %d with seq: %d\n", ack_pkt->th_ack, ack_pkt->th_seq);	
	ack_pkt->th_win = RWIN;
	ack_pkt->th_off = TH_MIN_OFFSET;
	network_send(sd, ack_pkt, TH_MIN_OFFSET*sizeof(uint32_t));
	free(ack_pkt);	
}
Exemplo n.º 27
0
void ntp_update(void* h)
{
	ntp_handle* handle = (ntp_handle*)h;
	char packet[NTP_PACKET_SIZE];

	int sec, nsec;
	uint32_t* time = (uint32_t*)packet;

	//printf("Sending...\n");

	#define LI 0
	#define VN 3
	#define MODE 3
	#define STRATUM 0
	#define POLL 4
	#define PREC -6

	timer_get(
		&sec,
		&nsec);
	
	memset(
		packet,
		'\0',
		NTP_PACKET_SIZE);
	
	time[0] = htonl(
		(LI << 30) | (VN << 27) | (MODE << 24) |
		(STRATUM << 16) | (POLL << 8) | (PREC & 0xff));

	time[1] = htonl(1<<16);
        time[2] = htonl(1<<16);

	//
	// We need to set the 'Transmit Timestamp' to the server. When
	// the message comes back, this value it moved to the 'Origin Timestamp'
	// field (time[6] and time[7]).
	//
	// For more details on thy these constants are used, see ntpserver.c.
	//
	time[10] = htonl((uint32_t)(sec + 2208988800ULL));
	time[11] = htonl((uint32_t)(((uint64_t)nsec << 32) / 1000000000ULL));

	// Store the last time we sent.	
	handle->last_sec_sent = time[10];
	handle->last_nsec_sent = time[11];
	
	network_send(
		handle->sock,
		packet,
		NTP_PACKET_SIZE);
}
Exemplo n.º 28
0
void nethandler_rx()
{
	uip_len = network_read();

	if (uip_len > 0) {
		switch (ntohs(BUF->type)) {
		case UIP_ETHTYPE_IP:
			uip_arp_ipin();
			uip_input();
			if (uip_len > 0) {
				uip_arp_out();
				network_send();
			}
		break;
		case UIP_ETHTYPE_ARP:
			uip_arp_arpin();
			if (uip_len > 0)
				network_send();
		break;
		}
	}
}
Exemplo n.º 29
0
/* ***************************************************
 * Function: sendrcv_syn
 * ***************************************************
 * 	Helper function for active-side network initiation.
 *   It sends the SYN packet and waits for the right SYN_ACK
 */
static bool sendrcv_syn(mysocket_t sd, context_t *ctx, struct tcphdr* in_packet)
{
	/* Set SYN Headers */
	struct tcphdr* out_packet = (struct tcphdr *)calloc(1,(TH_MIN_OFFSET*sizeof(uint32_t)));
	out_packet->th_ack = 0; 
	out_packet->th_seq = ctx->initial_sequence_num;
  	out_packet->th_flags = TH_SYN;
	out_packet->th_off = TH_MIN_OFFSET;
	out_packet->th_win = RWIN;
	ctx->nxt_seq_num = ctx->initial_sequence_num + 1; /* SYN Packet =  1 byte */
	our_dprintf("Active Sending SYN.  Seq:%d\n", out_packet->th_seq);
	
	unsigned int event;
	bool success = false;
	int num_tries = 1;
	struct timeval tstart;
	gettimeofday(&tstart,NULL); /*start the timer */
	network_send(sd, out_packet, (TH_MIN_OFFSET*sizeof(uint32_t)));
	/* Try sending the SYN a total of 6 times before giving up */
	while ((num_tries < MAX_RT_TRIES) && !success){
		event = wait_for_event(sd, NETWORK_DATA, ctx, tstart);
		if (event & NETWORK_DATA) {
			network_recv(sd, in_packet);
			/* Check to see if we got the right SYN_ACK */
			success = ((in_packet->th_flags & (TH_SYN | TH_ACK)) &&
			    	  (in_packet->th_ack == ctx->nxt_seq_num));
		}
		else if (event==TIMEOUT){
			our_dprintf("Active - SYN TIMEOUT!, no SYN_ACK!\n");
			gettimeofday(&tstart,NULL);
			num_tries+=1;
			our_dprintf("Active Resending SYN.  Seq:%d\n", out_packet->th_seq);
			network_send(sd, out_packet, (TH_MIN_OFFSET*sizeof(uint32_t)));
		}
	}	
	free(out_packet);
	return success;
}
Exemplo n.º 30
0
void
Ethernet_Task(void) {
     int i;

     ethernet_process();
     
     if(timer_expired(&periodic_timer)) {
	  timer_reset(&periodic_timer);
	  
	  for(i = 0; i < UIP_CONNS; i++) {
	       uip_periodic(i);
	       if(uip_len > 0) {
		    uip_arp_out();
		    network_send();
	       }
	  }
	  
	  for(i = 0; i < UIP_UDP_CONNS; i++) {
	       uip_udp_periodic(i);
	       if(uip_len > 0) {
		    uip_arp_out();
		    network_send();
	       }
	  }

	  interface_periodic();
	  
     }
     
     
     if(timer_expired(&arp_timer)) {
	  timer_reset(&arp_timer);
	  uip_arp_timer();
	  
     }
     
}