예제 #1
0
파일: txrx.c 프로젝트: yurai007/lfs-academy
int main(int argc, char *argv[])
{
    (void)argc, (void)argv;

    unsigned char packet[MAX_PACKET_SIZE];
    struct interface_data iface_data;
    unsigned char dest_mac[ETH_ALEN] = {0x90, 0x2b, 0x34, 0x1a, 0x5f, 0x04};
    struct sockaddr_ll dest_address;
    const int packet_size = 64;
    unsigned char payload[4] = {0xde, 0xad, 0xbe, 0xef};

    int sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
    assert(sockfd > 2);

    get_src_mac(&iface_data, sockfd);
    fill_packet(packet, dest_mac, iface_data.if_mac.ifr_hwaddr.sa_data, packet_size, payload);

    dest_address.sll_ifindex = iface_data.index;
    dest_address.sll_halen = ETH_ALEN;
    memcpy(dest_address.sll_addr, dest_mac, ETH_ALEN);

    int result = sendto(sockfd, packet, packet_size, 0, (struct sockaddr*)&dest_address,
               sizeof(struct sockaddr_ll));
    assert(result == packet_size);

    return 0;
}
예제 #2
0
파일: ntp.c 프로젝트: Diggen85/a-culfw
void
ntp_sendpacket()
{
  if(eth_debug) {
    DC('n'); DC('s'); ntp_func(0);
  }

  if(ntp_conn == 0)
    return;

  uip_udp_conn = ntp_conn;
  fill_packet((ntp_packet_t*)uip_appdata);
  uip_send(uip_appdata, sizeof(ntp_packet_t));

  uip_process(UIP_UDP_SEND_CONN);
  uip_arp_out();
  network_send();
}
예제 #3
0
__dead static void
worker(void *arg)
{
	ifnet_t *ifp = ifunit(IFNAME_INT);
	unsigned int i = (uintptr_t)arg;
	struct mbuf *m = fill_packet(i);
	uint64_t n = 0;

	while (!run)
		/* spin-wait */;
	while (!done) {
		int error;

		error = npf_packet_handler(NULL, &m, ifp, PFIL_OUT);
		KASSERT(error == 0);
		n++;
	}
	npackets[i] = n;
	kthread_exit(0);
}
예제 #4
0
static int
test_bpf_code(void *code, size_t size)
{
	ifnet_t *dummy_ifp = npf_test_addif(IFNAME_TEST, false, false);
	npf_cache_t npc = { .npc_info = 0 };
	bpf_args_t bc_args;
	struct mbuf *m;
	nbuf_t nbuf;
	int ret, jret;
	void *jcode;

	/* Layer 3 (IP + TCP). */
	m = fill_packet(IPPROTO_TCP);
	nbuf_init(&nbuf, m, dummy_ifp);
	npf_cache_all(&npc, &nbuf);

	memset(&bc_args, 0, sizeof(bpf_args_t));
	bc_args.pkt = m;
	bc_args.wirelen = m_length(m);
	bc_args.arg = &npc;

	ret = npf_bpf_filter(&bc_args, code, NULL);

	/* JIT-compiled code. */
	jcode = npf_bpf_compile(code, size);
	if (jcode) {
		jret = npf_bpf_filter(&bc_args, NULL, jcode);
		assert(ret == jret);
		bpf_jit_freecode(jcode);
	} else if (lverbose) {
		printf("JIT-compilation failed\n");
	}
	m_freem(m);

	return ret;
}

static uint32_t
npf_bpfcop_run(u_int reg)
{
	struct bpf_insn insns_npf_bpfcop[] = {
		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
		BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg),
		BPF_STMT(BPF_RET+BPF_A, 0),
	};
	return test_bpf_code(&insns_npf_bpfcop, sizeof(insns_npf_bpfcop));
}

static bool
npf_bpfcop_test(void)
{
	bool fail = false;

	/* A <- IP version (4 or 6) */
	struct bpf_insn insns_ipver[] = {
		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
		BPF_STMT(BPF_RET+BPF_A, 0),
	};
	fail |= (test_bpf_code(&insns_ipver, sizeof(insns_ipver)) != IPVERSION);

	/* BPF_MW_IPVERI <- IP version */
	fail |= (npf_bpfcop_run(BPF_MW_IPVER) != IPVERSION);

	/* BPF_MW_L4OFF <- L4 header offset */
	fail |= (npf_bpfcop_run(BPF_MW_L4OFF) != sizeof(struct ip));

	/* BPF_MW_L4PROTO <- L4 protocol */
	fail |= (npf_bpfcop_run(BPF_MW_L4PROTO) != IPPROTO_TCP);

	return fail;
}
예제 #5
0
int do_serial_comms(const char *ttyfile, const char *baudrate, struct record *root)
{
	int fd;
	int len;
	speed_t speed;
	uint8_t *ptr;
	struct record *rec;
	const uint8_t BACKSPACE[] = {0x08};
	const uint8_t ERASE_ALL[] = {0x00};
	const uint8_t ACK = 0x06;
	ptr = io_buffer;

	if (baudrate_to_speed_t(baudrate, &speed)) {
		fprintf(stderr, "unsupported baudrate, %s\n", baudrate);
		return -1;
	}

	fd = serial_open(ttyfile, speed);
	if (fd < 0) {
		fprintf(stderr, "error opening serial port\n");
		return -1;
	}

	fprintf(stdout, "Putting card into download mode.\n");
	//Set PROG and reset
	set_dtr(fd, true);
	set_rts(fd, true);

	sleep(1);
	

	fprintf(stdout, "Trying to synchronize with ADuC70xx: ");
	fflush(stdout);
	
	while (serial_read_fully(fd, ptr, 1, 100));
	
	set_rts(fd, false);
	

	do {
		len = serial_write_fully(fd, BACKSPACE, sizeof(BACKSPACE), 1000);
		len = serial_read_fully(fd, ptr, 24, 100);
		
	} while (!((len > 6) && (memcmp(ptr, "ADuC70", 6) == 0)));

	//Disable PROG
	set_dtr(fd, false);
	
	while (serial_read_fully(fd, ptr, 1, 100));

	if ((len > 6) && (memcmp(ptr, "ADuC70", 6) == 0)) {
		int product_len = (memchr(ptr, ' ', 24) - (void *)ptr);
		fprintf(stdout, "Found %.*s\n", product_len, ptr);

		fprintf(stdout, "Erasing: ");
		fflush(stdout);

		len = fill_packet(ptr, 'E', 0x00000000, ERASE_ALL, sizeof(ERASE_ALL));
		len = serial_write_fully(fd, ptr, len, 1000);
		len = serial_read_fully(fd, ptr, 1, 10000);

		if ((len <= 0) || (ptr[0] != ACK)) {
			fprintf(stdout, "FAILED\n");
			goto error;
		}

		fprintf(stdout, "OK\n");

		rec = root;
		while (rec != NULL) {
			int count = 0;
			uint32_t address = rec->address;
			uint8_t *data = data_buffer;
			struct record *drec;

			for (drec = rec; ((drec != NULL) && ((address + count) == drec->address) && ((count + drec->count) < DATA_BUFFER_SIZE)); drec = drec->next) {
				memcpy(data, drec->data, drec->count);
				data += drec->count;
				count += drec->count;
			}
			rec = drec;

			fprintf(stdout, "Writting (%#x): ", address);
			fflush(stdout);

			len = fill_packet(ptr, 'W', address, data_buffer, count);
			len = serial_write_fully(fd, ptr, len, 1000);
			len = serial_read_fully(fd, ptr, 1, 10000);

			if ((len <= 0) || (ptr[0] != ACK)) {
				fprintf(stdout, "FAILED\n");
				goto error;
			}

			fprintf(stdout, "OK\n");
		}

		if (reset) {
			fprintf(stdout, "Resetting: ");
			fflush(stdout);

			len = fill_packet(ptr, 'R', 0x00000001, NULL, 0);
			len = serial_write_fully(fd, ptr, len, 1000);
			len = serial_read_fully(fd, ptr, 1, 10000);

			if ((len <= 0) || (ptr[0] != ACK)) {
				fprintf(stdout, "FAILED\n");
				goto error;
			}

			fprintf(stdout, "OK\n");
		}
	} else {
		fprintf(stdout, "FAILED\n");
		goto error;
	}

	if (follow) {
		int count = 600;
		while (count--) {
			len = serial_read(fd, ptr, 16, 1000);
			if (len > 0) {
				dump_text(stdout, ptr, len);
			}
		}
	}

error:
	if (serial_close(fd) < 0) {
		fprintf(stderr, "error closing serial port\n");
		return -1;
	}

	return 0;
}
예제 #6
0
파일: autoack-tx.c 프로젝트: 1uk3/contiki
void main(void) {
	volatile packet_t *p;
	char c;
	uint16_t r=30; /* start reception 100us before ack should arrive */
	uint16_t end=180; /* 750 us receive window*/

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

	uart_init(UART1, 115200);

	vreg_init();

	maca_init();

	set_channel(0); /* channel 11 */
//	set_power(0x0f); /* 0xf = -1dbm, see 3-22 */
//	set_power(0x11); /* 0x11 = 3dbm, see 3-22 */
	set_power(0x12); /* 0x12 is the highest, not documented */

        /* sets up tx_on, should be a board specific item */
	GPIO->FUNC_SEL_44 = 1;	 
	GPIO->PAD_DIR_SET_44 = 1;	 

	GPIO->FUNC_SEL_45 = 2;	 
	GPIO->PAD_DIR_SET_45 = 1;	 

	*MACA_RXACKDELAY = r;
	
	printf("rx warmup: %d\n\r", (int)(*MACA_WARMUP & 0xfff));

	*MACA_RXEND = end;

	printf("rx end: %d\n\r", (int)(*MACA_RXEND & 0xfff));

	set_prm_mode(AUTOACK);

	print_welcome("rftest-tx");

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

		while((p = rx_packet())) {
			if(p) {
				printf("RX: ");
				print_packet(p);
				free_packet(p);
			}
		}

		if(uart1_can_get()) {
			c = uart1_getc();

			switch(c) {
			case 'z':
				r++;
				if(r > 4095) { r = 0; }
				*MACA_RXACKDELAY = r;
				printf("rx ack delay: %d\n\r", r);
				break;
			case 'x':
				if(r == 0) { r = 4095; } else { r--; }
				*MACA_RXACKDELAY = r;
				printf("rx ack delay: %d\n\r", r);
				break;
			case 'q':
				end++;
				if(r > 4095) { r = 0; }
				*MACA_RXEND = end;
				printf("rx end: %d\n\r", end);
				break;
			case 'w':
				end--;
				if(r == 0) { r = 4095; } else { r--; }
				*MACA_RXEND = end;
				printf("rx end: %d\n\r", end);
				break;
			default:
				p = get_free_packet();
				if(p) {
					fill_packet(p);
					
					printf("autoack-tx --- ");
					print_packet(p);
					
					tx_packet(p);				
				}
				break;
			}
		}
		
	}

}
예제 #7
0
void main(void) {
  volatile packet_t *p;
#ifdef CARRIER_SENSE
  volatile uint32_t i;
#endif
  uint16_t r=30; /* start reception 100us before ack should arrive */
  uint16_t end=180; /* 750 us receive window*/

  /* trim the reference osc. to 24MHz */
  trim_xtal();
  uart_init(INC, MOD, SAMP);
  vreg_init();
  maca_init();

  ///* Setup the timer */
  *TMR_ENBL = 0;                     /* tmrs reset to enabled */
  *TMR0_SCTRL = 0;
  *TMR0_LOAD = 0;                    /* reload to zero */
  *TMR0_COMP_UP = 18750;             /* trigger a reload at the end */
  *TMR0_CMPLD1 = 18750;              /* compare 1 triggered reload level, 10HZ maybe? */
  *TMR0_CNTR = 0;                    /* reset count register */
  *TMR0_CTRL = (COUNT_MODE<<13) | (PRIME_SRC<<9) | (SEC_SRC<<7) | (ONCE<<6) | (LEN<<5) | (DIR<<4) | (CO_INIT<<3) | (OUT_MODE);
  *TMR_ENBL = 0xf;                   /* enable all the timers --- why not? */

  set_channel(CHANNEL); /* channel 11 */
  set_power(0x12); /* 0x12 is the highest, not documented */

  /* sets up tx_on, should be a board specific item */
  GPIO->FUNC_SEL_44 = 1;	 
  GPIO->PAD_DIR_SET_44 = 1;	 
  GPIO->FUNC_SEL_45 = 2;	 
  GPIO->PAD_DIR_SET_45 = 1;	 
  *MACA_RXACKDELAY = r;
  *MACA_RXEND = end;

  set_prm_mode(AUTOACK);

  while(1) {		

    if((*TMR0_SCTRL >> 15) != 0) 
      tick();

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

    while((p = rx_packet())) {
      if(p) free_packet(p);
    }

    p = get_free_packet();
    if(p) {
      fill_packet(p);

#ifdef CARRIER_SENSE
      for(i=0; i<POWER_DELAY; i++) {continue;}
      while(get_power()>74) {}
#endif

#ifdef BLOCKING_TX
      blocking_tx_packet(p);
#else
      tx_packet(p);
#endif

      current_pkts++;

#if defined(RANDOM_WAIT_TIME) || defined(FIXED_WAIT)
      random_wait();
#endif
    }
  }
}
예제 #8
0
파일: pg3.c 프로젝트: 2mac/iputils
static void pg_inject(void)
{
	u32 saddr;
	struct net_device *odev;
	struct sk_buff *skb;
	struct timeval start, stop;
	u32 total, idle;
	int pc, lcount;

	odev = pg_setup_inject(&saddr);
	if (!odev)
		return;

	skb = fill_packet(odev, saddr);
	if (skb == NULL)
		goto out_reldev;

	forced_stop = 0;
	idle_acc_hi = 0;
	idle_acc_lo = 0;
	pc = 0;
	lcount = pg_count;
	do_gettimeofday(&start);

	for(;;) {
		spin_lock_bh(&odev->xmit_lock);
		atomic_inc(&skb->users);
		if (!netif_queue_stopped(odev)) {
			if (odev->hard_start_xmit(skb, odev)) {
				kfree_skb(skb);
				if (net_ratelimit())
					printk(KERN_INFO "Hard xmit error\n");
			}
			pc++;
		} else {
			kfree_skb(skb);
		}
		spin_unlock_bh(&odev->xmit_lock);

		if (pg_ipg)
			nanospin(pg_ipg);
		if (forced_stop)
			goto out_intr;
		if (signal_pending(current))
			goto out_intr;

		if (--lcount == 0) {
			if (atomic_read(&skb->users) != 1) {
				u32 idle_start, idle;

				idle_start = get_cycles();
				while (atomic_read(&skb->users) != 1) {
					if (signal_pending(current))
						goto out_intr;
					schedule();
				}
				idle = get_cycles() - idle_start;
				idle_acc_lo += idle;
				if (idle_acc_lo < idle)
					idle_acc_hi++;
			}
			break;
		}

		if (netif_queue_stopped(odev) || current->need_resched) {
			u32 idle_start, idle;

			idle_start = get_cycles();
			do {
				if (signal_pending(current))
					goto out_intr;
				if (!netif_running(odev))
					goto out_intr;
				if (current->need_resched)
					schedule();
				else
					do_softirq();
			} while (netif_queue_stopped(odev));
			idle = get_cycles() - idle_start;
			idle_acc_lo += idle;
			if (idle_acc_lo < idle)
				idle_acc_hi++;
		}
	}

	do_gettimeofday(&stop);

	total = (stop.tv_sec - start.tv_sec)*1000000 +
		stop.tv_usec - start.tv_usec;

	idle = (((idle_acc_hi<<20)/pg_cpu_speed)<<12)+idle_acc_lo/pg_cpu_speed;

	if (1) {
		char *p = pg_result;

		p += sprintf(p, "OK: %u(c%u+d%u) usec, %u (%dbyte,%dfrags) %upps %uMB/sec",
			     total, total-idle, idle,
			     pc, skb->len, skb_shinfo(skb)->nr_frags,
			     ((pc*1000)/(total/1000)),
			     (((pc*1000)/(total/1000))*pkt_size)/1024/1024
			     );
	}

out_relskb:
	kfree_skb(skb);
out_reldev:
	dev_put(odev);
	return;

out_intr:
	sprintf(pg_result, "Interrupted");
	goto out_relskb;
}
예제 #9
0
PacketCenter::PacketCenter (string input, PacketType inputType)
{
	/* By default the encryption key is set to null, for obvious reasons. */
	_key = 0;
	/* Initialize the vectors. */
	packets[ENCRYPTED].resize(1);
	packets[DECRYPTED].resize(1);
	processed[0] = false;
	processed[1] = false;

	HungryVector<Packet*>* packet_vector =0;

	/* If we're being handed a plaintext string */
	if (inputType == DECRYPTED)
	{
		/* Alias the packet vector we're currently work with */
		packet_vector = &packets[DECRYPTED];
		unsigned int input_len = input.length();

		/* Pad the input to have a length evenly divisible by eight. */
		while (input_len % Packet::SIZE != 0)
		{
			input.append(" ");
			input_len++;
		}
		/* Determine the number of paackets that will be generated
		 * from the input.
		 */
		unsigned int num_packets = input_len/Packet::SIZE;

		/*Split the input into chunks and place them into packets,
		 * all of which go into the DECRYPTED packets vector.
		 */
		for (unsigned int p = 0; p < num_packets; p++)
		{
			int start = p*Packet::SIZE;
			string substring = input.substr(start, Packet::SIZE);

			Packet* pkt = new Packet(substring);
			packet_vector->add(pkt);
		}

		/* Get rid of extra slots from the double function. */
		packet_vector->trim();

		/* Make sure that we don't have the option to process this again. */
		processed[DECRYPTED] = true;
	}
	else
	{

		/* Grab the correct vector. */
		packet_vector = &packets[ENCRYPTED];

		/* Get rid of the extra space that should be at the end of the string,
		 * taking any other accidental extra spaces with it.
		 */
		TurnLeft::Utils::trimchar(input);

		/* There will be one space between each set, therefore, there is one
		 * more set than spaces.
		 */
		int num_chars = count(input.begin(), input.end(), ' ')+1;

		/* This should be a number divisble by nine already, because
		 * Blowfish null terminates packets.
		 * If this assertion fails, it's because somebody
		 * was tampering with the data...
		 */
		assert(num_chars % (Packet::SIZE+1) == 0);


		int num_packets = num_chars/(Packet::SIZE+1);

		/* This variable will be modified by the fill_packet method */
		int start = 0;
		for (int p = 0; p < num_packets; p++)
		{
			/* Extracts the next eight char sets from the string,
			 * converts the sets into an unsigned char, and assigns
			 * that char to a spot in the CharArray that will
			 * be given to the packet.
			 */
			Packet* pkt = fill_packet(input, start);
			packet_vector->add(pkt);
		}

		/* Trim any extra slots generated by the double method. */
		packet_vector->trim();

		/* Make sure we are not able to process encrypted data again. */
		processed[ENCRYPTED] = true;
	}
}
예제 #10
0
/* Test that fill_header generate right header
 * Test header and packet are right size header=8 packet=1024
 * Test fill_packet generate right packet
 * Test read_header return right header data structure
 * Test read_packet reads correct amount of payload*/
int main(int argc, char *argv[])
{
	init_test(argc,argv);
	assert(sizeof(header_t)==8);
	assert(sizeof(packet_t)==1024);
	packet_t test_packet;
	header_t test_header;
	/* Test write and read header */
	/* fill_header fills buf with header */
	fill_header(100,200,333,ACK,&test_packet);
	/* We have empty test_header, read data in buf and assign it to test_heaer */
	read_header(&test_header,&test_packet);
	assert(test_header.seq==100);
	assert(test_header.ack==200);
	assert(test_header.offset==333);
	assert(test_header.flag==ACK);
	/* Test write and read payload */
	/* Initial send buffer */
	u_char sender_buf[2048],recv_buf[2048];
	memset(recv_buf,0,2048);
	u_char * psend=sender_buf;
	u_char * precv=recv_buf;
	int i;
	for (i=0;i<1016;i++)
	{
		sender_buf[i]=5;
	}
	for (i=1016;i<2032;i++)
	{
		sender_buf[i]=6;
	}
	for (i=2032;i<2048;i++)
	{
		sender_buf[i]=7;
	}
	/* Send 1016 bytes payload packet */
	fill_packet(psend,&test_packet,PAYLOAD_SIZE);
	/* Suppose we send test_packet...... */
	/* OK, Now we receive new data, cast it into test_packet */
	read_packet(precv,&test_packet,PAYLOAD_SIZE);
	for (i=0;i<1016;i++)
	{
		assert(recv_buf[i]==5);
	}
	psend+=PAYLOAD_SIZE;
	precv+=PAYLOAD_SIZE;
	fill_packet(psend,&test_packet,PAYLOAD_SIZE);
	read_packet(precv,&test_packet,PAYLOAD_SIZE);
	for (i=1016;i<2032;i++)
	{
		assert(recv_buf[i]==6);
	}
	psend+=PAYLOAD_SIZE;
	precv+=PAYLOAD_SIZE;
	fill_packet(psend,&test_packet,16);
	read_packet(precv,&test_packet,16);
	for (i=2032;i<2048;i++)
	{
		assert(recv_buf[i]==7);
	}

	return 0;
}