diff options
Diffstat (limited to 'pcap2tzsp.c')
-rw-r--r-- | pcap2tzsp.c | 100 |
1 files changed, 19 insertions, 81 deletions
diff --git a/pcap2tzsp.c b/pcap2tzsp.c index bf175ea..16b6e56 100644 --- a/pcap2tzsp.c +++ b/pcap2tzsp.c @@ -22,70 +22,7 @@ This file is part of Pcap2tzsp. See LICENCE file. */ -/* Basics */ -#include <stdio.h> -#include <stdlib.h> -#include <stdint.h> /* Consider using msinttypes for Visual Studio */ -#include <signal.h> -#include <string.h> - -#ifdef WIN32 /* snprintf compatiblity */ -#define my_snprintf sprintf_s -#else -#define my_snprintf snprintf -#endif - - -/* Packet capture stuff */ -#include <pcap.h> - -/* UDP stuff */ -#ifdef WIN32 -#include <winsock2.h> -#include <ws2tcpip.h> -#else -#include <sys/types.h> -#include <sys/socket.h> -#include <unistd.h> -#include <netdb.h> /* Name resolution */ -#define SOCKET int -#define INVALID_SOCKET -1 -#endif - -/* Time management (for bwlimit) */ -#include <time.h> - -/* Args parsing and basename() for usage */ -#ifdef WIN32 -#undef _UNICODE -#include <getopt.h> /* Consider using getopt4win for WIN32 */ - - /* Poor's men hand-written basename. - With calloc() and this is diffent from POSIX basename() */ - char * basename(const char *path) { - errno_t e; - size_t len1, len2; - char filename[256], ext[8]; - char *res=NULL; - - e=_splitpath_s(path, NULL, 0, NULL, 0, filename, 256, ext, 8); - if (e != 0) { - res=strdup(""); - } else { - len1=strlen(filename); - len2=strlen(ext); - res=calloc(len1+len2 + 1, sizeof(char)); - if ( res != NULL ) { - strcpy_s(res, len1+1, filename); - strcpy_s(res+len1, len2+1, ext); - } - } - return res; - } -#else -#include <getopt.h> -#include <libgen.h> -#endif +#include "pcap2tzsp.h" /* Option default values (as strings) */ #define DEFAULT_FILTER "not ( icmp[icmptype]=icmp-unreach or (udp and dst %s and port %s) )" @@ -93,8 +30,8 @@ #define DEFAULT_PORT "37008" #define DEFAULT_SNAPLEN "70" -/* MAX_TZSP_PAYLOAD is for not trying to send TZSP packets greater than the interface MTU - MAX_TZSP_PAYLOAD = MTU - IP header - UDP header - TZSP header +/* MAX_TZSP_PAYLOAD is for not trying to send TZSP packets greater than the interface MTU + MAX_TZSP_PAYLOAD = MTU - IP header - UDP header - TZSP header I am assuming the following : - Layer 2 overhead is the same for the captured packet and the transmitted packet - MTU is assumed to be 1500 for now @@ -109,13 +46,14 @@ /* TODO List + * Essayer WIN32_LEAN_AND_MEAN avec VS * Implémenter une bwlimit en sortie (ptks/s et/ou bytes/s) * Calcul dynamique de MAX_TZSP_PAYLOAD */ /* Functions declaration */ -int make_dgram_socket(char host[], char service[], int hint_flags, char **resolved_address); -void start_capture_loop(char *pcap_filter); +SOCKET make_dgram_socket(char host[], char service[], int hint_flags, char **resolved_address); +void start_capture_loop(char *pcap_filter); void process_packet(u_char *void_args, const struct pcap_pkthdr* pkthdr, const u_char * packet); /* Custom types definition */ @@ -135,7 +73,7 @@ static char *opt_host=NULL; static char *opt_port=NULL; static char *opt_snaplen=NULL; -pcap_t *pcap_handle = NULL; +pcap_t *pcap_handle = NULL; void usage(char progname[]) { printf("Usage : %s [--verbose] [--brief] [-i <iface>] [-h <host>] [-p <port>] [custom_pcap_filter]\n", progname); @@ -278,7 +216,7 @@ int main(int argc, char *argv[]) { return 0; } -int make_dgram_socket(char host[], char service[], int hint_flags, char **resolved_address) { +SOCKET make_dgram_socket(char host[], char service[], int hint_flags, char **resolved_address) { /* Code taken from Client program example of getaddrinfo(3) manpage Tweaks added for WIN32 "compatibility" @@ -331,7 +269,7 @@ int make_dgram_socket(char host[], char service[], int hint_flags, char **resolv /* This part was not in the example as is */ if ( resolved_address != NULL) { /* If wanted, return numerical form of the choosen host address */ - if ( getnameinfo(rp->ai_addr, rp->ai_addrlen, hbuf, + if ( getnameinfo(rp->ai_addr, rp->ai_addrlen, hbuf, NI_MAXHOST_NUMERIC, NULL, 0, NI_NUMERICHOST) == 0 ) { *resolved_address=strdup(hbuf); } @@ -342,10 +280,10 @@ int make_dgram_socket(char host[], char service[], int hint_flags, char **resolv } -void start_capture_loop(char *pcap_filter) { +void start_capture_loop(char *pcap_filter) { //Global for signal handling - //pcap_t *pcap_handle = NULL; + //pcap_t *pcap_handle = NULL; char *resolved_address=NULL; static char *pcap_device=NULL; char pcap_errbuf[PCAP_ERRBUF_SIZE]; @@ -355,11 +293,11 @@ void start_capture_loop(char *pcap_filter) { int snaplen, hints_flags, pcap_filter_synthetised=0; size_t pcap_filter_len; /* size_t is "mandatory" for WIN32 */ - memset(pcap_errbuf,0,PCAP_ERRBUF_SIZE); - memset(&process_packet_args,0,sizeof(process_packet_args_t)); + memset(pcap_errbuf,0,PCAP_ERRBUF_SIZE); + memset(&process_packet_args,0,sizeof(process_packet_args_t)); if (opt_iface == NULL) { - /* Get the name of the first device suitable for capture */ + /* Get the name of the first device suitable for capture */ if ( (pcap_device = pcap_lookupdev(pcap_errbuf)) == NULL){ fprintf(stderr, "ERROR: %s\n", pcap_errbuf); exit(20); @@ -387,7 +325,7 @@ void start_capture_loop(char *pcap_filter) { /* pcap init */ if (opt_verbose) printf("Opening device %s for capturing\n", pcap_device); - /* Open device in promiscuous mode */ + /* Open device in promiscuous mode */ if ( (pcap_handle = pcap_open_live(pcap_device, snaplen, 1, 512, pcap_errbuf)) == NULL){ fprintf(stderr, "ERROR: %s\n", pcap_errbuf); exit(21); @@ -419,7 +357,7 @@ void start_capture_loop(char *pcap_filter) { pcap_freecode(&pcap_filter_prog); - /* Loop forever & call process_packet() for every received packet */ + /* Loop forever & call process_packet() for every received packet */ if ( pcap_loop(pcap_handle, -1, process_packet, (u_char *)&process_packet_args) == -1) { fprintf(stderr, "ERROR: %s\n", pcap_geterr(pcap_handle) ); exit(25); @@ -427,7 +365,7 @@ void start_capture_loop(char *pcap_filter) { fprintf(stderr, "\n%u packets captured\n%u TZSP packets sent\n", process_packet_args.captured_pkt_count, process_packet_args.sent_pkt_count); - + if (pcap_stats(pcap_handle, &stat) == -1) { fprintf(stderr, "ERROR: %s\n", pcap_geterr(pcap_handle) ); exit(26); @@ -460,7 +398,7 @@ void process_packet(u_char *void_args, const struct pcap_pkthdr* pkthdr, const u char buf[UDP_SEND_BUFLEN]; /* struct timespec ts_reqsleep; // For simulation */ - process_packet_args_t *args=(process_packet_args_t *)void_args; + process_packet_args_t *args=(process_packet_args_t *)void_args; /* Catpured packet counting and displaying (max once by second) */ args->captured_pkt_count++; @@ -495,7 +433,7 @@ void process_packet(u_char *void_args, const struct pcap_pkthdr* pkthdr, const u buf[5]=0x04; /* Tag length == 4 bytes */ /* buf[6,7,8,9] Timestamp on 4 bytes (network order) */ - memcpy(buf+6, &field_ts, 4); + memcpy(buf+6, &field_ts, 4); /* Wireshark don't dissect that */ //buf[10]=0x28; /* Tag type TAG_PACKET_COUNT */ |