Beispiel #1
0
int send_packet( const packet *p, const char *why )
{
    int ret;
    mbedtls_net_context *dst = p->dst;

    /* insert corrupted ApplicationData record? */
    if( opt.bad_ad &&
        strcmp( p->type, "ApplicationData" ) == 0 )
    {
        unsigned char buf[MAX_MSG_SIZE];
        memcpy( buf, p->buf, p->len );

        if( p->len <= 13 )
        {
            mbedtls_printf( "  ! can't corrupt empty AD record" );
        }
        else
        {
            ++buf[13];
            print_packet( p, "corrupted" );
        }

        if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
        {
            mbedtls_printf( "  ! dispatch returned %d\n", ret );
            return( ret );
        }
    }

    print_packet( p, why );
    if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
    {
        mbedtls_printf( "  ! dispatch returned %d\n", ret );
        return( ret );
    }

    /* Don't duplicate Application Data, only handshake covered */
    if( opt.duplicate != 0 &&
        strcmp( p->type, "ApplicationData" ) != 0 &&
        rand() % opt.duplicate == 0 )
    {
        print_packet( p, "duplicated" );

        if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
        {
            mbedtls_printf( "  ! dispatch returned %d\n", ret );
            return( ret );
        }
    }

    return( 0 );
}
Beispiel #2
0
int main() {
    struct packet p;
    init_packet(&p);
    print_packet(&p);
    tag_client_send(&p);
    print_packet(&p);
    tag_server_recv(&p);
    print_packet(&p);
    tag_server_send(&p);
    print_packet(&p);
    tag_client_recv(&p);
    print_packet(&p);
    return 0;
}
Beispiel #3
0
static int client_send (uint8_t *data, uint32_t *count)
{
    memcpy(send_buffer.data, data, *count);
    send_buffer.len = *count;

    print_packet(" REQUEST: ", send_buffer.data, send_buffer.len);

    server_process_message(send_buffer.data, send_buffer.len,
                           recv_buffer.data, &recv_buffer.len);

    print_packet("RESPONSE: ", recv_buffer.data, recv_buffer.len);

    return 0;
}
Beispiel #4
0
int main(int argc, char **argv)
{
  if (argc != 2)
  {
    printf("No Argument Specified\n");
    return 1;
  }
  char *endptr = argv[1];
  int number = strtol(argv[1], &endptr, 10);
  if (endptr == argv[1] || number < 0 )
  {
    printf("Invalid Argument\n");
    return 1;
  }
  FILE *fp = fopen( CONTROLLER_DEV, "r+b");
  if ( fp == NULL )
  {
    printf("Controller Does Not Exist\n");
    return 1;
  }
  controller control;
  fwrite( &number, 4, 1, fp );
  for (int i = 0; i < number; i++)
  {
    fread(&control.place_holder, 4, 1, fp);
    print_packet(control);
  }
  fclose(fp);
  //free(control);
  return 0;
}
Beispiel #5
0
void process_packet(u_char *args, const struct pcap_pkthdr *header,
		const u_char *packet)
{
	const struct sniff_ethernet *ether;	/* The ethernet header */
	const struct sniff_ip *ip;		/* The IP header */
	const struct sniff_tcp *tcp;		/* The TCP header */
	const char *payload;			/* Packet payload. */

	u_int size_ip;	/* IP Header length */
	u_int size_tcp; /* TCP Header length */

	ether = (struct sniff_ethernet*)(packet); 
	ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
	size_ip = IP_HL(ip) * 4;
	if(size_ip < 20) {
		printf("\t* Invalid IP header length: %u bytes\n", size_ip);
		return;
	}

	tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
	size_tcp = TH_OFF(tcp) * 4;
	if(size_tcp < 20) {
		printf("\t*Invalid TCP header length: %u bytes\n", size_tcp);

	}

	payload = (u_char*)(packet + SIZE_ETHERNET + size_ip + size_tcp);

	print_packet(ether,ip,tcp);
}
Beispiel #6
0
// Parse the ethernet headers, and return the payload position (0 on error).
uint32_t eth_parse(struct pcap_pkthdr *header, uint8_t *packet,
                   eth_info * eth, config * conf) {
    uint32_t pos = 0;

    if (header->len < 14) {
        fprintf(stderr, "Truncated Packet(eth)\n");
        return 0;
    }

    while (pos < 6) {
        eth->dstmac[pos] = packet[pos];
        eth->srcmac[pos] = packet[pos+6];
        pos++;
    }
    pos = pos + 6;
   
    // Skip the extra 2 byte field inserted in "Linux Cooked" captures.
    if (conf->datalink == DLT_LINUX_SLL) {
        pos = pos + 2;
    }

    // Skip VLAN tagging 
    if (packet[pos] == 0x81 && packet[pos+1] == 0) pos = pos + 4;

    eth->ethtype = (packet[pos] << 8) + packet[pos+1];
    pos = pos + 2;

    SHOW_RAW(
        printf("\neth ");
        print_packet(header->len, packet, 0, pos, 18);
    )
Beispiel #7
0
int open_file(const char *path)
{
    char errbuf[PCAP_ERRBUF_SIZE];
    int packet_count = 0;
    pcap_t *file;

    struct pcap_pkthdr h;

    file = pcap_open_offline(path, errbuf);

    if (!file)
    {
        printf("Error: pcap_open_offline(): %s %s %d.\n", errbuf, __FILE__, __LINE__);
        return ERROR;
    }
    u_char *packet;
    while ((packet = (u_int8_t *) pcap_next(file, &h)) != NULL)
    {
        print_packet(&h, packet);
        packet_count++;
    }

    printf("%d paquetes en el archivo %s.\n", packet_count, path);

    return OK;
}
Beispiel #8
0
/* send an ack for the given message and message ID */
static void send_ack (int sock, struct allnet_header * hp,
                      unsigned char * message_ack,
                      int send_resend_request, char * contact, keyset kset)
{
  if ((hp->transport & ALLNET_TRANSPORT_ACK_REQ) == 0) {
printf ("packet not requesting an ack, no ack sent\n");
    return;
  }
  int size;
  struct allnet_header * ackp =
    create_ack (hp, message_ack, NULL, ADDRESS_BITS, &size);
  if (ackp == NULL)
    return;
  /* also save in the (very likely) event that we receive our own ack */
  currently_sent_ack = (currently_sent_ack + 1) % NUM_ACKS;
  memcpy (recently_sent_acks [currently_sent_ack], message_ack,
          MESSAGE_ID_SIZE);
#ifdef DEBUG_PRINT
  print_packet ((char *) ackp, size, "sending ack", 1);
#endif /* DEBUG_PRINT */
  send_pipe_message_free (sock, (char *) ackp, size, ALLNET_PRIORITY_LOCAL);
/* after sending the ack, see if we can get any outstanding
 * messages from the peer */
  if (send_resend_request)
    request_and_resend (sock, contact, kset);
}
Beispiel #9
0
int main(int argc, char** argv) {
  if (argc < 2) {
    std::cerr << "USAGE: parsepgp <file>" << std::endl;
    return 1;
  }

  std::ifstream pgp_file;
  pgp_file.open(argv[1]); // Flawfinder: ignore (give the user what they want)

  std::stringstream str_stream;
  str_stream << pgp_file.rdbuf();

  try {
    std::string file_contents = str_stream.str();
    parse4880::parse(
        parse4880::ustring(file_contents.begin(), file_contents.end()),
        [](std::shared_ptr<parse4880::PGPPacket> packet) -> bool {
          print_packet(*packet, 0);
          return true;
        });
  }
  catch(const parse4880::parse4880_error& e) {
    fprintf(stderr, "Parse error:\n\t%s\n", e.what());
    return 1;
  }
}
Beispiel #10
0
/**********************************************************************
* %FUNCTION: handle_frame_from_tunnel
* %ARGUMENTS:
*  ses -- l2tp session
*  buf -- received PPP frame
*  len -- length of frame
* %RETURNS:
*  Nothing
* %DESCRIPTION:
*  Shoots the frame to PPP's pty
***********************************************************************/
void l2tp_sync_ppp_handle_frame_from_tunnel(l2tp_session *ses, unsigned char *buf, size_t len)
{
    int n;

	d_dbg("l2tp_sync_ppp_handle_frame_from_tunnel >>>\n");
	
    //+++ michael_lee: (2012.06.15 11:56:55)
    // this will cause kernel skb unalign at ppp_synctty.c ppp_sync_input function. remove it.
    //--- michael_lee
#if 0
    /* Add framing bytes */
    *--buf = 0x03;
    *--buf = 0xFF;
    len += 2;
#endif
#if 0
	print_packet(2, buf, len, "l2tp_sync_ppp_handle_from_from_tunnel");
#endif

	//+++ fix by siyou. 2011/1/6 11:08¤W¤È
		//marco, add 20 ms sleep here, or in some situation, two packets will combine as one packet
		//which will affect the state machine and we will not reply chap response		
		//usleep(20000);
	//---

    /* TODO: Add error checking */
	if (ses->pty_fd < 0) {
		d_dbg("Attempt to write %d bytes to non existent fd.", len);
	} else {
		n = write(ses->pty_fd, buf, len);
	}
	d_dbg("l2tp_sync_ppp_handle_frame_from_tunnel <<<\n");
}
// send WHOHAS to all peers
void send_whohas_packet_to_all(struct packet* packets, int packet_count, int socket, struct sockaddr* dst_addr){
  int i = 0;
  for(i=0;i<packet_count;i++){
  	print_packet(&packets[i]);
    send_packet(packets[i], socket, dst_addr);
  }
}
Beispiel #12
0
/**********************************************************************
* %FUNCTION: handle_frame_to_tunnel
* %ARGUMENTS:
*  ses -- l2tp session
* %RETURNS:
*  Nothing
* %DESCRIPTION:
*  Handles readability on PTY; shoots PPP frame over tunnel
***********************************************************************/
void l2tp_sync_ppp_handle_frame_to_tunnel(l2tp_session * ses)
{
	static unsigned char buf[4096+EXTRA_HEADER_ROOM];
	unsigned char * payload;
	int n;
	int iters = 5;

	d_dbg("%s >>>\n" , __FUNCTION__);
	
	/* It seems to be better to read in a loop than to go
	 * back to select loop.  However, don't loop forever, or
	 * we could have a DoS potential */
	payload = buf + EXTRA_HEADER_ROOM;

	while (iters--)
	{	/* EXTRA_HEADER_ROOM bytes extra space for l2tp header */
		n = read(ses->pty_fd, payload, sizeof(buf)-EXTRA_HEADER_ROOM);
		/* TODO: Check this.... */
		if (n <= 2) break;
		if (!ses) continue;

		/* Chop off framing bytes */
#if 0
		print_packet(2, payload, n, "l2tp_sync_ppp_handle_from_to_tunnel");
#endif
		if (payload[0] == 0xff && payload[1] == 0x03)
		{
			payload += 2;
			n -= 2;
		}
		l2tp_dgram_send_ppp_frame(ses, payload, n);
	}

	d_dbg("l2tp_sync_ppp_handle_from_to_tunnel <<<\n");
}
Beispiel #13
0
void send_packet(apacket *p, atransport *t)
{
    unsigned char *x;
    unsigned sum;
    unsigned count;

    p->msg.magic = p->msg.command ^ 0xffffffff;

    count = p->msg.data_length;
    x = (unsigned char *) p->data;
    sum = 0;
    while(count-- > 0){
        sum += *x++;
    }
    p->msg.data_check = sum;

    print_packet("send", p);

    if (t == NULL) {
        D("Transport is null \n");
        // Zap errno because print_packet() and other stuff have errno effect.
        errno = 0;
        fatal_errno("Transport is null");
    }

    if(write_packet(t->transport_socket, t->serial, &p)){
        fatal_errno("cannot enqueue packet on transport socket");
    }
}
Beispiel #14
0
static uint8
cs8900a_output (struct device_desc *dev, uint8 * buf, uint16 packet_len)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	int len;

	//printf("%s: packet_len:%d\n", __FUNCTION__, packet_len);
#if 0
	print_packet (buf, packet_len);
#endif
	if ((len = net_dev->net_write (net_dev, buf, packet_len)) == -1) {
		fprintf (stderr, "write to tapif error in skyeye-ne2k.c\n");
		return -1;
	}
	io->ctrl_st[CtrlStNum (PP_TxEvent)] |= 0x100;
	io->ctrl_st[CtrlStNum (PP_BusST)] |= Rdy4TxNOW;

	if (io->ctrl_st[CtrlStNum (PP_BusCTL)] & EnableRQ) {
		set_time (io->index, packet_len);
		io->need_update = 1;
		net_cs8900a_set_update_intr (dev);
	}
	return 0;
}
/*---------------------------------------------------------------------------*/
static void
plb_send(mac_callback_t sent, void *ptr)
{
  PRINTF("plb_send\n");

  if ( packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == 0 )	//data
   {
 	  PRINTF("plb_send : DATA\n");
 	  send_req = 1;
 	  sent_callback = sent;
 	  sent_ptr = ptr;
 	 //packetbuf_clear_hdr();
 	  temp_len=packetbuf_datalen();
 	  packetbuf_copyto(dataptr_temp);
 	  print_packet(dataptr_temp,packetbuf_totlen());//JJH3
   }
   //kdw sync
   else if ( packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == 1 ) //sync
   {
 		sent_callback = sent;
 		sent_ptr = ptr;
 		plb_send_sync_start();
   }
   else // error
   {
 	  mac_call_sent_callback(sent, ptr, MAC_TX_ERR, 1); //error   fill this
   }

  return;
}
Beispiel #16
0
void print_packets(std::list<std::shared_ptr<parse4880::PGPPacket>> packets,
                   int level) {

  for (auto i = packets.begin(); i != packets.end(); i++) {
    print_packet(**i, level);
  }
}
Beispiel #17
0
/* handle an incoming packet, acking it if it is a data packet for us
 * returns the message length > 0 if this was a valid data message from a peer.
 * if it gets a valid key, returns -1 (details below)
 * Otherwise returns 0 and does not fill in any of the following results.
 *
 * if it is a data or ack, it is saved in the xchat log
 * if it is a valid data message from a peer or a broadcaster,
 * fills in verified and broadcast
 * fills in contact, message (to point to malloc'd buffers, must be freed)
 * if not broadcast, fills in desc (also malloc'd), sent (if not null)
 * and duplicate.
 * if verified and not broadcast, fills in kset.
 * the data message (if any) is null-terminated
 *
 * if kcontact and ksecret1 are not NULL, assumes we are also looking
 * for key exchange messages sent to us matching either of ksecret1 or
 * (if not NULL) ksecret2.  If such a key is found, returns -1.
 * there are two ways of calling this:
 * - if the user specified the peer's secret, first send initial key,
 *   then call handle_packet with our secret in ksecret1 and our
 *   peer's secret in ksecret2.
 * - otherwise, put our secret in ksecret1, make ksecret2 and kaddr NULL,
 *   and handle_packet is ready to receive a key.
 * In either case, if a matching key is received, it is saved and a
 * response is sent (if a response is a duplicate, it does no harm).
 * kmax_hops specifies the maximum hop count of incoming acceptable keys,
 * and the hop count used in sending the key.
 *
 * if subscription is not null, listens for a reply containing a key
 * matching the subscription, returning -2 if a match is found.
 */
int handle_packet (int sock, char * packet, int psize,
                   char ** contact, keyset * kset,
                   char ** message, char ** desc,
                   int * verified, time_t * sent,
                   int * duplicate, int * broadcast,
                   char * kcontact, char * ksecret1, char * ksecret2,
                   int kmax_hops,
                   char * subscription, 
                   unsigned char * addr, int nbits)
{
  if (! is_valid_message (packet, psize))
    return 0;

  struct allnet_header * hp = (struct allnet_header *) packet;
  int hsize = ALLNET_SIZE (hp->transport);
  if (psize < hsize)
    return 0;

#ifdef DEBUG_PRINT
  if (hp->hops > 0)  /* not my own packet */
    print_packet (packet, psize, "xcommon received", 1);
#endif /* DEBUG_PRINT */

  if (hp->message_type == ALLNET_TYPE_ACK) {
    handle_ack (sock, packet, psize, hsize);
    return 0;
  }

  if (hp->message_type == ALLNET_TYPE_CLEAR) { /* a broadcast packet */
    if ((subscription != NULL) && (addr != NULL)) {
      int sub = handle_sub (sock, hp, packet + hsize, psize - hsize,
                            subscription, addr, nbits);
#ifdef DEBUG_PRINT
      printf ("handle_sub (%d, %p, %p, %d, %s, %p, %d) ==> %d\n",
              sock, hp, packet + hsize, psize - hsize, subscription,
              addr, nbits, sub);
#endif /* DEBUG_PRINT */
      if (sub > 0)   /* received a key in response to our subscription */
        return sub;
    }
#ifdef DEBUG_PRINT
    else
      printf ("subscription %p, addr %p, did not call handle_sub\n",
              subscription, addr);
#endif /* DEBUG_PRINT */
    return handle_clear (hp, packet + hsize, psize - hsize,
                         contact, message, verified, broadcast);
  }

  if (hp->message_type == ALLNET_TYPE_DATA) /* an encrypted data packet */
    return handle_data (sock, hp, packet + hsize, psize - hsize,
                        contact, kset, message, desc, verified, sent,
                        duplicate, broadcast);

  if (hp->message_type == ALLNET_TYPE_KEY_XCHG)
    return handle_key (sock, hp, packet + hsize, psize - hsize,
                       kcontact, ksecret1, ksecret2, kmax_hops);

  return 0;
}
Beispiel #18
0
void main(void) {
	volatile packet_t *p;
	volatile uint8_t t=20;
	uint8_t chan;
	char c;

	gpio_data(0);
	
	gpio_pad_dir_set( 1ULL << LED );
        /* read from the data register instead of the pad */
	/* this is needed because the led clamps the voltage low */
	gpio_data_sel( 1ULL << LED);

	/* trim the reference osc. to 24MHz */
	trim_xtal();

	uart_init(UART1, 115200);

	vreg_init();

	maca_init();

        /* sets up tx_on, should be a board specific item */
        *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2));
	gpio_pad_dir_set( 1ULL << 44 );

	set_power(0x0f); /* 0dbm */
	chan = 0;
	set_channel(chan); /* channel 11 */

	*MACA_MACPANID = 0xaaaa;
	*MACA_MAC16ADDR = 0x1111;
	*MACA_TXACKDELAY = 68; /* 68 puts the tx ack at about the correct spot */
	set_prm_mode(AUTOACK);

	print_welcome("rftest-rx");
	while(1) {		

		/* call check_maca() periodically --- this works around */
		/* a few lockup conditions */
		check_maca();

		if((p = rx_packet())) {
			/* print and free the packet */
			printf("autoack-rx --- ");
			print_packet(p);
			maca_free_packet(p);
		}

		if(uart1_can_get()) {
			c = uart1_getc();
			if(c == 'z') t++;
			if(c == 'x') t--;
			*MACA_TXACKDELAY = t;
			printf("tx ack delay: %d\n\r", t);
		}

	}
}
Beispiel #19
0
static bool server_send_packet(Client *c, Packet *pkt) {
	print_packet("server-send:", pkt);
	if (send_packet(c->socket, pkt))
		return true;
	debug("FAILED\n");
	c->state = STATE_DISCONNECTED;
	return false;
}
Beispiel #20
0
void read_from_file (FILE *fp) {
    uint8_t cmdbuf[MAXCMD];
    uint8_t sz = 0;

    uint8_t response[MAXCMD];

    char line[MAXLINECMD];
    char *p;

    while (fgets (line, MAXLINECMD, fp)) 
    {
        p = line;
        sz = 0;
        while (*p && sz < MAXCMD)
        {
            if (*p == ' ') {
                p++;
                continue;
            }
            if (*p == '\n' || *p == '\r') {
                break;
            }

            if (isxdigit(*p) && isxdigit(p[1])) {
                    sscanf(p, "%02x", (unsigned int *)&cmdbuf[sz]);
                    sz++;
                    p += 2;
                } else {
                    break;
                }
        }
        if (sz > 0) 
        {
            if (optsendpackets && cmdbuf[0] == 0x09) 
            {
                print_packet(cmdbuf, sz);
                send_raw_packet(watch, sz, cmdbuf, response);
                print_packet(response, response[1] + 2);
            } else {
                // we're not going to send anything to the watch, just print it
                print_packet(cmdbuf, sz);
            }
        }
    }
}
Beispiel #21
0
static bool server_write_pty(Packet *pkt) {
	print_packet("server-write-pty:", pkt);
	size_t size = pkt->len;
	if (write_all(server.pty, pkt->u.msg, size) == size)
		return true;
	debug("FAILED\n");
	server.running = false;
	return false;
}
Beispiel #22
0
static bool server_recv_packet(Client *c, Packet *pkt) {
	if (recv_packet(c->socket, pkt)) {
		print_packet("server-recv:", pkt);
		return true;
	}
	debug("server-recv: FAILED\n");
	c->state = STATE_DISCONNECTED;
	return false;
}
Beispiel #23
0
/** Remove extended attributes */
int do_removexattr (const char * path, const char *name){
    char buffer[BUFFERSIZE];
    memset(buffer, 0, BUFFERSIZE);
    corefs_packet* packet = (corefs_packet*)buffer;
    int ret;
    unsigned int packet_size;
    server_data *sd=NULL;

    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_removexattr_local(path, name);
    }
    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;

#ifdef DEBUG
    dprintf(stderr, "REMOVEXATTR: path[%s] name[%s]\n", path, name);
#endif

    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(packet->payload.request.user_ids),
                                path, NULL);

    /* Build the request packet */
    packet_size = build_xattr(packet, COREFS_XATTR_REMOVEXATTR, name,
                              r_path, 0, 0);

    /* encapsulate in network order */
    char request_buf[packet_size];
    memset(request_buf, 0, packet_size);
    ret = encap_corefs_header(request_buf, packet);
    encap_corefs_request(request_buf + ret, packet);
#ifdef DEBUG_NETWORK
    fprintf(stderr, "printing removexattr request\n");
    print_packet(*packet);
#endif 
    if (send_packet(sd->ctx, request_buf, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
    memset(buffer, 0, BUFFERSIZE);
    if ((ret = client_receive_specified(sd->ctx, buffer,
                                        COREFS_RESPONSE_STATUS)) < 0) {
        dprintf(stderr, "GETXATTR: error receiving removexattr status.\n");
        return ret;
    }
    return 0;
}
Beispiel #24
0
/* Receive a Topfield protocol packet.
 * Returns a negative number if the packet read failed for some reason.
 */
ssize_t get_tf_packet(int fd, struct tf_packet * packet)
{
    __u8 *buf = (__u8 *) packet;
    int r;

    trace(3, fprintf(stderr, "get_tf_packet\n"));

    r = usb_bulk_read(fd, 0x82, buf, MAXIMUM_PACKET_SIZE,
                      TF_PROTOCOL_TIMEOUT);
    if(r < 0)
    {
        fprintf(stderr, "USB read error: %s\n", strerror(errno));
        return -1;
    }

    if(r < PACKET_HEAD_SIZE)
    {
        fprintf(stderr, "Short read. %d bytes\n", r);
        return -1;
    }

    /* Send SUCCESS as soon as we see a data transfer packet */
    if(DATA_HDD_FILE_DATA == get_u32_raw(&packet->cmd))
    {
        send_success(fd);
    }

    swap_in_packet(packet);

    {
        __u16 crc;
        __u16 calc_crc;
        __u16 len = get_u16(&packet->length);

        if(len < PACKET_HEAD_SIZE)
        {
            fprintf(stderr, "Invalid packet length %04x\n", len);
            return -1;
        }

        crc = get_u16(&packet->crc);
        calc_crc = get_crc(packet);

        /* Complain about CRC mismatch */
        if(crc != calc_crc)
        {
            fprintf(stderr, "WARNING: Packet CRC %04x, expected %04x\n", crc,
                    calc_crc);
        }
    }

    print_packet(packet, " IN<");
    return r;
}
Beispiel #25
0
int client_receive_specified(COMMCTX* ctx, char* buffer, unsigned int type)
{
    int ret;
    corefs_packet * packet =(corefs_packet*)buffer; 
    char resp_buf[BUFFERSIZE];
    memset(resp_buf, 0, BUFFERSIZE);
  
    ret=receive_packet(ctx, resp_buf);

    if (ret < header_size) {
        dprintf(stderr, "error receiving packet.\n");
        return -EIO;
    }

    /* Copy the header */
    corefs_packet * reply =(corefs_packet*)resp_buf;
    packet->header.magic = reply->header.magic;
    packet->header.type = reply->header.type;
    packet->header.sequence = reply->header.sequence;
    packet->header.payload_size = reply->header.payload_size;
  

    /* decap the received message */
    
    if(packet->header.type == COREFS_RESPONSE)
        decap_corefs_response(resp_buf + header_size, packet);
    else{
        dprintf(stderr, "client_receive_specified: Received wrong packet type [%u], packet sequence[%u]\n", packet->header.type, packet->header.sequence);
        return -EPROTO;
    }
#ifdef DEBUG_NETWORK
    dprintf(stderr, "client_receive_specified: printing response\n");
    print_packet(*packet);
#endif
 
    if(packet->payload.response.type == COREFS_RESPONSE_ERROR){
        return -packet->payload.response.rop.status.bits;
    }
    else if((packet->payload.response.type != type) &&
            !((type == COREFS_RESPONSE_DATA) &&
              (packet->payload.request.type == COREFS_RESPONSE_MOREDATA))) {
        dprintf(stderr, "client_receive_specified: Error - expecting packet type %i, received %i.\n", type, packet->payload.response.type);
        return -EPROTO;
    }
    else if (packet->payload.response.type == COREFS_RESPONSE_STATUS){
        ret = packet->payload.response.rop.status.bits;
    }
    else {
        ret= packet->header.payload_size;
    }

    return ret;
  
}
Beispiel #26
0
static bool server_read_pty(Packet *pkt) {
	pkt->type = MSG_CONTENT;
	ssize_t len = read(server.pty, pkt->u.msg, sizeof(pkt->u.msg));
	if (len > 0)
		pkt->len = len;
	else if (len == 0)
		server.running = false;
	else if (len == -1 && errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK)
		server.running = false;
	print_packet("server-read-pty:", pkt);
	return len > 0;
}
Beispiel #27
0
/* Given a Topfield protocol packet, this function will calculate the required
 * CRC and send the packet out over a bulk pipe. */
ssize_t send_tf_packet(int fd, struct tf_packet *packet)
{
    unsigned int pl = get_u16(&packet->length);
    ssize_t byte_count = (pl + 1) & ~1;

    trace(3, fprintf(stderr, "%s\n", __func__));
    put_u16(&packet->crc, get_crc(packet));
    print_packet(packet, "OUT>");
    swap_out_packet(packet);
    return usb_bulk_write(fd, 0x01, (__u8 *) packet, byte_count,
                          TF_PROTOCOL_TIMEOUT);
}
Beispiel #28
0
int main(int argc, char **argv)
{
    int c;
    char *input_file, *output_file = NULL;
    program_name = argv[0];

    input_file= dev_file;

    while((c = getopt(argc, argv, "i:o:")) != -1) {
        switch (c) {
        case 'i':
            strcpy(buf_i, optarg);
            input_file = buf_i;
            break;
        case 'o':
            strcpy(buf_o, optarg);
            output_file = buf_o;
            break;
        default:
            usage();
        }
    }

    int fdin, err;
    FILE * fout;


    fdin = open(input_file, O_RDONLY);

    if(output_file == NULL){
        fout = stdout;
    } else{
        fout = fopen(output_file, "w+");
    }

    while(1){
        int len;

        len = read(fdin, buffer, 2000);
        if(len > 0){
            print_packet(fout, buffer, len);
        } else{
            break;
        }
    }

    close(fdin);
    fclose(fout);

    return 0;
}
Beispiel #29
0
int main(int argc, const char *argv[])
{
	int len, size, tcphdrlen;
	u8 *pdata, *http;
	u8 dstmac[ETH_ALEN] = {0};
	char recvbuf[BUFSIZE];
	struct sockaddr_ll sa;
	struct ethhdr *ethhdr;
	struct iphdr *iphdr;
	struct tcphdr *tcphdr;
	char *ifname = "wfm";

SOCK:
	if ((sock_fd = open_socket(ifname)) < 0) {
		perror("open_socket");
		exit(EXIT_FAILURE);
	}

	if ((ifindex = interface_index(ifname)) < 0) {
		perror("interface_index");
		exit(EXIT_FAILURE);
	}

	if (interface_addr(ifname, dstmac) < 0) {
		perror("interface_addr");
		exit(EXIT_FAILURE);
	}

	memset(&sa, 0, sizeof(sa));
	sa.sll_family = AF_PACKET;
	sa.sll_protocol = htons(ETH_P_ALL);
	sa.sll_ifindex = ifindex;

	if (bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
		exit(EXIT_FAILURE);

	for ( ; ; ) {
		len = recvfrom(sock_fd, recvbuf, BUFSIZE, 0, NULL, NULL);
		if (len < 0) {
			close(sock_fd);
			sleep(3);
			goto SOCK;
		}

		/* process packet ... */
		print_packet(recvbuf, len);
		/* process packet ... */
	}
	return 0;
}
/*---------------------------------------------------------------------------*/
static char
plb_send_strobe(rimeaddr_t *dst, int *acked, uint8_t type)
{
  PRINTF("plb_send_strobe  [dst: %u.%u] [type: %x]\n",dst->u8[0],dst->u8[1],type);

  uint8_t strobe[MAX_STROBE_SIZE];
  int strobe_len = 0;
  rtimer_clock_t wt;

  // Make PLB header
  packetbuf_clear();
  if( (strobe_len = plb_create_header(dst,type)) < 0 ){
    return -1;
  }

  // Make packet -> strobe
  strobe_len = strobe_len +1;	// assign space for packet type
  if( strobe_len > (int)sizeof(strobe)) {
    /* Failed to send */
    PRINTF("plb: send failed, too large header\n");
    return -1;
  }
  memcpy(strobe, packetbuf_hdrptr(), strobe_len);

  /* Send beacon and wait ack : STROBE_NUM_MAX times */
  int strobe_num = 0;
  radio_on();

  while((strobe_num < STROBE_NUM_MAX) && ((*acked) == 0)){
    PRINTF("plb_send_strobe : strobe %d\n",strobe_num);

#if DEBUG_PACKET
    print_packet(strobe, strobe_len);
#endif

    if(NETSTACK_RADIO.send(strobe, strobe_len) != RADIO_TX_OK){
      return -1;
    }
    strobe_num ++;

    (*acked) = plb_wait_ack(type);
    if(*acked){
      PRINTF("ack! return: %d\n", *acked);
    }
  }
  radio_off();
  return 0;
}