diff options
author | Kristian Lyngstol <kly@kly.no> | 2016-02-26 13:05:31 +0000 |
---|---|---|
committer | Kristian Lyngstol <kly@kly.no> | 2016-02-26 13:05:31 +0000 |
commit | 885156ee6a26ed047bba3f90541eaab92b65d758 (patch) | |
tree | e8a70cd6bca09641efac0c64c4c20d27efab2bda /examples/historical/mbd/derpspan.c | |
parent | c6997a4810e09619e9018c91d163f3f38b17212c (diff) | |
parent | 3dae75bde90aecc0cef2e3496f3565dcb3eeec0c (diff) |
Merge branch 'master' of github.com:tech-server/tgmanage
Diffstat (limited to 'examples/historical/mbd/derpspan.c')
-rw-r--r-- | examples/historical/mbd/derpspan.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/historical/mbd/derpspan.c b/examples/historical/mbd/derpspan.c new file mode 100644 index 0000000..b9fb362 --- /dev/null +++ b/examples/historical/mbd/derpspan.c @@ -0,0 +1,48 @@ +// gcc -O2 -o derspan derspan.c -lpcap -std=gnu99 -Wall + +#include <pcap.h> +#include <stdlib.h> +#include <netinet/ip.h> +#include <stdint.h> +#include <stdio.h> + +int rawsock; + +void my_callback(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) +{ + int len = h->caplen; + if (len < 40) { + //printf("skipped short packet\n"); + return; + } + if (bytes[14] != 0x88 || bytes[15] != 0xbe) { + //printf("skipped non-ethernet packet\n"); + return; + } + if (bytes[36] != 0x08 || bytes[37] != 0x00) { + //printf("skipped non-IPv4 packet\n"); + return; + } + + struct sockaddr_in self; + self.sin_family = AF_INET; + self.sin_addr.s_addr = htonl(0x7f000001); // localhost + self.sin_port = htons(1337); + + sendto(rawsock, bytes + 38, len - 38, 0, (struct sockaddr *)&self, sizeof(self)); +} + +int main(int argc, char **argv) +{ + rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + if (rawsock == -1) { + perror("socket"); + exit(0); + } + + pcap_t *pcap = pcap_open_live(argv[1], 1500, 1, 1000, NULL); + pcap_activate(pcap); + pcap_loop(pcap, -1, my_callback, NULL); + return 0; +} + |