From 2414450562696b45bd37b14530d5398d92a323dd Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Tue, 23 Oct 2012 21:16:14 +0000 Subject: Version qui compile sous Visual Studio 2005, CodeBlocks avec MinGW et Linux Ubuntu 12.04. J'ai carrément fait des fichiers includes par compliateur car ça faisait des tas de #if pourris. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///var/svn/2012-tzsp/trunk@14 147d2d3d-d0bd-48ea-923a-d90ac20f5906 --- Makefile | 2 +- README | 16 ++++ pcap2tzsp.c | 100 +++++------------------ pcap2tzsp.cbp | 64 +++++++++++++++ pcap2tzsp.h | 50 ++++++++++++ pcap2tzsp.vcproj | 213 ++++++++++++++++++++++++++++++++++++++++++++++++ pcap2tzsp_linux.h | 55 +++++++++++++ pcap2tzsp_win32_mingw.h | 57 +++++++++++++ pcap2tzsp_win32_vs.h | 73 +++++++++++++++++ 9 files changed, 548 insertions(+), 82 deletions(-) create mode 100644 README create mode 100644 pcap2tzsp.cbp create mode 100644 pcap2tzsp.h create mode 100644 pcap2tzsp.vcproj create mode 100644 pcap2tzsp_linux.h create mode 100644 pcap2tzsp_win32_mingw.h create mode 100644 pcap2tzsp_win32_vs.h diff --git a/Makefile b/Makefile index c9d7637..e16310a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all : pcap2tzsp pcap2tzsp: pcap2tzsp.o - gcc -o pcap2tzsp pcap2tzsp.o -lpcap + gcc -Wall -o pcap2tzsp pcap2tzsp.o -lpcap pcap2tzsp.o : pcap2tzsp.c gcc -Wall -c pcap2tzsp.c clean : diff --git a/README b/README new file mode 100644 index 0000000..3803e82 --- /dev/null +++ b/README @@ -0,0 +1,16 @@ + +Files description : + +LICENCE GPL 3 licence text +README This file + +Makefile Compliation directives for linux +pcap2tzsp.cbp Project file for Code-Blocks (I use the 10.05 release) +pcap2tzsp.vcproj Project file for Visual Studio (Express) 2005 (aka version 8) + +pcap2tzsp.c Main file with all the code +pcap2tzsp.h Header included by pcap2tzsp.c. Includes the right pcap2tzsp_*.h detecting compilation env. +pcap2tzsp_linux.h Specific header for pcap2tzsp.c library headers on Linux platform +pcap2tzsp_win32_mingw.h Specific header for pcap2tzsp.c library headers on Windows platform using MinGW32 +pcap2tzsp_win32_vs.h Specific header for pcap2tzsp.c library headers on Windows platform using Visual Studio + 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 -#include -#include /* Consider using msinttypes for Visual Studio */ -#include -#include - -#ifdef WIN32 /* snprintf compatiblity */ -#define my_snprintf sprintf_s -#else -#define my_snprintf snprintf -#endif - - -/* Packet capture stuff */ -#include - -/* UDP stuff */ -#ifdef WIN32 -#include -#include -#else -#include -#include -#include -#include /* Name resolution */ -#define SOCKET int -#define INVALID_SOCKET -1 -#endif - -/* Time management (for bwlimit) */ -#include - -/* Args parsing and basename() for usage */ -#ifdef WIN32 -#undef _UNICODE -#include /* 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 -#include -#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 ] [-h ] [-p ] [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 */ diff --git a/pcap2tzsp.cbp b/pcap2tzsp.cbp new file mode 100644 index 0000000..f638dca --- /dev/null +++ b/pcap2tzsp.cbp @@ -0,0 +1,64 @@ + + + + + + diff --git a/pcap2tzsp.h b/pcap2tzsp.h new file mode 100644 index 0000000..80b5c94 --- /dev/null +++ b/pcap2tzsp.h @@ -0,0 +1,50 @@ +/* + + Pcap2tzsp allows to capture ethernet trafic and send all headers + to a defined host using the TaZmen Sniffing Protocol (TZSP). + + Copyright (C) 2012 Ludovic Pouzenc + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + + This file is part of Pcap2tzsp. See LICENCE file. +*/ + + +#ifndef PCAP2TZSP_H_INCLUDED +#define PCAP2TZSP_H_INCLUDED + + +/* Get the right include file */ +#if ( defined(WIN32) && defined(_MSC_VER) ) +/* Visual studio case */ +#include "pcap2tzsp_win32_vs.h" + +#elif ( defined(WIN32) && defined(__MINGW32__) ) +/* MinGW on Windows case*/ +#include "pcap2tzsp_win32_mingw.h" + +#elif ( defined(WIN32) ) +/* TODO */ +#pragma message("You are in an unspecifed case.") + +#else +/* Linux case */ +#include "pcap2tzsp_linux.h" +#endif + + +#endif // PCAP2TZSP_H_INCLUDED + diff --git a/pcap2tzsp.vcproj b/pcap2tzsp.vcproj new file mode 100644 index 0000000..16915c0 --- /dev/null +++ b/pcap2tzsp.vcproj @@ -0,0 +1,213 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pcap2tzsp_linux.h b/pcap2tzsp_linux.h new file mode 100644 index 0000000..6090929 --- /dev/null +++ b/pcap2tzsp_linux.h @@ -0,0 +1,55 @@ +/* + + Pcap2tzsp allows to capture ethernet trafic and send all headers + to a defined host using the TaZmen Sniffing Protocol (TZSP). + + Copyright (C) 2012 Ludovic Pouzenc + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + + This file is part of Pcap2tzsp. See LICENCE file. +*/ + +#ifndef PCAP2TZSP_LINUX_H_INCLUDED +#define PCAP2TZSP_LINUX_H_INCLUDED + +/* Basics */ +#include +#include +#include +#include +#include +#define my_snprintf snprintf/* snprintf "compatiblity" */ + +/* Packet capture stuff */ +#include + +/* UDP stuff */ +#include +#include +#include +#include /* Name resolution */ +#define SOCKET int +#define INVALID_SOCKET -1 + +/* Time management (for bwlimit) */ +#include + +/* Args parsing and basename() for usage */ +#include +#include + + +#endif // PCAP2TZSP_LINUX_H_INCLUDED diff --git a/pcap2tzsp_win32_mingw.h b/pcap2tzsp_win32_mingw.h new file mode 100644 index 0000000..3d8da83 --- /dev/null +++ b/pcap2tzsp_win32_mingw.h @@ -0,0 +1,57 @@ +/* + + Pcap2tzsp allows to capture ethernet trafic and send all headers + to a defined host using the TaZmen Sniffing Protocol (TZSP). + + Copyright (C) 2012 Ludovic Pouzenc + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + + This file is part of Pcap2tzsp. See LICENCE file. +*/ + +#ifndef PCAP2TZSP_WIN32_MINGW_H_INCLUDED +#define PCAP2TZSP_WIN32_MINGW_H_INCLUDED + +#define WINVER 0x0501 + +/* Basics */ +#include +#include +#include +#include +#include +#define my_snprintf snprintf/* snprintf "compatiblity" */ + +/* Packet capture stuff */ +#include + +/* UDP stuff */ +#include +#include +/* Constants missing in MINGW +(I don't understand why and this will cause troubles) */ +#define AI_NUMERICSERV 0x00000008 +#define AI_ADDRCONFIG 0x00000400 +#define AI_V4MAPPED 0x00000800 + +/* Time management (for bwlimit) */ +#include + +/* Args parsing and basename() for usage */ +#include +#include + +#endif // PCAP2TZSP_WIN32_MINGW_H_INCLUDED diff --git a/pcap2tzsp_win32_vs.h b/pcap2tzsp_win32_vs.h new file mode 100644 index 0000000..92d187e --- /dev/null +++ b/pcap2tzsp_win32_vs.h @@ -0,0 +1,73 @@ +/* + + Pcap2tzsp allows to capture ethernet trafic and send all headers + to a defined host using the TaZmen Sniffing Protocol (TZSP). + + Copyright (C) 2012 Ludovic Pouzenc + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + + This file is part of Pcap2tzsp. See LICENCE file. +*/ + +#ifndef PCAP2TZSP_WIN32_VS_H_INCLUDED +#define PCAP2TZSP_WIN32_VS_H_INCLUDED + +/* Basics */ +#include +#include +#include /* Consider using msinttypes for Visual Studio */ +#include +#include +#define my_snprintf sprintf_s /* snprintf "compatiblity" */ + +/* Packet capture stuff */ +#include + +/* UDP stuff */ +#include +#include + +/* Time management (for bwlimit) */ +#include + +/* Args parsing and basename() for usage */ +#undef _UNICODE +#include /* 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; +} + +#endif // PCAP2TZSP_WIN32_VS_H_INCLUDED -- cgit v1.2.3