Skip to content

Commit

Permalink
Fix IPv6 addresses formatting for src_ipn and dst_ipn attributes …
Browse files Browse the repository at this point in the history
…in report.xml
  • Loading branch information
darkk committed Dec 17, 2015
1 parent be23d2d commit f8dfa44
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
Empty file modified bootstrap.sh 100644 → 100755
Empty file.
17 changes: 17 additions & 0 deletions src/inet_ntop.h
@@ -0,0 +1,17 @@
#ifndef TCPFLOW_INET_NTOP_H
#define TCPFLOW_INET_NTOP_H

#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#ifndef HAVE_INET_NTOP
const char *inet_ntop(int af, const void *src,char *dst, socklen_t size);
#endif

#if defined(__MINGW32__)
// <ws2tcpip.h> has this prototype for ws2_32 dll, but has type-conflicts with winsock2.h
WINSOCK_API_LINKAGE LPCWSTR WSAAPI inet_ntop(INT Family, PVOID pAddr, LPWSTR pStringBuf, size_t StringBufSIze);
#endif

#endif
9 changes: 1 addition & 8 deletions src/tcpflow.h
Expand Up @@ -259,14 +259,7 @@ typedef unsigned char u_int8_t;
/* tcpflow.cpp - CLI */
extern const char *progname;
void terminate(int sig) __attribute__ ((__noreturn__));
#ifndef HAVE_INET_NTOP
const char *inet_ntop(int af, const void *src,char *dst, socklen_t size);
#endif

#if defined(__MINGW32__)
// <ws2tcpip.h> has this prototype for ws2_32 dll, but has type-conflicts with winsock2.h
WINSOCK_API_LINKAGE LPCWSTR WSAAPI inet_ntop(INT Family, PVOID pAddr, LPWSTR pStringBuf, size_t StringBufSIze);
#endif
#include "inet_ntop.h"

#ifdef HAVE_PTHREAD
#include <semaphore.h>
Expand Down
4 changes: 2 additions & 2 deletions src/tcpip.cpp
Expand Up @@ -75,8 +75,8 @@ void tcpip::dump_xml(class dfxml_writer *xreport,const std::string &xmladd)
std::stringstream attrs;
attrs << "startime='" << dfxml_writer::to8601(myflow.tstart) << "' ";
attrs << "endtime='" << dfxml_writer::to8601(myflow.tlast) << "' ";
attrs << "src_ipn='" << myflow.src << "' ";
attrs << "dst_ipn='" << myflow.dst << "' ";
attrs << "src_ipn='" << ipaddr_prn(myflow.src, myflow.family) << "' ";
attrs << "dst_ipn='" << ipaddr_prn(myflow.dst, myflow.family) << "' ";
if(myflow.has_mac_daddr()) attrs << "mac_daddr='" << macaddr(myflow.mac_daddr) << "' ";
if(myflow.has_mac_saddr()) attrs << "mac_saddr='" << macaddr(myflow.mac_saddr) << "' ";
attrs << "packets='" << myflow.packet_count << "' ";
Expand Down
20 changes: 16 additions & 4 deletions src/tcpip.h
Expand Up @@ -3,6 +3,8 @@

#include <fstream>

#include "inet_ntop.h"

/** On windows, there is no in_addr_t; this is from
* /usr/include/netinet/in.h
*/
Expand Down Expand Up @@ -54,9 +56,19 @@ public:;
inline bool operator < (const ipaddr &b) const { return memcmp(this->addr,b.addr,sizeof(this->addr))<0; }
};

inline std::ostream & operator <<(std::ostream &os,const ipaddr &b) {
os << (int)b.addr[0] <<"."<<(int)b.addr[1] << "."
<< (int)b.addr[2] << "." << (int)b.addr[3];
class ipaddr_prn {
public:
const ipaddr& ia;
const sa_family_t family;
ipaddr_prn(const ipaddr& ia_, sa_family_t family_)
: ia(ia_), family(family_)
{ }
};

inline std::ostream & operator <<(std::ostream &os, const ipaddr_prn &b) {
char buf[INET6_ADDRSTRLEN];
inet_ntop(b.family, b.ia.addr, buf, sizeof(buf));
os << buf;
return os;
}

Expand Down Expand Up @@ -125,7 +137,7 @@ class flow_addr {

std::string str() const {
std::stringstream s;
s << "flow[" << src << ":" << sport << "->" << dst << ":" << dport << "]";
s << "flow[" << ipaddr_prn(src, family) << ":" << sport << "->" << ipaddr_prn(dst, family) << ":" << dport << "]";
return s.str();
}
};
Expand Down

0 comments on commit f8dfa44

Please sign in to comment.