Vitezslav Samel (2): push ntohs(port) down to the point we're getting it from packet change port-like variables to type in_port_t
src/fltdefs.h | 8 ++++---- src/ipfilter.c | 25 +++++++++++++++---------- src/ipfilter.h | 4 ++-- src/ipfrag.c | 12 ++++++------ src/ipfrag.h | 8 ++++---- src/itrafmon.c | 6 +++--- src/packet.c | 33 +++++++++++++++------------------ src/packet.h | 5 ++--- src/serv.c | 24 ++++++++++++------------ src/serv.h | 4 ++-- 10 files changed, 65 insertions(+), 64 deletions(-)
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/ipfrag.c | 8 ++++---- src/itrafmon.c | 4 ++-- src/packet.c | 24 +++++++++++------------- src/serv.c | 4 ++-- 4 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/src/ipfrag.c b/src/ipfrag.c index 15a06f1..01616fb 100644 --- a/src/ipfrag.c +++ b/src/ipfrag.c @@ -190,11 +190,11 @@ unsigned int processfragment(struct iphdr *packet, unsigned int *sport, ftmp->firstin = 1; tpacket = ((char *) (packet)) + (packet->ihl * 4); if (packet->protocol == IPPROTO_TCP) { - ftmp->s_port = ((struct tcphdr *) tpacket)->source; - ftmp->d_port = ((struct tcphdr *) tpacket)->dest; + ftmp->s_port = ntohs(((struct tcphdr *) tpacket)->source); + ftmp->d_port = ntohs(((struct tcphdr *) tpacket)->dest); } else if (packet->protocol == IPPROTO_UDP) { - ftmp->s_port = ((struct udphdr *) tpacket)->source; - ftmp->d_port = ((struct udphdr *) tpacket)->dest; + ftmp->s_port = ntohs(((struct udphdr *) tpacket)->source); + ftmp->d_port = ntohs(((struct udphdr *) tpacket)->dest); } } while (dtmp != NULL) { diff --git a/src/itrafmon.c b/src/itrafmon.c index 597920c..f334005 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -1028,8 +1028,8 @@ void ipmon(time_t facilitytime, char *ifptr)
__u8 ip_protocol = pkt_ip_protocol(&pkt); if (ip_protocol == IPPROTO_TCP) { - sockaddr_set_port(&saddr, ntohs(sport)); - sockaddr_set_port(&daddr, ntohs(dport)); + sockaddr_set_port(&saddr, sport); + sockaddr_set_port(&daddr, dport); tcpentry = in_table(&table, &saddr, &daddr, ifname, logging, logfile, options.timeout);
diff --git a/src/packet.c b/src/packet.c index d881e14..374f243 100644 --- a/src/packet.c +++ b/src/packet.c @@ -239,13 +239,13 @@ again: switch (ip->protocol) { case IPPROTO_TCP: tcp = (struct tcphdr *) ip_payload; - sport_tmp = tcp->source; - dport_tmp = tcp->dest; + sport_tmp = ntohs(tcp->source); + dport_tmp = ntohs(tcp->dest); break; case IPPROTO_UDP: udp = (struct udphdr *) ip_payload; - sport_tmp = udp->source; - dport_tmp = udp->dest; + sport_tmp = ntohs(udp->source); + dport_tmp = ntohs(udp->dest); break; default: sport_tmp = 0; @@ -263,12 +263,10 @@ again: if (dport != NULL) *dport = dport_tmp;
- /* - * Process IP filter - */ - f_sport = ntohs(sport_tmp); - f_dport = ntohs(dport_tmp); + f_sport = sport_tmp; + f_dport = dport_tmp; } + /* Process IP filter */ if ((ofilter.filtercode != 0) && (!ipfilter @@ -293,16 +291,16 @@ again: case IPPROTO_TCP: tcp = (struct tcphdr *) ip_payload; if (sport) - *sport = tcp->source; + *sport = ntohs(tcp->source); if (dport) - *dport = tcp->dest; + *dport = ntohs(tcp->dest); break; case IPPROTO_UDP: udp = (struct udphdr *) ip_payload; if (sport) - *sport = udp->source; + *sport = ntohs(udp->source); if (dport) - *dport = udp->dest; + *dport = ntohs(udp->dest); break; default: if (sport) diff --git a/src/serv.c b/src/serv.c index fdadf03..f15740e 100644 --- a/src/serv.c +++ b/src/serv.c @@ -1063,8 +1063,8 @@ void servmon(char *ifname, time_t facilitytime) switch (ip_protocol) { case IPPROTO_TCP: case IPPROTO_UDP: - updateportent(&list, ip_protocol, ntohs(sport), - ntohs(dport), iplen, idx, ports); + updateportent(&list, ip_protocol, sport, + dport, iplen, idx, ports); break; default: /* unknown L4 protocol */
Change type of all variables which stores port value to the right type: in_port_t
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/fltdefs.h | 8 ++++---- src/ipfilter.c | 25 +++++++++++++++---------- src/ipfilter.h | 4 ++-- src/ipfrag.c | 4 ++-- src/ipfrag.h | 8 ++++---- src/itrafmon.c | 2 +- src/packet.c | 9 ++++----- src/packet.h | 5 ++--- src/serv.c | 20 ++++++++++---------- src/serv.h | 4 ++-- 10 files changed, 46 insertions(+), 43 deletions(-)
diff --git a/src/fltdefs.h b/src/fltdefs.h index 3853469..cc0512f 100644 --- a/src/fltdefs.h +++ b/src/fltdefs.h @@ -38,10 +38,10 @@ struct hostparams { char d_fqdn[45]; char s_mask[20]; char d_mask[20]; - unsigned int sport1; - unsigned int sport2; - unsigned int dport1; - unsigned int dport2; + in_port_t sport1; + in_port_t sport2; + in_port_t dport1; + in_port_t dport2; int filters[256]; char protolist[70]; char reverse; diff --git a/src/ipfilter.c b/src/ipfilter.c index bec2dfd..7f9ee64 100644 --- a/src/ipfilter.c +++ b/src/ipfilter.c @@ -26,6 +26,15 @@ ipfilter.c - user interface and filter function for all IP packets #include "parseproto.h" #include "cidr.h"
+static in_port_t parse_port(char *buf) +{ + unsigned int value; + + if ((strtoul_ui(buf, 10, &value) == 0) && (value <= 65535)) + return value; + else + return 0; +} void gethostparams(struct hostparams *data, char *init_saddr, char *init_smask, char *init_sport1, char *init_sport2, char *init_daddr, char *init_dmask, char *init_dport1, char *init_dport2, @@ -185,12 +194,10 @@ void gethostparams(struct hostparams *data, char *init_saddr, char *init_smask, * Process Source Port fields */ fieldptr = fieldptr->nextfield; - if (strtoul_ui(fieldptr->buf, 10, &data->sport1) == -1) - data->sport1 = 0; + data->sport1 = parse_port(fieldptr->buf);
fieldptr = fieldptr->nextfield; - if (strtoul_ui(fieldptr->buf, 10, &data->sport2) == -1) - data->sport2 = 0; + data->sport2 = parse_port(fieldptr->buf);
/* * Process Destination Address field @@ -226,12 +233,10 @@ void gethostparams(struct hostparams *data, char *init_saddr, char *init_smask, * Process Dedination Port fields */ fieldptr = fieldptr->nextfield; - if (strtoul_ui(fieldptr->buf, 10, &data->dport1) == -1) - data->dport1 = 0; + data->dport1 = parse_port(fieldptr->buf);
fieldptr = fieldptr->nextfield; - if (strtoul_ui(fieldptr->buf, 10, &data->dport2) == -1) - data->dport2 = 0; + data->dport2 = parse_port(fieldptr->buf);
/* * Process IP protocol filter fields @@ -363,8 +368,8 @@ void ipfilterselect(int *aborted) /* * Display/logging filter for other (non-TCP, non-UDP) IP protocols. */ -int ipfilter(unsigned long saddr, unsigned long daddr, unsigned int sport, - unsigned int dport, unsigned int protocol, int match_opp_mode) +int ipfilter(unsigned long saddr, unsigned long daddr, in_port_t sport, + in_port_t dport, unsigned int protocol, int match_opp_mode) { struct filterent *fe = ofilter.fl.head; int result = 0; diff --git a/src/ipfilter.h b/src/ipfilter.h index 753acd7..ac82b93 100644 --- a/src/ipfilter.h +++ b/src/ipfilter.h @@ -6,7 +6,7 @@ void gethostparams(struct hostparams *data, char *init_saddr, char *init_smask, char *init_dmask, char *init_dport1, char *init_dport2, char *initinex, char *initmatchop, int *aborted); void ipfilterselect(int *faborted); -int ipfilter(unsigned long saddr, unsigned long daddr, unsigned int sport, - unsigned int dport, unsigned int protocol, int match_opp_mode); +int ipfilter(unsigned long saddr, unsigned long daddr, in_port_t sport, + in_port_t dport, unsigned int protocol, int match_opp_mode);
#endif /* IPTRAF_NG_IPFILTER_H */ diff --git a/src/ipfrag.c b/src/ipfrag.c index 01616fb..76196e6 100644 --- a/src/ipfrag.c +++ b/src/ipfrag.c @@ -153,8 +153,8 @@ void destroyfraglist(void) * monitor or 0 for an error condition. */
-unsigned int processfragment(struct iphdr *packet, unsigned int *sport, - unsigned int *dport, int *firstin) +unsigned int processfragment(struct iphdr *packet, in_port_t *sport, + in_port_t *dport, int *firstin) { struct fragent *ftmp; struct fragdescent *dtmp; diff --git a/src/ipfrag.h b/src/ipfrag.h index c0915a4..f20e6dd 100644 --- a/src/ipfrag.h +++ b/src/ipfrag.h @@ -16,9 +16,9 @@ struct fragdescent {
struct fragent { unsigned long s_addr; - unsigned int s_port; + in_port_t s_port; unsigned long d_addr; - unsigned int d_port; + in_port_t d_port; unsigned int id; unsigned int protocol; int firstin; @@ -36,7 +36,7 @@ struct fragfreelistent { };
void destroyfraglist(void); -unsigned int processfragment(struct iphdr *packet, unsigned int *sport, - unsigned int *dport, int *firstin); +unsigned int processfragment(struct iphdr *packet, in_port_t *sport, + in_port_t *dport, int *firstin);
#endif /* IPTRAF_NG_IPFRAG_H */ diff --git a/src/itrafmon.c b/src/itrafmon.c index f334005..b35468f 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -550,7 +550,7 @@ void ipmon(time_t facilitytime, char *ifptr)
unsigned int frag_off; struct tcphdr *transpacket; /* IP-encapsulated packet */ - unsigned int sport = 0, dport = 0; /* TCP/UDP port values */ + in_port_t sport = 0, dport = 0; /* TCP/UDP port values */ char sp_buf[10];
unsigned long screen_idx = 1; diff --git a/src/packet.c b/src/packet.c index 374f243..251466b 100644 --- a/src/packet.c +++ b/src/packet.c @@ -177,9 +177,8 @@ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) }
int packet_process(struct pkt_hdr *pkt, unsigned int *total_br, - unsigned int *sport, unsigned int *dport, - int match_opposite, - int v6inv4asv6) + in_port_t *sport, in_port_t *dport, + int match_opposite, int v6inv4asv6) { /* move packet pointer (pkt->pkt_payload) past data link header */ if (packet_adjust(pkt) != 0) @@ -192,7 +191,7 @@ again: struct iphdr *ip = pkt->iphdr; int hdr_check; register int ip_checksum; - unsigned int f_sport = 0, f_dport = 0; + in_port_t f_sport = 0, f_dport = 0;
/* * Compute and verify IP header checksum. @@ -207,7 +206,7 @@ again:
if ((ip->protocol == IPPROTO_TCP || ip->protocol == IPPROTO_UDP) && (sport != NULL && dport != NULL)) { - unsigned int sport_tmp, dport_tmp; + in_port_t sport_tmp, dport_tmp;
/* * Process TCP/UDP fragments diff --git a/src/packet.h b/src/packet.h index 8fc656d..00bc3c9 100644 --- a/src/packet.h +++ b/src/packet.h @@ -79,9 +79,8 @@ static inline __u8 pkt_ip_protocol(const struct pkt_hdr *p) void open_socket(int *fd); int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win); int packet_process(struct pkt_hdr *pkt, unsigned int *total_br, - unsigned int *sport, unsigned int *dport, - int match_opposite, - int v6inv4asv6); + in_port_t *sport, in_port_t *dport, + int match_opposite, int v6inv4asv6); void pkt_cleanup(void);
#endif /* IPTRAF_NG_PACKET_H */ diff --git a/src/serv.c b/src/serv.c index f15740e..513cc94 100644 --- a/src/serv.c +++ b/src/serv.c @@ -47,7 +47,7 @@ struct serv_spans { };
struct portlistent { - unsigned int port; + in_port_t port; unsigned int protocol; char servname[11]; unsigned int idx; @@ -189,7 +189,7 @@ static void initportlist(struct portlist *list)
static struct portlistent *addtoportlist(struct portlist *list, unsigned int protocol, - unsigned int port) + in_port_t port) { struct portlistent *ptemp;
@@ -235,7 +235,7 @@ static struct portlistent *addtoportlist(struct portlist *list, return ptemp; }
-static int portinlist(struct porttab *table, unsigned int port) +static int portinlist(struct porttab *table, in_port_t port) { struct porttab *ptmp = table;
@@ -250,13 +250,13 @@ static int portinlist(struct porttab *table, unsigned int port) return 0; }
-static int goodport(unsigned int port, struct porttab *table) +static int goodport(in_port_t port, struct porttab *table) { return ((port < 1024) || (portinlist(table, port))); }
static struct portlistent *inportlist(struct portlist *list, - unsigned int protocol, unsigned int port) + unsigned int protocol, in_port_t port) { struct portlistent *ptmp = list->head;
@@ -347,7 +347,7 @@ static void destroyportlist(struct portlist *list) }
static void updateportent(struct portlist *list, unsigned int protocol, - unsigned int sport, unsigned int dport, int br, + in_port_t sport, in_port_t dport, int br, unsigned int idx, struct porttab *ports) { struct portlistent *sport_listent = NULL; @@ -761,8 +761,8 @@ void servmon(char *ifname, time_t facilitytime)
unsigned int idx = 1;
- unsigned int sport = 0; - unsigned int dport = 0; + in_port_t sport = 0; + in_port_t dport = 0;
struct timeval tv; struct timeval tv_rate; @@ -1113,7 +1113,7 @@ err: strcpy(current_logfile, ""); }
-static void portdlg(unsigned int *port_min, unsigned int *port_max, +static void portdlg(in_port_t *port_min, in_port_t *port_max, int *aborted) { WINDOW *bw; @@ -1239,7 +1239,7 @@ static int dup_portentry(struct porttab *table, unsigned int min,
void addmoreports(struct porttab **table) { - unsigned int port_min = 0, port_max = 0; + in_port_t port_min = 0, port_max = 0; int aborted; struct porttab *ptmp;
diff --git a/src/serv.h b/src/serv.h index d5c9c76..dde7c3e 100644 --- a/src/serv.h +++ b/src/serv.h @@ -8,8 +8,8 @@ serv.h - TCP/UDP port statistics header file ***/
struct porttab { - unsigned int port_min; - unsigned int port_max; + in_port_t port_min; + in_port_t port_max; struct porttab *prev_entry; struct porttab *next_entry; };
Vitezslav Samel vitezslav@samel.cz writes:
Vitezslav Samel (2): push ntohs(port) down to the point we're getting it from packet change port-like variables to type in_port_t
src/fltdefs.h | 8 ++++---- src/ipfilter.c | 25 +++++++++++++++---------- src/ipfilter.h | 4 ++-- src/ipfrag.c | 12 ++++++------ src/ipfrag.h | 8 ++++---- src/itrafmon.c | 6 +++--- src/packet.c | 33 +++++++++++++++------------------ src/packet.h | 5 ++--- src/serv.c | 24 ++++++++++++------------ src/serv.h | 4 ++-- 10 files changed, 65 insertions(+), 64 deletions(-)
applied. thanks.
iptraf-ng@lists.fedorahosted.org