#include "faketcp.h" #include #include #include #include #include #include #include void tcp_input(int fd, const void* input, const void* payload, int tot_len) { const struct iphdr* iphdr = static_cast(input); const struct tcphdr* tcphdr = static_cast(payload); } int main() { union { unsigned char buf[ETH_FRAME_LEN]; struct iphdr iphdr; }; const int iphdr_size = sizeof iphdr; char ifname[IFNAMSIZ] = "tun%d"; int fd = tun_alloc(ifname); if (fd < 0) { fprintf(stderr, "tunnel interface allocation failed\n"); exit(1); } printf("allocted tunnel interface %s\n", ifname); sleep(1); for (;;) { int nread = read(fd, buf, sizeof(buf)); if (nread < 0) { perror("read"); close(fd); exit(1); } printf("read %d bytes from tunnel interface %s.\n", nread, ifname); if (nread >= iphdr_size && iphdr.version == 4 && iphdr.ihl*4 >= iphdr_size && iphdr.ihl*4 <= nread && iphdr.tot_len == htons(nread) && in_checksum(buf, iphdr.ihl*4) == 0) { const void* payload = buf + iphdr.ihl*4; if (iphdr.protocol == IPPROTO_ICMP) { icmp_input(fd, buf, payload, nread); } else if (iphdr.protocol == IPPROTO_TCP) { tcp_input(fd, buf, payload, nread); } } else { printf("bad packet\n"); for (int i = 0; i < nread; ++i) { if (i % 4 == 0) printf("\n"); printf("%02x ", buf[i]); } printf("\n"); } } return 0; }