|
From: <Xb...@us...> - 2012-04-17 14:29:16
|
Revision: 1384
http://scstudio.svn.sourceforge.net/scstudio/?rev=1384&view=rev
Author: Xborza
Date: 2012-04-17 14:29:06 +0000 (Tue, 17 Apr 2012)
Log Message:
-----------
First version of pcap implementation
Modified Paths:
--------------
trunk/src/data/pcap/CMakeLists.txt
trunk/src/data/pcap/pcap_load.cpp
trunk/src/data/pcap/pcap_load.h
trunk/tests/pcap/CMakeLists.txt
trunk/tests/pcap/pcap_test.cpp
Modified: trunk/src/data/pcap/CMakeLists.txt
===================================================================
--- trunk/src/data/pcap/CMakeLists.txt 2012-04-17 13:57:19 UTC (rev 1383)
+++ trunk/src/data/pcap/CMakeLists.txt 2012-04-17 14:29:06 UTC (rev 1384)
@@ -4,6 +4,8 @@
ADD_LIBRARY(scpcap SHARED
export.h
+ pcap_struct.h
+ pcap_struct.cpp
module.cpp
pcap_load.h
pcap_load.cpp
@@ -17,5 +19,6 @@
TARGET_LINK_LIBRARIES(scpcap
scmsc
+ ws2_32
${PCAP_LIBRARY}
)
Modified: trunk/src/data/pcap/pcap_load.cpp
===================================================================
--- trunk/src/data/pcap/pcap_load.cpp 2012-04-17 13:57:19 UTC (rev 1383)
+++ trunk/src/data/pcap/pcap_load.cpp 2012-04-17 14:29:06 UTC (rev 1384)
@@ -19,55 +19,142 @@
// include pcap library
// under Windows WinPcap Developer's Pack http://www.winpcap.org/devel.htm
// under UN*X libpcap http://www.tcpdump.org
-#include <pcap.h>
#include "pcap_load.h"
+
std::vector<MscPtr> Pcap::load_msc(const std::string &filename)
{
- std::vector<MscPtr> result;
- int res;
- MscPtr msc = new BMsc(L"name");
+std::vector<MscPtr> result;
- std::cout << "Hello, Here I am!!! at PCAP load function" << std::endl;
- std::wcout << msc->get_label() << std::endl;
- return result;
+pcap_if_t *alldevs;
+pcap_if_t *d;
+int inum;
+int i=0;
+pcap_t *adhandle;
+char errbuf[PCAP_ERRBUF_SIZE];
+u_int netmask;
+char packet_filter[] = "(ip) and (tcp)";
+struct bpf_program fcode;
- pcap_t *fp;
- char errbuf[PCAP_ERRBUF_SIZE];
- // open the capture file
- if((fp = pcap_open_offline(filename.c_str(), errbuf)) == NULL)
- {
- print_report(RS_ERROR,
- stringize() << "Cannot open file '" << TOWSTRING(filename) << "'.");
- return result;
- }
+ /* Retrieve the device list */
+ if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
+ {
+ fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
+ exit(1);
+ }
+
+ /* Print the list */
+ for(d=alldevs; d; d=d->next)
+ {
+ printf("%d. %s", ++i, d->name);
+ if (d->description)
+ printf(" (%s)\n", d->description);
+ else
+ printf(" (No description available)\n");
+ }
- struct pcap_pkthdr *header;
- const u_char *pkt_data;
+ if(i==0)
+ {
+ printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
+ return result;
+ }
+
+ printf("Enter the interface number (1-%d) or press 0 choose offline mode:",i);
+ scanf_s("%d", &inum);
+
+ if(inum < 0 || inum > i)
+ {
+ printf("\nInterface number out of range.\n");
+ /* Free the device list */
+ pcap_freealldevs(alldevs);
+ return result;
+ }
- // inspect all packets in the file
-/*
- while((res = pcap_next_ex(fp, &header, &pkt_data)) >= 0)
- {
- PacketDataUnit packet;
- packet.time_sent = packet.time_received =
- header->ts.tv_sec + header->ts.tv_usec / 1000000;
- packet.length = header->caplen;
- packet.data = pkt_data;
+ if(inum != 0){ // online mode
- inspect_ethernet(packet);
- }
-*/
- if(res == -1)
- {
- print_report(RS_ERROR,
- stringize() << "Error reading the packets: '" << pcap_geterr(fp));
- }
+ /* Jump to the selected adapter */
+ for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
+
+ /* Open the adapter */
+ if ( (adhandle= pcap_open(d->name, // name of the device
+ 65536, // portion of the packet to capture.
+ // 65536 grants that the whole packet will be captured on all the MACs.
+ PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
+ 1000, // read timeout
+ NULL, // remote authentication
+ errbuf // error buffer
+ ) ) == NULL)
+ {
+ fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
+ /* Free the device list */
+ pcap_freealldevs(alldevs);
+ return result;
+ }
+ }
+ else{ // OPEN OFFLINE MODE
+ if((adhandle = pcap_open_offline(filename.c_str(),errbuf)) == NULL){
+ std::cout << "ERROR: Cannot open pcap file, wrong name!\n";
+ return result;
+ }
+ }
+
+ /* Check the link layer. We support only Ethernet for simplicity. */
+ if(pcap_datalink(adhandle) != DLT_EN10MB)
+ {
+ fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
+ /* Free the device list */
+ pcap_freealldevs(alldevs);
+ return result;
+ }
+
- pcap_close(fp);
- return result;
+ if(inum != 0 && d->addresses != NULL)
+ /* Retrieve the mask of the first address of the interface */
+ netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
+ else
+ /* If the interface is without addresses we suppose to be in a C class network */
+ netmask=0xffffff;
+
+
+ //compile the filter
+ if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
+ {
+ fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
+ /* Free the device list */
+ pcap_freealldevs(alldevs);
+ return result;
+ }
+
+ //set the filter
+ if (pcap_setfilter(adhandle, &fcode)<0)
+ {
+ fprintf(stderr,"\nError setting the filter.\n");
+ /* Free the device list */
+ pcap_freealldevs(alldevs);
+ return result;
+ }
+
+ printf("\nlistening on %s...\n", (inum == 0) ? "OFFLINE" : d->description);
+
+ /* At this point, we don't need any more the device list. Free it */
+ pcap_freealldevs(alldevs);
+
+ /* start the capture */
+
+ PcapContext* pcapCon;
+ pcapCon = new PcapContext();
+
+ handStruct hands;
+ hands.pcap = pcapCon;
+ hands.result = &result;
+
+ pcap_loop(adhandle, 0, packet_handler, (u_char*)&hands);
+
+ result.push_back(hands.pcap->getMsc());
+
+ return result;
}
ImportFormatter::TransformationList Pcap::get_transformations(MscPtr msc) const
@@ -77,4 +164,7 @@
return result;
}
+
+
+
// $Id$
Modified: trunk/src/data/pcap/pcap_load.h
===================================================================
--- trunk/src/data/pcap/pcap_load.h 2012-04-17 13:57:19 UTC (rev 1383)
+++ trunk/src/data/pcap/pcap_load.h 2012-04-17 14:29:06 UTC (rev 1384)
@@ -20,14 +20,21 @@
#define _SCPCAP_PCAP_IMPORT_H_
#ifdef WIN32
-#include <windows.h>
#define uint16_t UINT16
#define uint32_t UINT32
+#include <WinSock2.h>
+#include <Windows.h>
#endif
-#include "data/formatter.h"
+//#include "pcap_struct.h"
+#include <stdio.h>
+
+#define HAVE_REMOTE
+
+#include "pcap_struct.h"
#include "data/pcap/export.h"
+
class SCPCAP_EXPORT Pcap : public Formatter, public ImportFormatter
{
public:
@@ -61,6 +68,6 @@
int inspect_udp(const PacketDataUnit& packet);
};
-#endif /* _SCPCAP_PCAP_IMPORT_H_ */
+#endif _SCPCAP_PCAP_IMPORT_H_
// $Id$
Modified: trunk/tests/pcap/CMakeLists.txt
===================================================================
--- trunk/tests/pcap/CMakeLists.txt 2012-04-17 13:57:19 UTC (rev 1383)
+++ trunk/tests/pcap/CMakeLists.txt 2012-04-17 14:29:06 UTC (rev 1384)
@@ -4,6 +4,8 @@
pcap_test.cpp
)
+INCLUDE_DIRECTORIES(${PCAP_INCLUDE_DIR})
+
TARGET_LINK_LIBRARIES(pcap_test
scpcap
scpseudocode
Modified: trunk/tests/pcap/pcap_test.cpp
===================================================================
--- trunk/tests/pcap/pcap_test.cpp 2012-04-17 13:57:19 UTC (rev 1383)
+++ trunk/tests/pcap/pcap_test.cpp 2012-04-17 14:29:06 UTC (rev 1384)
@@ -15,9 +15,11 @@
*
* $Id: z120_test.cpp 1274 2012-01-10 15:49:30Z lkorenciak $
*/
+#define _WINSOCKAPI_
#include <string.h>
#include <iostream>
+#include <fstream>
#include "data/Z120/z120.h"
#include "data/pcap/pcap_load.h"
@@ -43,12 +45,13 @@
int main(int argc, char** argv)
{
- if(argc < 3)
+ if(argc < 4)
{
- std::wcerr << "Usage: " << argv[0] << " <filename> <satisfied>" << std::endl;
+ std::wcerr << "Usage: " << argv[0] << " <(pcap)filename> <(msc)filename> <satisfied>" << std::endl;
return 1;
}
+ std::ofstream outFile;
Pcap pcap;
Z120 z120;
@@ -58,8 +61,8 @@
int errors = 0;
char *endptr;
- int satisfied = strtol(argv[2], &endptr, 10);
- if(*argv[2] == '\0' || *endptr != '\0')
+ int satisfied = strtol(argv[3], &endptr, 10);
+ if(*argv[3] == '\0' || *endptr != '\0')
{
std::wcerr << "ERROR: Not a boolean value: " << argv[2] << std::endl;
return 1;
@@ -67,9 +70,16 @@
std::vector<MscPtr> msc = pcap.load_msc(argv[1]);
- char *path = strdup(argv[1]);
+ char *path = _strdup(argv[1]);
char *filename = extract_filename(path);
+ char *path2 = _strdup(argv[2]);
+ std::string outmsc;
+ outmsc.append(argv[2]);
+ outmsc.append(".mpr");
+ outFile.open(outmsc);
+
+
if(!msc.empty())
{
if(satisfied)
@@ -84,7 +94,7 @@
try
{
- z120.save_msc(std::cout, TOWSTRING(filename), msc[0], msc);
+ z120.save_msc(outFile, TOWSTRING(filename), msc[0], msc);
}
catch(std::exception& exc)
{
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|