convert some functions using sockaddr_*() helpers to be address
family agnostic.
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/itrafmon.c | 42 ++++++++----------
src/othptab.c | 40 +++++++----------
src/othptab.h | 12 ++---
src/revname.c | 38 +++-------------
src/revname.h | 2 +-
src/rvnamed.c | 97 ++++++++----------------------------------
src/rvnamed.h | 3 +-
src/tcptable.c | 129 +++++++++++++++++++++++++-------------------------------
src/tcptable.h | 24 +++++-----
9 files changed, 138 insertions(+), 249 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index 4c64cac..d28d988 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -32,6 +32,7 @@ itrafmon.c - the IP traffic monitor module
#include "instances.h"
#include "logvars.h"
#include "itrafmon.h"
+#include "sockaddr.h"
#define SCROLLUP 0
#define SCROLLDOWN 1
@@ -1011,16 +1012,21 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
ifname = ifnamebuf;
}
+ struct sockaddr_storage saddr, daddr;
switch(pkt.pkt_protocol) {
case ETH_P_IP:
frag_off = pkt.iphdr->frag_off;
+ sockaddr_make_ipv4(&saddr, pkt.iphdr->saddr);
+ sockaddr_make_ipv4(&daddr, pkt.iphdr->daddr);
break;
case ETH_P_IPV6:
frag_off = 0;
+ sockaddr_make_ipv6(&saddr, &pkt.ip6_hdr->ip6_src);
+ sockaddr_make_ipv6(&daddr, &pkt.ip6_hdr->ip6_dst);
break;
default:
- add_othp_entry(&othptbl, &pkt, 0, 0, NULL,
- NULL, NOT_IP,
+ add_othp_entry(&othptbl, &pkt, NULL, NULL,
+ NOT_IP,
pkt.pkt_protocol,
pkt.pkt_payload, ifname, 0,
0, logging, logfile,
@@ -1036,21 +1042,14 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
if (ip_protocol == IPPROTO_TCP) {
if (pkt.iphdr) {
tcpentry =
- in_table(&table,
- pkt.iphdr->saddr,
- pkt.iphdr->daddr,
- NULL, NULL,
+ in_table(&table, &saddr, &daddr,
ntohs(sport),
ntohs(dport),
ifname, logging,
logfile, options);
} else {
tcpentry =
- in_table(&table, 0, 0,
- (uint8_t *) &pkt.ip6_hdr->
- ip6_src.s6_addr,
- (uint8_t *) &pkt.ip6_hdr->
- ip6_dst.s6_addr,
+ in_table(&table, &saddr, &daddr,
ntohs(sport), ntohs(dport),
ifname, logging, logfile, options);
}
@@ -1071,19 +1070,16 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
wasempty = (table.head == NULL);
if (pkt.iphdr)
tcpentry =
- addentry(&table,
- (unsigned long) pkt.iphdr->saddr,
- (unsigned long) pkt.iphdr->daddr,
- NULL, NULL, sport, dport,
+ addentry(&table, &saddr, &daddr,
+ sport, dport,
pkt_ip_protocol(&pkt),
ifname, &revlook, rvnfd,
options->servnames);
else
tcpentry =
- addentry(&table, 0, 0,
- (uint8_t *) &pkt.ip6_hdr->ip6_src.s6_addr,
- (uint8_t *) &pkt.ip6_hdr->ip6_dst.s6_addr,
- sport, dport, pkt_ip_protocol(&pkt),
+ addentry(&table, &saddr, &daddr,
+ sport, dport,
+ pkt_ip_protocol(&pkt),
ifname, &revlook, rvnfd,
options->servnames);
if (tcpentry != NULL) {
@@ -1192,9 +1188,8 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
process_dest_unreach(&table, (char *) transpacket,
ifname);
}
- add_othp_entry(&othptbl, &pkt, pkt.iphdr->saddr,
- pkt.iphdr->daddr, NULL, NULL, IS_IP,
- pkt_ip_protocol(&pkt),
+ add_othp_entry(&othptbl, &pkt, &saddr, &daddr,
+ IS_IP, pkt_ip_protocol(&pkt),
(char *) transpacket,
ifname, &revlook, rvnfd,
logging, logfile,
@@ -1206,8 +1201,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
process_dest_unreach(&table, (char *) transpacket,
ifname);
- add_othp_entry(&othptbl, &pkt, 0, 0,
- &pkt.ip6_hdr->ip6_src, &pkt.ip6_hdr->ip6_dst,
+ add_othp_entry(&othptbl, &pkt, &saddr, &daddr,
IS_IP, pkt_ip_protocol(&pkt),
(char *) transpacket, ifname,
&revlook, rvnfd,
diff --git a/src/othptab.c b/src/othptab.c
index 6b77336..592ee09 100644
--- a/src/othptab.c
+++ b/src/othptab.c
@@ -24,6 +24,7 @@ othptab.c - non-TCP protocol display module
#include "addproto.h"
#include "packet.h"
#include "hostmon.h"
+#include "sockaddr.h"
#define MSGSTRING_MAX 240
#define SHORTSTRING_MAX 40
@@ -141,18 +142,22 @@ void process_dest_unreach(struct tcptable *table, char *packet, char
*ifname)
if (ip6->ip6_nxt != IPPROTO_TCP)
return;
tcp = (struct tcphdr *) (packet + 48);
+ struct sockaddr_storage saddr, daddr;
+ sockaddr_make_ipv6(&saddr, &ip6->ip6_src);
+ sockaddr_make_ipv6(&daddr, &ip6->ip6_dst);
tcpentry =
- in_table(table, 0, 0, ip6->ip6_src.s6_addr,
- ip6->ip6_dst.s6_addr, ntohs(tcp->source),
+ in_table(table, &saddr, &daddr, ntohs(tcp->source),
ntohs(tcp->dest), ifname, 0, NULL, NULL);
} else {
if (ip->protocol != IPPROTO_TCP)
return;
tcp = (struct tcphdr *) (packet + 8 + (ip->ihl * 4));
+ struct sockaddr_storage saddr, daddr;
+ sockaddr_make_ipv4(&saddr, ip->saddr);
+ sockaddr_make_ipv4(&daddr, ip->daddr);
tcpentry =
- in_table(table, ip->saddr, ip->daddr, NULL, NULL,
- ntohs(tcp->source), ntohs(tcp->dest), ifname, 0,
- NULL, NULL);
+ in_table(table, &saddr, &daddr, ntohs(tcp->source),
+ ntohs(tcp->dest), ifname, 0, NULL, NULL);
}
if (tcpentry != NULL) {
@@ -162,9 +167,9 @@ void process_dest_unreach(struct tcptable *table, char *packet, char
*ifname)
}
struct othptabent *add_othp_entry(struct othptable *table, struct pkt_hdr *pkt,
- unsigned long saddr,
- unsigned long daddr, struct in6_addr *s6addr,
- struct in6_addr *d6addr, int is_ip,
+ struct sockaddr_storage *saddr,
+ struct sockaddr_storage *daddr,
+ int is_ip,
int protocol,
char *packet2,
char *ifname, int *rev_lookup, int rvnfd,
@@ -173,7 +178,6 @@ struct othptabent *add_othp_entry(struct othptable *table, struct
pkt_hdr *pkt,
{
struct othptabent *new_entry;
struct othptabent *temp;
- struct in_addr isaddr, idaddr;
new_entry = xmallocz(sizeof(struct othptabent));
@@ -191,22 +195,12 @@ struct othptabent *add_othp_entry(struct othptable *table, struct
pkt_hdr *pkt,
}
if (is_ip) {
- new_entry->saddr = isaddr.s_addr = saddr;
- new_entry->daddr = idaddr.s_addr = daddr;
-
- if (s6addr != NULL)
- memcpy(&new_entry->s6addr, s6addr, 16);
- else
- memset(&new_entry->s6addr, 0, 16);
-
- if (d6addr != NULL)
- memcpy(&new_entry->d6addr, d6addr, 16);
- else
- memset(&new_entry->s6addr, 0, 16);
+ sockaddr_copy(&new_entry->saddr, saddr);
+ sockaddr_copy(&new_entry->daddr, daddr);
- revname(rev_lookup, &isaddr, s6addr, new_entry->s_fqdn,
+ revname(rev_lookup, saddr, new_entry->s_fqdn,
sizeof(new_entry->s_fqdn), rvnfd);
- revname(rev_lookup, &idaddr, d6addr, new_entry->d_fqdn,
+ revname(rev_lookup, daddr, new_entry->d_fqdn,
sizeof(new_entry->d_fqdn), rvnfd);
if (!fragment) {
diff --git a/src/othptab.h b/src/othptab.h
index fbd9456..153d013 100644
--- a/src/othptab.h
+++ b/src/othptab.h
@@ -20,10 +20,8 @@ othptab.h - header file for the non-TCP routines
#define VSCRL_OFFSET 60 /* Characters to vertically scroll */
struct othptabent {
- unsigned long int saddr;
- unsigned long int daddr;
- struct in6_addr s6addr;
- struct in6_addr d6addr;
+ struct sockaddr_storage saddr;
+ struct sockaddr_storage daddr;
char smacaddr[18]; /* FIXME: use dynamicly allocated space */
char dmacaddr[18];
unsigned short linkproto;
@@ -124,9 +122,9 @@ void init_othp_table(struct othptable *table, int mac);
void process_dest_unreach(struct tcptable *table, char *packet, char *ifname);
struct othptabent *add_othp_entry(struct othptable *table, struct pkt_hdr *pkt,
- unsigned long saddr,
- unsigned long daddr, struct in6_addr *s6addr,
- struct in6_addr *d6addr, int is_ip,
+ struct sockaddr_storage *saddr,
+ struct sockaddr_storage *daddr,
+ int is_ip,
int protocol,
char *packet2,
char *ifname, int *rev_lookup, int rvnamedon,
diff --git a/src/revname.c b/src/revname.c
index 28a1b68..8188789 100644
--- a/src/revname.c
+++ b/src/revname.c
@@ -16,6 +16,7 @@ interim IP addresses in the meantime.
#include "getpath.h"
#include "revname.h"
#include "rvnamed.h"
+#include "sockaddr.h"
char revname_socket[80];
@@ -133,10 +134,9 @@ void close_rvn_socket(int fd)
}
}
-int revname(int *lookup, struct in_addr *saddr, struct in6_addr *s6addr,
+int revname(int *lookup, struct sockaddr_storage *addr,
char *target, size_t target_size, int rvnfd)
{
- struct hostent *he;
struct rvn rpkt;
int br;
struct sockaddr_un su;
@@ -152,13 +152,7 @@ int revname(int *lookup, struct in_addr *saddr, struct in6_addr
*s6addr,
strcpy(su.sun_path, IPTSOCKNAME);
rpkt.type = RVN_REQUEST;
- rpkt.saddr.s_addr = saddr->s_addr;
-
- if (s6addr != NULL)
- memcpy(rpkt.s6addr.s6_addr, s6addr->s6_addr,
- 16);
- else
- memset(rpkt.s6addr.s6_addr, 0, 4);
+ sockaddr_copy(&rpkt.addr, addr);
sendto(rvnfd, &rpkt, sizeof(struct rvn), 0,
(struct sockaddr *) &su,
@@ -188,10 +182,7 @@ int revname(int *lookup, struct in_addr *saddr, struct in6_addr
*s6addr,
} while ((br < 0) && (errno == EINTR));
if (br < 0) {
- if (saddr->s_addr != 0)
- inet_ntop(AF_INET, saddr, target, target_size - 1);
- else
- inet_ntop(AF_INET6, s6addr, target, target_size - 1);
+ sockaddr_ntop(addr, target, target_size);
printipcerr();
*lookup = 0;
return RESOLVED;
@@ -199,20 +190,9 @@ int revname(int *lookup, struct in_addr *saddr, struct in6_addr
*s6addr,
strncpy(target, rpkt.fqdn, target_size - 1);
return (rpkt.ready);
} else {
- if (saddr->s_addr != 0)
- he = gethostbyaddr((char *) saddr,
- sizeof(struct in_addr),
- AF_INET);
- else
- he = gethostbyaddr((char *) s6addr,
- sizeof(struct in6_addr),
- AF_INET6);
-
+ struct hostent *he = sockaddr_gethostbyaddr(addr);
if (he == NULL) {
- if (saddr->s_addr != 0)
- inet_ntop(AF_INET, saddr, target, target_size - 1);
- else
- inet_ntop(AF_INET6, s6addr, target, target_size - 1);
+ sockaddr_ntop(addr, target, target_size);
} else {
strncpy(target, he->h_name, target_size - 1);
}
@@ -220,11 +200,7 @@ int revname(int *lookup, struct in_addr *saddr, struct in6_addr
*s6addr,
return RESOLVED;
}
} else {
- if (saddr->s_addr != 0 || s6addr == NULL)
- inet_ntop(AF_INET, saddr, target, target_size - 1);
- else
- inet_ntop(AF_INET6, s6addr, target, target_size - 1);
-
+ sockaddr_ntop(addr, target, target_size);
return RESOLVED;
}
return NOTRESOLVED;
diff --git a/src/revname.h b/src/revname.h
index 9a994da..847d832 100644
--- a/src/revname.h
+++ b/src/revname.h
@@ -12,7 +12,7 @@ void killrvnamed(void);
void open_rvn_socket(int *fd);
void close_rvn_socket(int fd);
-int revname(int *lookup, struct in_addr *saddr, struct in6_addr *s6addr,
+int revname(int *lookup, struct sockaddr_storage *addr,
char *target, size_t target_size, int rvnfd);
#endif /* IPTRAF_NG_REVNAME_H */
diff --git a/src/rvnamed.c b/src/rvnamed.c
index a663b85..2ad1fd4 100644
--- a/src/rvnamed.c
+++ b/src/rvnamed.c
@@ -42,6 +42,7 @@ socket protocol.
#include <sys/wait.h>
#include "rvnamed.h"
#include "dirs.h"
+#include "sockaddr.h"
#define NUM_CACHE_ENTRIES 2048
#define TIME_TARGET_MAX 30
@@ -49,8 +50,7 @@ socket protocol.
#define __unused __attribute__((unused))
struct hosts {
- unsigned long addr;
- uint8_t addr6[16];
+ struct sockaddr_storage addr;
char fqdn[45];
int ready;
};
@@ -88,23 +88,14 @@ static void process_rvn_packet(struct rvn *rvnpacket)
ccfd = socket(PF_UNIX, SOCK_DGRAM, 0);
- if (rvnpacket->saddr.s_addr != 0)
- he = gethostbyaddr((char *) &(rvnpacket->saddr),
- sizeof(struct in_addr), AF_INET);
- else
- he = gethostbyaddr((char *) &(rvnpacket->s6addr),
- sizeof(struct in6_addr), AF_INET6);
-
+ he = sockaddr_gethostbyaddr(&rvnpacket->addr);
if (he == NULL) {
- if (rvnpacket->saddr.s_addr != 0)
- inet_ntop(AF_INET, &rvnpacket->saddr,
- rvnpacket->fqdn, sizeof(rvnpacket->fqdn));
- else
- inet_ntop(AF_INET6, &(rvnpacket->s6addr),
- rvnpacket->fqdn, sizeof(rvnpacket->fqdn));
+ sockaddr_ntop(&rvnpacket->addr, rvnpacket->fqdn,
+ sizeof(rvnpacket->fqdn));
} else {
memset(rvnpacket->fqdn, 0, sizeof(rvnpacket->fqdn));
- strncpy(rvnpacket->fqdn, he->h_name, sizeof(rvnpacket->fqdn)-1);
+ strncpy(rvnpacket->fqdn, he->h_name,
+ sizeof(rvnpacket->fqdn) - 1);
}
ccsa.sun_family = AF_UNIX;
@@ -124,17 +115,9 @@ static int name_resolved(struct rvn *rvnpacket, struct hosts
*hostlist,
unsigned int lastfree)
{
for (unsigned int i = 0; i != lastfree; i++)
- if (hostlist[i].ready == RESOLVED) {
- if (rvnpacket->saddr.s_addr != 0) {
- if (rvnpacket->saddr.s_addr == hostlist[i].addr)
- return i;
- } else {
- if (!memcmp(rvnpacket->s6addr.s6_addr,
- hostlist[i].addr6,
- sizeof(hostlist[i].addr6)))
- return i;
- }
- }
+ if ((hostlist[i].ready == RESOLVED)
+ && sockaddr_is_equal(&rvnpacket->addr, &hostlist[i].addr))
+ return i;
return -1;
}
@@ -147,15 +130,10 @@ static int name_resolved(struct rvn *rvnpacket, struct hosts
*hostlist,
static int addrstat(struct rvn *rvnpacket, struct hosts *hostlist,
unsigned int lastfree)
{
- for (unsigned int i = 0; i != lastfree; i++) {
- if (rvnpacket->saddr.s_addr != 0) {
- if (rvnpacket->saddr.s_addr == hostlist[i].addr)
- return hostlist[i].ready;
- } else if (!memcmp(&rvnpacket->s6addr.s6_addr,
- &hostlist[i].addr6,
- sizeof(hostlist[i].addr6)))
+ for (unsigned int i = 0; i != lastfree; i++)
+ if (sockaddr_is_equal(&rvnpacket->addr, &hostlist[i].addr))
return hostlist[i].ready;
- }
+
return NOTRESOLVED;
}
@@ -302,12 +280,7 @@ int main(void)
hi = 0;
while (hi <= lastfree) {
- if ((hostlist[hi].addr ==
- rvnpacket.saddr.s_addr)
- && !memcmp(rvnpacket.s6addr.s6_addr,
- hostlist[hi].addr6,
- sizeof(hostlist[hi].
- addr6)))
+ if (sockaddr_is_equal(&hostlist[hi].addr, &rvnpacket.addr))
break;
hi++;
}
@@ -320,13 +293,9 @@ int main(void)
if (hostindex == NUM_CACHE_ENTRIES)
hostindex = 0;
- hostlist[hi].addr =
- rvnpacket.saddr.s_addr;
- memcpy(hostlist[hi].addr6,
- rvnpacket.s6addr.s6_addr,
- sizeof(hostlist[hi].addr6));
+ sockaddr_copy(&hostlist[hi].addr, &rvnpacket.addr);
}
- strncpy(hostlist[hi].fqdn, rvnpacket.fqdn, 44);
+ strncpy(hostlist[hi].fqdn, rvnpacket.fqdn, sizeof(hostlist[hi].fqdn) - 1);
hostlist[hi].ready = RESOLVED;
}
@@ -423,26 +392,8 @@ int main(void)
* address is RESOLVING.
*
*/
- hostlist
- [hostindex].
- addr =
- rvnpacket.
- saddr.
- s_addr;
- memcpy(hostlist
- [hostindex].
- addr6,
- rvnpacket.
- s6addr.
- s6_addr,
- sizeof
- (hostlist
- [hostindex].
- addr6));
- hostlist
- [hostindex].
- ready =
- RESOLVING;
+ sockaddr_copy(&hostlist[hostindex].addr, &rvnpacket.addr);
+ hostlist[hostindex].ready = RESOLVING;
maxlogged = 0;
fr = fork();
@@ -505,17 +456,7 @@ int main(void)
}
}
rvnpacket.type = RVN_REPLY;
- memset(rvnpacket.fqdn, 0, sizeof(rvnpacket.fqdn));
- if (rvnpacket.saddr.s_addr != 0)
- inet_ntop(AF_INET,
- &rvnpacket.saddr,
- rvnpacket.fqdn,
- sizeof(rvnpacket.fqdn));
- else
- inet_ntop(AF_INET6,
- &rvnpacket.s6addr,
- rvnpacket.fqdn,
- sizeof(rvnpacket.fqdn));
+ sockaddr_ntop(&rvnpacket.addr, rvnpacket.fqdn, sizeof(rvnpacket.fqdn));
rvnpacket.ready = RESOLVING;
br = sendto(ifd, &rvnpacket,
diff --git a/src/rvnamed.h b/src/rvnamed.h
index 91442d6..6155493 100644
--- a/src/rvnamed.h
+++ b/src/rvnamed.h
@@ -24,8 +24,7 @@
struct rvn {
int type;
int ready;
- struct in_addr saddr;
- struct in6_addr s6addr;
+ struct sockaddr_storage addr;
char fqdn[45];
};
diff --git a/src/tcptable.c b/src/tcptable.c
index 5a19cb5..1167b10 100644
--- a/src/tcptable.c
+++ b/src/tcptable.c
@@ -20,6 +20,7 @@ tcptable.c - table manipulation routines for the IP monitor
#include "rvnamed.h"
#include "servname.h"
#include "hostmon.h"
+#include "sockaddr.h"
#define MSGSTRING_MAX 320
@@ -50,26 +51,47 @@ static void setlabels(WINDOW *win, int mode)
* The hash function for the TCP hash table
*/
-static unsigned int tcp_hash(unsigned long saddr, uint32_t *s6addr,
- unsigned int sport, unsigned long daddr,
- uint32_t *d6addr, unsigned int dport,
+static unsigned int tcp_hash(struct sockaddr_storage *saddr, unsigned int sport,
+ struct sockaddr_storage *daddr, unsigned int dport,
char *ifname)
{
size_t i;
- int ifsum = 0;
+ unsigned int ifsum = 0;
for (i = 0; i <= strlen(ifname) - 1; i++)
ifsum += ifname[i];
- if (s6addr != 0 && d6addr != 0) {
- for (i = 0; i < 4; i++) {
- saddr ^= s6addr[i];
- daddr ^= d6addr[i];
- }
+ switch (saddr->ss_family) {
+ case AF_INET:
+ ifsum += 4 * ((struct sockaddr_in *)saddr)->sin_addr.s_addr;
+ ifsum += 3 * sport;
+ break;
+ case AF_INET6: {
+ unsigned int ip6sum = 0;
+ for (i = 0; i < 4; i++)
+ ip6sum ^= ((struct sockaddr_in6 *)saddr)->sin6_addr.s6_addr32[i];
+ ifsum += 4 * ip6sum;
+ ifsum += 3 * sport;
+ break; }
+ default:
+ die("%s(): saddr: unknown address family", __FUNCTION__);
}
-
- return ((ifsum + (4 * saddr) + (3 * sport) + (2 * daddr) +
- dport) % ENTRIES_IN_HASH_TABLE);
+ switch (daddr->ss_family) {
+ case AF_INET:
+ ifsum += 2 * ((struct sockaddr_in *)daddr)->sin_addr.s_addr;
+ ifsum += dport;
+ break;
+ case AF_INET6: {
+ unsigned int ip6sum = 0;
+ for (i = 0; i < 4; i++)
+ ip6sum ^= ((struct sockaddr_in6 *)daddr)->sin6_addr.s6_addr32[i];
+ ifsum += 2 * ip6sum;
+ ifsum += dport;
+ break; }
+ default:
+ die("%s(): daddr: unknown address family", __FUNCTION__);
+ }
+ return (ifsum % ENTRIES_IN_HASH_TABLE);
}
static void print_tcp_num_entries(struct tcptable *table)
@@ -138,10 +160,8 @@ static void add_tcp_hash_entry(struct tcptable *table, struct
tcptableent *entry
unsigned int hp; /* hash position in table */
struct tcp_hashentry *ptmp;
- hp = tcp_hash(entry->saddr.s_addr, entry->s6addr.s6_addr32,
- entry->sport, entry->daddr.s_addr,
- entry->d6addr.s6_addr32, entry->dport, entry->ifname);
-
+ hp = tcp_hash(&entry->saddr, entry->sport,
+ &entry->daddr, entry->dport, entry->ifname);
ptmp = xmallocz(sizeof(struct tcp_hashentry));
/*
* Add backpointer from screen node to hash node for deletion later
@@ -205,10 +225,11 @@ static void del_tcp_hash_node(struct tcptable *table, struct
tcptableent *entry)
* Add a new entry to the TCP screen table
*/
-struct tcptableent *addentry(struct tcptable *table, unsigned long int saddr,
- unsigned long int daddr, uint8_t * s6addr,
- uint8_t * d6addr, unsigned int sport,
- unsigned int dport, int protocol, char *ifname,
+struct tcptableent *addentry(struct tcptable *table,
+ struct sockaddr_storage *saddr,
+ struct sockaddr_storage *daddr,
+ unsigned int sport, unsigned int dport,
+ int protocol, char *ifname,
int *rev_lookup, int rvnfd, int servnames)
{
struct tcptableent *new_entry;
@@ -296,24 +317,10 @@ struct tcptableent *addentry(struct tcptable *table, unsigned long
int saddr,
* Fill in address fields with raw IP addresses
*/
- new_entry->saddr.s_addr = new_entry->oth_connection->daddr.s_addr =
- saddr;
- if (s6addr == NULL) {
- memset(&new_entry->s6addr, 0, 16);
- memset(&new_entry->oth_connection->d6addr, 0, 16);
- } else {
- memcpy(&new_entry->s6addr, s6addr, 16);
- memcpy(&new_entry->oth_connection->d6addr, s6addr, 16);
- }
- new_entry->daddr.s_addr = new_entry->oth_connection->saddr.s_addr =
- daddr;
- if (d6addr == NULL) {
- memset(&new_entry->d6addr, 0, 16);
- memset(&new_entry->oth_connection->s6addr, 0, 16);
- } else {
- memcpy(&new_entry->d6addr, d6addr, 16);
- memcpy(&new_entry->oth_connection->s6addr, d6addr, 16);
- }
+ sockaddr_copy(&new_entry->saddr, saddr);
+ sockaddr_copy(&new_entry->oth_connection->daddr, saddr);
+ sockaddr_copy(&new_entry->daddr, daddr);
+ sockaddr_copy(&new_entry->oth_connection->saddr, daddr);
new_entry->protocol = protocol;
/*
@@ -351,11 +358,11 @@ struct tcptableent *addentry(struct tcptable *table, unsigned long
int saddr,
new_entry->stat = new_entry->oth_connection->stat = 0;
new_entry->s_fstat =
- revname(rev_lookup, &(new_entry->saddr), &new_entry->s6addr,
+ revname(rev_lookup, &new_entry->saddr,
new_entry->s_fqdn, sizeof(new_entry->s_fqdn), rvnfd);
new_entry->d_fstat =
- revname(rev_lookup, &(new_entry->daddr), &new_entry->d6addr,
+ revname(rev_lookup, &new_entry->daddr,
new_entry->d_fqdn, sizeof(new_entry->d_fqdn), rvnfd);
/*
@@ -492,11 +499,12 @@ void write_timeout_log(int logging, FILE * logfile, struct
tcptableent *tcpnode,
}
}
-struct tcptableent *in_table(struct tcptable *table, unsigned long saddr,
- unsigned long daddr, uint8_t * s6addr,
- uint8_t * d6addr, unsigned int sport,
- unsigned int dport, char *ifname, int logging,
- FILE * logfile, struct OPTIONS *opts)
+struct tcptableent *in_table(struct tcptable *table,
+ struct sockaddr_storage *saddr,
+ struct sockaddr_storage *daddr,
+ unsigned int sport, unsigned int dport,
+ char *ifname, int logging,
+ FILE *logfile, struct OPTIONS *opts)
{
struct tcp_hashentry *hashptr;
unsigned int hp;
@@ -504,9 +512,6 @@ struct tcptableent *in_table(struct tcptable *table, unsigned long
saddr,
time_t now;
time_t timeout;
- int sfree = 0;
- int dfree = 0;
-
if (opts != NULL)
timeout = opts->timeout;
else
@@ -519,23 +524,12 @@ struct tcptableent *in_table(struct tcptable *table, unsigned long
saddr,
* Determine hash table index for this set of addresses and ports
*/
- hp = tcp_hash(saddr, (uint32_t *) s6addr, sport, daddr,
- (uint32_t *) d6addr, dport, ifname);
+ hp = tcp_hash(saddr, sport, daddr, dport, ifname);
hashptr = table->hash_table[hp];
- if (s6addr == NULL) {
- s6addr = xmallocz(sizeof(struct in6_addr));
- sfree = 1;
- }
- if (d6addr == NULL) {
- d6addr = xmallocz(sizeof(struct in6_addr));
- dfree = 1;
- }
while (hashptr != NULL) {
- if ((hashptr->tcpnode->saddr.s_addr == saddr)
- && (!memcmp(&hashptr->tcpnode->s6addr.s6_addr, s6addr, 16))
- && (hashptr->tcpnode->daddr.s_addr == daddr)
- && (!memcmp(&hashptr->tcpnode->d6addr.s6_addr, d6addr, 16))
+ if (sockaddr_is_equal(&hashptr->tcpnode->saddr, saddr)
+ && sockaddr_is_equal(&hashptr->tcpnode->daddr, daddr)
&& (hashptr->tcpnode->sport == sport)
&& (hashptr->tcpnode->dport == dport)
&& (strcmp(hashptr->tcpnode->ifname, ifname) == 0))
@@ -562,11 +556,6 @@ struct tcptableent *in_table(struct tcptable *table, unsigned long
saddr,
hashptr = hashptr->next_entry;
}
- if (sfree)
- free(s6addr);
- if (dfree)
- free(d6addr);
-
if (hashptr != NULL) { /* needed to avoid SIGSEGV */
if ((((hashptr->tcpnode->finsent == 2)
&& (hashptr->tcpnode->oth_connection->finsent == 2)))
@@ -599,16 +588,14 @@ void updateentry(struct tcptable *table, struct tcptableent
*tableentry,
if (tableentry->s_fstat != RESOLVED) {
tableentry->s_fstat =
- revname(revlook, &(tableentry->saddr),
- &(tableentry->s6addr), tableentry->s_fqdn,
+ revname(revlook, &tableentry->saddr, tableentry->s_fqdn,
sizeof(tableentry->s_fqdn), rvnfd);
strcpy(tableentry->oth_connection->d_fqdn, tableentry->s_fqdn);
tableentry->oth_connection->d_fstat = tableentry->s_fstat;
}
if (tableentry->d_fstat != RESOLVED) {
tableentry->d_fstat =
- revname(revlook, &(tableentry->daddr),
- &(tableentry->d6addr), tableentry->d_fqdn,
+ revname(revlook, &tableentry->daddr, tableentry->d_fqdn,
sizeof(tableentry->d_fqdn), rvnfd);
strcpy(tableentry->oth_connection->s_fqdn, tableentry->d_fqdn);
tableentry->oth_connection->s_fstat = tableentry->d_fstat;
diff --git a/src/tcptable.h b/src/tcptable.h
index 8b3b931..cc2d0c0 100644
--- a/src/tcptable.h
+++ b/src/tcptable.h
@@ -22,10 +22,8 @@
#define ENTRIES_IN_HASH_TABLE 1543
struct tcptableent {
- struct in_addr saddr;
- struct in_addr daddr;
- struct in6_addr s6addr;
- struct in6_addr d6addr;
+ struct sockaddr_storage saddr;
+ struct sockaddr_storage daddr;
char s_fqdn[45]; /* fully-qualified domain names */
char d_fqdn[45];
int s_fstat;
@@ -98,16 +96,18 @@ struct tcptable {
void init_tcp_table(struct tcptable *table);
-struct tcptableent *addentry(struct tcptable *table, unsigned long int saddr,
- unsigned long int daddr, uint8_t * s6addr,
- uint8_t * d6addr, unsigned int sport,
- unsigned int dport, int protocol, char *ifname,
+struct tcptableent *addentry(struct tcptable *table,
+ struct sockaddr_storage *saddr,
+ struct sockaddr_storage *daddr,
+ unsigned int sport, unsigned int dport,
+ int protocol, char *ifname,
int *rev_lookup, int rvnamedon, int servnames);
-struct tcptableent *in_table(struct tcptable *table, unsigned long saddr,
- unsigned long daddr, uint8_t * s6addr,
- uint8_t * d6addr, unsigned int sport,
- unsigned int dport, char *ifname, int logging,
+struct tcptableent *in_table(struct tcptable *table,
+ struct sockaddr_storage *saddr,
+ struct sockaddr_storage *daddr,
+ unsigned int sport, unsigned int dport,
+ char *ifname, int logging,
FILE * logfile, struct OPTIONS *opts);
void updateentry(struct tcptable *table, struct tcptableent *tableentry,
--
1.7.8.4