acceptall.cc revision cc808594
1cc808594SShuo Chen#include "faketcp.h" 2cc808594SShuo Chen 3cc808594SShuo Chen#include <stdio.h> 4cc808594SShuo Chen#include <stdlib.h> 5cc808594SShuo Chen#include <string.h> 6cc808594SShuo Chen#include <unistd.h> 7cc808594SShuo Chen#include <netinet/ip.h> 8cc808594SShuo Chen#include <netinet/tcp.h> 9cc808594SShuo Chen#include <linux/if_ether.h> 10cc808594SShuo Chen 11cc808594SShuo Chenvoid tcp_input(int fd, const void* input, const void* payload, int tot_len) 12cc808594SShuo Chen{ 13cc808594SShuo Chen const struct iphdr* iphdr = static_cast<const struct iphdr*>(input); 14cc808594SShuo Chen const struct tcphdr* tcphdr = static_cast<const struct tcphdr*>(payload); 15cc808594SShuo Chen 16cc808594SShuo Chen} 17cc808594SShuo Chen 18cc808594SShuo Chenint main() 19cc808594SShuo Chen{ 20cc808594SShuo Chen union 21cc808594SShuo Chen { 22cc808594SShuo Chen unsigned char buf[ETH_FRAME_LEN]; 23cc808594SShuo Chen struct iphdr iphdr; 24cc808594SShuo Chen }; 25cc808594SShuo Chen 26cc808594SShuo Chen const int iphdr_size = sizeof iphdr; 27cc808594SShuo Chen 28cc808594SShuo Chen char ifname[IFNAMSIZ] = "tun%d"; 29cc808594SShuo Chen int fd = tun_alloc(ifname); 30cc808594SShuo Chen 31cc808594SShuo Chen if (fd < 0) 32cc808594SShuo Chen { 33cc808594SShuo Chen fprintf(stderr, "tunnel interface allocation failed\n"); 34cc808594SShuo Chen exit(1); 35cc808594SShuo Chen } 36cc808594SShuo Chen 37cc808594SShuo Chen printf("allocted tunnel interface %s\n", ifname); 38cc808594SShuo Chen sleep(1); 39cc808594SShuo Chen 40cc808594SShuo Chen for (;;) 41cc808594SShuo Chen { 42cc808594SShuo Chen int nread = read(fd, buf, sizeof(buf)); 43cc808594SShuo Chen if (nread < 0) 44cc808594SShuo Chen { 45cc808594SShuo Chen perror("read"); 46cc808594SShuo Chen close(fd); 47cc808594SShuo Chen exit(1); 48cc808594SShuo Chen } 49cc808594SShuo Chen printf("read %d bytes from tunnel interface %s.\n", nread, ifname); 50cc808594SShuo Chen 51cc808594SShuo Chen if (nread >= iphdr_size 52cc808594SShuo Chen && iphdr.version == 4 53cc808594SShuo Chen && iphdr.ihl*4 >= iphdr_size 54cc808594SShuo Chen && iphdr.ihl*4 <= nread 55cc808594SShuo Chen && iphdr.tot_len == htons(nread) 56cc808594SShuo Chen && in_checksum(buf, iphdr.ihl*4) == 0) 57cc808594SShuo Chen { 58cc808594SShuo Chen const void* payload = buf + iphdr.ihl*4; 59cc808594SShuo Chen if (iphdr.protocol == IPPROTO_ICMP) 60cc808594SShuo Chen { 61cc808594SShuo Chen icmp_input(fd, buf, payload, nread); 62cc808594SShuo Chen } 63cc808594SShuo Chen else if (iphdr.protocol == IPPROTO_TCP) 64cc808594SShuo Chen { 65cc808594SShuo Chen tcp_input(fd, buf, payload, nread); 66cc808594SShuo Chen } 67cc808594SShuo Chen } 68cc808594SShuo Chen else 69cc808594SShuo Chen { 70cc808594SShuo Chen printf("bad packet\n"); 71cc808594SShuo Chen for (int i = 0; i < nread; ++i) 72cc808594SShuo Chen { 73cc808594SShuo Chen if (i % 4 == 0) printf("\n"); 74cc808594SShuo Chen printf("%02x ", buf[i]); 75cc808594SShuo Chen } 76cc808594SShuo Chen printf("\n"); 77cc808594SShuo Chen } 78cc808594SShuo Chen } 79cc808594SShuo Chen 80cc808594SShuo Chen return 0; 81cc808594SShuo Chen} 82