/// start the client for the usb device 'dev' void start_client(char const *dev) { // on the server instead could create many char tun_name[IFNAMSIZ]; tun_setup(TUNTAP_INTERFACE); // a new device should be opened! tun_name[0] = 0; // create the tap-device // it will exit abruptly if it doesn't open it correctly tun_open(DEFAULT_CLIENT_NO, tun_name); fflush(stdout); setup_routes(tun_name); // wrapper for select fdglue_t fdg; mcp_t *mcp; serialif_t *sif; if (dev) { sif = create_serial_connection(dev, &mcp); } else { sif = create_fifo_connection(&mcp); } init_glue(&fdg,sif,mcp,DEFAULT_CLIENT_NO); main_loop(&fdg); }
int main(int argc, char **argv) { char print_buf[128], dev[IF_NAMESIZE]; log_init(); log_setlevel(LOGLVL_DEBUG); if (argc != 3) { fatal("%s <device> <rate>\n", argv[0]); exit(1); } queue_init(&pan_q); queue_init(&tun_q); init_reconstruct(); if (config_parse("ieee154_interface.conf", &device_config)) { log_fatal_perror("parsing config file failed"); exit(1); } /* set up the serial interface device */ pthread_mutex_init(&pan_lock, NULL); if (setup_serial(argv[1], argv[2], &device_config)) { fatal("opening serial device failed!\n"); exit(1); } snprintf(print_buf, 128, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", device_config.eui64.data[0],device_config.eui64.data[1],device_config.eui64.data[2], device_config.eui64.data[3],device_config.eui64.data[4],device_config.eui64.data[5], device_config.eui64.data[6],device_config.eui64.data[7]); info("device EUI-64: %s\n", print_buf); pthread_create(&pan_writer, NULL, pan_write, NULL); pthread_create(&reconstruct_thread, NULL, age_reconstruct, NULL); /* initialize thes tun */ tun_fd = tun_open(dev); if (tun_fd < 0) { log_fatal_perror("opening tun device failed"); exit(1); } if (tun_setup(dev, device_config.eui64) < 0) { exit(1); } sleep(1); pthread_create(&tun_reader, NULL, tun_dev_read, NULL); pthread_create(&pan_reader, NULL, pan_read, NULL); pthread_join(pan_writer, NULL); }
/* practice accepting connections and transfering data */ int main(int argg, char **argv) { char buf[8192], dev[IFNAMSIZ]; uint8_t *payload; int len, i, flags; ip_malloc_init(); payload = buf + sizeof(struct tun_pi); dev[0] = 0; if ((sock = tun_open(dev)) < 0) exit(1); if (tun_setup(dev, iface_addr) < 0) exit(1); /* tun_setup turns on non-blocking IO. Turn it off. */ flags = fcntl(sock, F_GETFL); flags &= ~O_NONBLOCK; fcntl(sock,F_SETFL, flags); struct tcplib_sock srv_sock; tcplib_init_sock(&srv_sock); memcpy(laddr.sin6_addr.s6_addr, iface_addr, 16); laddr.sin6_addr.s6_addr[15] = 2; laddr.sin6_port = htons(atoi(argv[1])); tcplib_bind(&srv_sock, &laddr); fd_set fds; struct timeval timeout; FD_ZERO(&fds); FD_SET(sock, &fds); FD_SET(fileno(stdin), &fds); timeout.tv_sec = 0; timeout.tv_usec = 500000; while (select(sock + 1, &fds, NULL, NULL, &timeout) >= 0) { if (FD_ISSET(sock, &fds)) { if ((len = read(sock, buf, 8192)) <= 0) break; // printf("read %i bytes\n", len); struct ip6_hdr *iph = (struct ip6_hdr *)payload; if (iph->nxt_hdr == IANA_TCP) { if (rand() % LOSS_RATE_RECPR == 0) { printf("dropping packet on rx\n"); } else { void *p = buf + sizeof(struct tun_pi) + sizeof(struct ip6_hdr); // printBuf(p, len - sizeof(struct tun_pi) - sizeof(struct tcp_hdr)); if (tcplib_process(iph, p)) // len - sizeof(struct tun_pi))) printf("TCPLIB_PROCESS: ERROR!\n"); } } } else if (FD_ISSET(fileno(stdin), &fds)) { char c = getchar(); switch (c) { case 'a': printf("ABORTING CONNETION\n"); tcplib_abort(&srv_sock); break; case 'c': printf("CLOSING CONNETION\n"); tcplib_close(&srv_sock); break; case 's': printf("connection state: %i\n", srv_sock.state); break; } } else { timeout.tv_sec = 0; timeout.tv_usec = 500000; tcplib_timer_process(); } if (srv_sock.state == TCP_CLOSED) { tcplib_bind(&srv_sock, &laddr); } FD_ZERO(&fds); FD_SET(sock, &fds); FD_SET(fileno(stdin), &fds); } tun_close(sock, dev); }