tun_t * tun_open(struct addr *src, struct addr *dst, int mtu) { struct intf_entry ifent; tun_t *tun; char dev[128]; int i; if (src->addr_type != ADDR_TYPE_IP || dst->addr_type != ADDR_TYPE_IP || src->addr_bits != IP_ADDR_BITS || dst->addr_bits != IP_ADDR_BITS) { errno = EINVAL; return (NULL); } if ((tun = calloc(1, sizeof(*tun))) == NULL) return (NULL); if ((tun->intf = intf_open()) == NULL) return (tun_close(tun)); memset(&ifent, 0, sizeof(ifent)); ifent.intf_len = sizeof(ifent); for (i = 0; i < MAX_DEVS; i++) { snprintf(dev, sizeof(dev), "/dev/tun%d", i); strlcpy(ifent.intf_name, dev + 5, sizeof(ifent.intf_name)); tun->save = ifent; if ((tun->fd = open(dev, O_RDWR, 0)) != -1 && intf_get(tun->intf, &tun->save) == 0) { route_t *r; struct route_entry entry; ifent.intf_flags = INTF_FLAG_UP|INTF_FLAG_POINTOPOINT; ifent.intf_addr = *src; ifent.intf_dst_addr = *dst; ifent.intf_mtu = mtu; if (intf_set(tun->intf, &ifent) < 0) tun = tun_close(tun); /* XXX - try to ensure our route got set */ if ((r = route_open()) != NULL) { entry.route_dst = *dst; entry.route_gw = *src; route_add(r, &entry); route_close(r); } break; } } if (i == MAX_DEVS) tun = tun_close(tun); return (tun); }
void slip_free (slip_t *slip) { #ifdef PCE_ENABLE_TUN if (slip->tun_fd >= 0) { tun_close (slip->tun_fd); } #endif }
static int fragroute_close(void) { if (ctx.tun != NULL) tun_close(ctx.tun); if (ctx.route != NULL) route_close(ctx.route); if (ctx.intf != NULL) intf_close(ctx.intf); if (ctx.eth != NULL) eth_close(ctx.eth); if (ctx.arp != NULL) arp_close(ctx.arp); if (ctx.dfile != NULL) pcap_dump_close(ctx.dfile); #ifdef WIN32 WSACleanup(); #endif return (-1); }
static void chr_slip_close (char_drv_t *cdrv) { char_slip_t *drv; drv = cdrv->ext; if (drv->tun_fd >= 0) { tun_close (drv->tun_fd); } if (drv->tun_name != NULL) { free (drv->tun_name); } free (drv); }
int slip_set_tun (slip_t *slip, const char *name) { #ifdef PCE_ENABLE_TUN if (slip->tun_fd >= 0) { tun_close (slip->tun_fd); } slip->tun_fd = tun_open (name); if (slip->tun_fd < 0) { return (1); } return (0); #else return (1); #endif }
tun_t * tun_open(struct addr *src, struct addr *dst, int mtu) { tun_t *tun; char cmd[512]; int ppa; if ((tun = calloc(1, sizeof(*tun))) == NULL) return (NULL); tun->fd = tun->ip_fd = tun->if_fd = -1; if ((tun->fd = open(DEV_TUN, O_RDWR, 0)) < 0) return (tun_close(tun)); if ((tun->ip_fd = open(DEV_IP, O_RDWR, 0)) < 0) return (tun_close(tun)); if ((ppa = ioctl(tun->fd, TUNNEWPPA, ppa)) < 0) return (tun_close(tun)); if ((tun->if_fd = open(DEV_TUN, O_RDWR, 0)) < 0) return (tun_close(tun)); if (ioctl(tun->if_fd, I_PUSH, "ip") < 0) return (tun_close(tun)); if (ioctl(tun->if_fd, IF_UNITSEL, (char *)&ppa) < 0) return (tun_close(tun)); if (ioctl(tun->ip_fd, I_LINK, tun->if_fd) < 0) return (tun_close(tun)); snprintf(tun->name, sizeof(tun->name), "tun%d", ppa); snprintf(cmd, sizeof(cmd), "ifconfig %s %s/32 %s mtu %d up", tun->name, addr_ntoa(src), addr_ntoa(dst), mtu); if (system(cmd) < 0) return (tun_close(tun)); return (tun); }
int tuntap_open(tuntap_dev *device /* ignored */, char *dev, char *device_ip, char *device_mask, const char * device_mac, int mtu) { int i; char tap_device[N2N_OSX_TAPDEVICE_SIZE]; for (i = 0; i < 255; i++) { snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i); device->fd = open(tap_device, O_RDWR); if(device->fd > 0) { traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); break; } } if(device->fd < 0) { traceEvent(TRACE_ERROR, "Unable to open tap device"); return(-1); } else { char buf[256]; FILE *fd; device->ip_addr = inet_addr(device_ip); if ( device_mac ) { /* FIXME - This is not tested. Might be wrong syntax for OS X */ /* Set the hw address before bringing the if up. */ snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", i, device_mac); system(buf); } snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", i, device_ip, device_mask, mtu); system(buf); traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", i, device_ip, device_mask); /* Read MAC address */ snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); /* traceEvent(TRACE_INFO, "%s", buf); */ fd = popen(buf, "r"); if(fd < 0) { tun_close(device); return(-1); } else { int a, b, c, d, e, f; buf[0] = 0; fgets(buf, sizeof(buf), fd); pclose(fd); if(buf[0] == '\0') { traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address"); exit(0); } traceEvent(TRACE_NORMAL, "Interface tap%d [MTU %d] mac %s", i, mtu, buf); if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { device->mac_addr[0] = a, device->mac_addr[1] = b; device->mac_addr[2] = c, device->mac_addr[3] = d; device->mac_addr[4] = e, device->mac_addr[5] = f; } } } /* read_mac(dev, device->mac_addr); */ return(device->fd); }
int tuntap_open(tuntap_dev *device /* ignored */, char *dev, const char *address_mode, /* static or dhcp */ char *device_ip, char *device_mask, const char * device_mac, int mtu) { char tap_device[NTVL_NETBSD_TAPDEVICE_SIZE]; struct ifreq req; if (dev) { snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev); device->fd = open(tap_device, O_RDWR); snprintf(tap_device, sizeof(tap_device), "%s", dev); } else { device->fd = open("/dev/tap", O_RDWR); if(device->fd >= 0) { if (ioctl(device->fd, TAPGIFNAME, &req) == -1) { traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno)); close(device->fd); return(-1); } else snprintf(tap_device, sizeof(tap_device), req.ifr_name); } } if(device->fd < 0) { traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno)); return(-1); } else { char buf[256]; FILE *fd; traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); device->ip_addr = inet_addr(device_ip); if ( device_mac && device_mac[0] != '\0' ) { /* Set the hw address before bringing the if up. */ snprintf(buf, sizeof(buf), "ifconfig %s link %s active", tap_device, device_mac); system(buf); } snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s mtu %d up", tap_device, device_ip, device_mask, mtu); system(buf); traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", tap_device, device_ip, device_mask); /* Read MAC address */ snprintf(buf, sizeof(buf), "ifconfig %s |grep address|cut -c 11-28", tap_device); /* traceEvent(TRACE_INFO, "%s", buf); */ fd = popen(buf, "r"); if(fd < 0) { tun_close(device); return(-1); } else { int a, b, c, d, e, f; buf[0] = 0; fgets(buf, sizeof(buf), fd); pclose(fd); if(buf[0] == '\0') { traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address", tap_device); exit(0); } traceEvent(TRACE_NORMAL, "Interface %s mac %s", tap_device, buf); if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { device->mac_addr[0] = a, device->mac_addr[1] = b; device->mac_addr[2] = c, device->mac_addr[3] = d; device->mac_addr[4] = e, device->mac_addr[5] = f; } } } /* read_mac(dev, device->mac_addr); */ return(device->fd); }
/* 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); }