Vitezslav Samel (3): itrafmon.c: ipmon(): invert one condition to make the source more readable itrafmon.c: ipmon(): change sequence of if()s into switch() use integer constant when dealing with integer variables
src/hostmon.c | 2 +- src/ifstats.c | 4 +- src/itrafmon.c | 740 ++++++++++++++++++++++++++++---------------------------- src/pktsize.c | 2 +- src/serv.c | 2 +- 5 files changed, 376 insertions(+), 374 deletions(-)
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/itrafmon.c | 697 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 349 insertions(+), 348 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c index 597f82c..1e8dd7c 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -1141,390 +1141,391 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, } }
- if (readlen > 0) { - total_pkts++; - show_stats(statwin, total_pkts); - - pkt_result = - processpacket((char *) tpacket, &packet, - (unsigned int *) &readlen, &br, - &sport, &dport, &fromaddr, - ofilter, MATCH_OPPOSITE_ALWAYS, - ifname, options->v6inv4asv6); - - if (pkt_result != PACKET_OK) - continue; - - if ((fromaddr.sll_protocol != ETH_P_IP) - && (fromaddr.sll_protocol != ETH_P_IPV6)) { - add_othp_entry(&othptbl, &table, 0, 0, NULL, - NULL, NOT_IP, - fromaddr.sll_protocol, fromaddr.sll_hatype, - (char *) tpacket, - (char *) packet, br, ifname, 0, - 0, 0, logging, logfile, - options->servnames, 0, &nomem); - continue; + if (readlen <= 0) + continue; + + total_pkts++; + show_stats(statwin, total_pkts); + + pkt_result = + processpacket((char *) tpacket, &packet, + (unsigned int *) &readlen, &br, + &sport, &dport, &fromaddr, + ofilter, MATCH_OPPOSITE_ALWAYS, + ifname, options->v6inv4asv6); + + if (pkt_result != PACKET_OK) + continue; + + if ((fromaddr.sll_protocol != ETH_P_IP) + && (fromaddr.sll_protocol != ETH_P_IPV6)) { + add_othp_entry(&othptbl, &table, 0, 0, NULL, + NULL, NOT_IP, + fromaddr.sll_protocol, fromaddr.sll_hatype, + (char *) tpacket, + (char *) packet, br, ifname, 0, + 0, 0, logging, logfile, + options->servnames, 0, &nomem); + continue; + } else { + if (fromaddr.sll_protocol == ETH_P_IP) { + ippacket = (struct iphdr *) packet; + iphlen = ippacket->ihl * 4; + ip6packet = NULL; + protocol = ippacket->protocol; + frag_off = ippacket->frag_off; } else { - if (fromaddr.sll_protocol == ETH_P_IP) { - ippacket = (struct iphdr *) packet; - iphlen = ippacket->ihl * 4; - ip6packet = NULL; - protocol = ippacket->protocol; - frag_off = ippacket->frag_off; + ip6packet = (struct ip6_hdr *) packet; + iphlen = 40; + ippacket = NULL; + protocol = ip6packet->ip6_nxt; + frag_off = 0; + } + transpacket = + (struct tcphdr *) (packet + iphlen); + + if (protocol == IPPROTO_TCP) { + + if (ippacket != NULL) { + tcpentry = + in_table(&table, + ippacket->saddr, + ippacket->daddr, + NULL, NULL, + ntohs(sport), + ntohs(dport), + ifname, logging, + logfile, &nomem, + options); } else { - ip6packet = (struct ip6_hdr *) packet; - iphlen = 40; - ippacket = NULL; - protocol = ip6packet->ip6_nxt; - frag_off = 0; + tcpentry = + in_table(&table, 0, 0, + (uint8_t + *) (&ip6packet-> + ip6_src. + s6_addr), + (uint8_t + *) (&ip6packet-> + ip6_dst. + s6_addr), + ntohs(sport), + ntohs(dport), + ifname, logging, + logfile, &nomem, + options); } - transpacket = - (struct tcphdr *) (packet + iphlen); - - if (protocol == IPPROTO_TCP) { - - if (ippacket != NULL) { - tcpentry = - in_table(&table, - ippacket->saddr, - ippacket->daddr, - NULL, NULL, - ntohs(sport), - ntohs(dport), - ifname, logging, - logfile, &nomem, - options); - } else { - tcpentry = - in_table(&table, 0, 0, - (uint8_t - *) (&ip6packet-> - ip6_src. - s6_addr), - (uint8_t - *) (&ip6packet-> - ip6_dst. - s6_addr), - ntohs(sport), - ntohs(dport), - ifname, logging, - logfile, &nomem, - options); - }
- /* - * Add a new entry if it doesn't exist, and, - * to reduce the chances of stales, not a FIN. - */ + /* + * Add a new entry if it doesn't exist, and, + * to reduce the chances of stales, not a FIN. + */
- if ((ntohs(frag_off) & 0x3fff) == 0) { /* first frag only */ - if ((tcpentry == NULL) - && (!(transpacket->fin))) { - - /* - * Ok, so we have a packet. Add it if this connection - * is not yet closed, or if it is a SYN packet. - */ - - if (!nomem) { - wasempty = - (table. - head == - NULL); - if (ippacket != - NULL) - tcpentry - = - addentry - (&table, - (unsigned - long) - ippacket-> - saddr, - (unsigned - long) - ippacket-> - daddr, - NULL, - NULL, - sport, - dport, - ippacket-> - protocol, - ifname, - &revlook, - rvnfd, - options-> - servnames, - &nomem); - else - tcpentry - = - addentry - (&table, - 0, - 0, - (uint8_t - *) - (&ip6packet-> - ip6_src. - s6_addr), - (uint8_t - *) - (&ip6packet-> - ip6_dst. - s6_addr), - sport, - dport, - ip6packet-> - ip6_nxt, - ifname, - &revlook, - rvnfd, - options-> - servnames, - &nomem); - if (tcpentry != - NULL) { - printentry - (&table, - tcpentry-> - oth_connection, - screen_idx, - mode); + if ((ntohs(frag_off) & 0x3fff) == 0) { /* first frag only */ + if ((tcpentry == NULL) + && (!(transpacket->fin))) {
- if (wasempty) { - set_barptr - ((void *) &(table.barptr), table.firstvisible, &(table.firstvisible->starttime), &(table.firstvisible->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); - table. - baridx - = - 1; - } - - if ((table.barptr == tcpentry) || (table.barptr == tcpentry->oth_connection)) - set_barptr - ((void *) &(table.barptr), table.barptr, &(table.barptr->starttime), &(table.barptr->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); - } - } - } - } - /* - * If we had an addentry() success, we should have no - * problem here. Same thing if we had a table lookup - * success. - */ - - if (tcpentry != NULL) { /* - * Don't bother updating the entry if the connection - * has been previously reset. (Does this really - * happen in practice?) + * Ok, so we have a packet. Add it if this connection + * is not yet closed, or if it is a SYN packet. */
- if (! - (tcpentry-> - stat & FLAG_RST)) { - if (revlook) { - p_sstat = - tcpentry-> - s_fstat; - p_dstat = - tcpentry-> - d_fstat; - } - if (ippacket != NULL) - updateentry + if (!nomem) { + wasempty = + (table. + head == + NULL); + if (ippacket != + NULL) + tcpentry + = + addentry (&table, - tcpentry, - transpacket, - tpacket, - fromaddr.sll_hatype, - readlen, - br, + (unsigned + long) + ippacket-> + saddr, + (unsigned + long) ippacket-> - frag_off, - logging, + daddr, + NULL, + NULL, + sport, + dport, + ippacket-> + protocol, + ifname, &revlook, rvnfd, - options, - logfile, + options-> + servnames, &nomem); else - updateentry + tcpentry + = + addentry (&table, - tcpentry, - transpacket, - tpacket, - fromaddr.sll_hatype, - readlen, - readlen, 0, - logging, + 0, + 0, + (uint8_t + *) + (&ip6packet-> + ip6_src. + s6_addr), + (uint8_t + *) + (&ip6packet-> + ip6_dst. + s6_addr), + sport, + dport, + ip6packet-> + ip6_nxt, + ifname, &revlook, rvnfd, - options, - logfile, - &nomem); - /* - * Log first packet of a TCP connection except if - * it's a RST, which was already logged earlier in - * updateentry() - */ - - if ((tcpentry->pcount == - 1) - && - (!(tcpentry-> - stat & FLAG_RST)) - && (logging)) { - strcpy - (msgstring, - "first packet"); - if (transpacket->syn) - strcat - (msgstring, - " (SYN)"); - - writetcplog - (logging, - logfile, - tcpentry, - readlen, options-> - mac, - msgstring); - } - - if ((revlook) - && - (((p_sstat != - RESOLVED) - && (tcpentry-> - s_fstat == - RESOLVED)) - || - ((p_dstat != - RESOLVED) - && (tcpentry-> - d_fstat == - RESOLVED)))) { - clearaddr - (&table, - tcpentry, - screen_idx); - clearaddr - (&table, - tcpentry-> - oth_connection, - screen_idx); - } - printentry(&table, - tcpentry, - screen_idx, - mode); - - /* - * Special cases: Update other direction if it's - * an ACK in response to a FIN. - * - * -- or -- - * - * Addresses were just resolved for the other - * direction, so we should also do so here. - */ - - if (((tcpentry->oth_connection->finsent == 2) && /* FINed and ACKed */ - (ntohl - (transpacket-> - seq) == - tcpentry-> - oth_connection-> - finack)) - || ((revlook) - && - (((p_sstat != - RESOLVED) - && (tcpentry-> - s_fstat == - RESOLVED)) - || - ((p_dstat != - RESOLVED) - && (tcpentry-> - d_fstat == - RESOLVED))))) + servnames, + &nomem); + if (tcpentry != + NULL) { printentry (&table, tcpentry-> oth_connection, screen_idx, mode); + + if (wasempty) { + set_barptr + ((void *) &(table.barptr), table.firstvisible, &(table.firstvisible->starttime), &(table.firstvisible->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); + table. + baridx + = + 1; + } + + if ((table.barptr == tcpentry) || (table.barptr == tcpentry->oth_connection)) + set_barptr + ((void *) &(table.barptr), table.barptr, &(table.barptr->starttime), &(table.barptr->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); + } } } - } else if (ippacket != NULL) { - fragment = - ((ntohs(ippacket->frag_off) & - 0x1fff) != 0); + } + /* + * If we had an addentry() success, we should have no + * problem here. Same thing if we had a table lookup + * success. + */
- if (ippacket->protocol == IPPROTO_ICMP) { + if (tcpentry != NULL) { + /* + * Don't bother updating the entry if the connection + * has been previously reset. (Does this really + * happen in practice?) + */
+ if (! + (tcpentry-> + stat & FLAG_RST)) { + if (revlook) { + p_sstat = + tcpentry-> + s_fstat; + p_dstat = + tcpentry-> + d_fstat; + } + if (ippacket != NULL) + updateentry + (&table, + tcpentry, + transpacket, + tpacket, + fromaddr.sll_hatype, + readlen, + br, + ippacket-> + frag_off, + logging, + &revlook, + rvnfd, + options, + logfile, + &nomem); + else + updateentry + (&table, + tcpentry, + transpacket, + tpacket, + fromaddr.sll_hatype, + readlen, + readlen, 0, + logging, + &revlook, + rvnfd, + options, + logfile, + &nomem); /* - * Cancel the corresponding TCP entry if an ICMP - * Destination Unreachable or TTL Exceeded message - * is received. + * Log first packet of a TCP connection except if + * it's a RST, which was already logged earlier in + * updateentry() */
- if (((struct icmphdr *) - transpacket)->type == - ICMP_DEST_UNREACH) - process_dest_unreach - (&table, (char *) - transpacket, - ifname, &nomem); + if ((tcpentry->pcount == + 1) + && + (!(tcpentry-> + stat & FLAG_RST)) + && (logging)) { + strcpy + (msgstring, + "first packet"); + if (transpacket->syn) + strcat + (msgstring, + " (SYN)"); + + writetcplog + (logging, + logfile, + tcpentry, + readlen, + options-> + mac, + msgstring); + }
- } - add_othp_entry(&othptbl, &table, - ippacket->saddr, - ippacket->daddr, NULL, - NULL, IS_IP, - ippacket->protocol, - fromaddr.sll_hatype, - (char *) tpacket, - (char *) transpacket, - readlen, ifname, - &revlook, rvnfd, - options->timeout, - logging, logfile, - options->servnames, - fragment, &nomem); + if ((revlook) + && + (((p_sstat != + RESOLVED) + && (tcpentry-> + s_fstat == + RESOLVED)) + || + ((p_dstat != + RESOLVED) + && (tcpentry-> + d_fstat == + RESOLVED)))) { + clearaddr + (&table, + tcpentry, + screen_idx); + clearaddr + (&table, + tcpentry-> + oth_connection, + screen_idx); + } + printentry(&table, + tcpentry, + screen_idx, + mode);
- } else { - if (ip6packet->ip6_nxt == - IPPROTO_ICMPV6) { - if (((struct icmp6_hdr *) - transpacket)->icmp6_type == - ICMP6_DST_UNREACH) - process_dest_unreach - (&table, (char *) - transpacket, - ifname, &nomem); + /* + * Special cases: Update other direction if it's + * an ACK in response to a FIN. + * + * -- or -- + * + * Addresses were just resolved for the other + * direction, so we should also do so here. + */ + + if (((tcpentry->oth_connection->finsent == 2) && /* FINed and ACKed */ + (ntohl + (transpacket-> + seq) == + tcpentry-> + oth_connection-> + finack)) + || ((revlook) + && + (((p_sstat != + RESOLVED) + && (tcpentry-> + s_fstat == + RESOLVED)) + || + ((p_dstat != + RESOLVED) + && (tcpentry-> + d_fstat == + RESOLVED))))) + printentry + (&table, + tcpentry-> + oth_connection, + screen_idx, + mode); } - add_othp_entry(&othptbl, &table, 0, 0, - &ip6packet->ip6_src, - &ip6packet->ip6_dst, - IS_IP, - ip6packet->ip6_nxt, - fromaddr.sll_hatype, - (char *) tpacket, - (char *) transpacket, - readlen, ifname, - &revlook, rvnfd, - options->timeout, - logging, logfile, - options->servnames, - fragment, &nomem); } + } else if (ippacket != NULL) { + fragment = + ((ntohs(ippacket->frag_off) & + 0x1fff) != 0); + + if (ippacket->protocol == IPPROTO_ICMP) { + + /* + * Cancel the corresponding TCP entry if an ICMP + * Destination Unreachable or TTL Exceeded message + * is received. + */ + + if (((struct icmphdr *) + transpacket)->type == + ICMP_DEST_UNREACH) + process_dest_unreach + (&table, (char *) + transpacket, + ifname, &nomem); + + } + add_othp_entry(&othptbl, &table, + ippacket->saddr, + ippacket->daddr, NULL, + NULL, IS_IP, + ippacket->protocol, + fromaddr.sll_hatype, + (char *) tpacket, + (char *) transpacket, + readlen, ifname, + &revlook, rvnfd, + options->timeout, + logging, logfile, + options->servnames, + fragment, &nomem); + + } else { + if (ip6packet->ip6_nxt == + IPPROTO_ICMPV6) { + if (((struct icmp6_hdr *) + transpacket)->icmp6_type == + ICMP6_DST_UNREACH) + process_dest_unreach + (&table, (char *) + transpacket, + ifname, &nomem); + } + add_othp_entry(&othptbl, &table, 0, 0, + &ip6packet->ip6_src, + &ip6packet->ip6_dst, + IS_IP, + ip6packet->ip6_nxt, + fromaddr.sll_hatype, + (char *) tpacket, + (char *) transpacket, + readlen, ifname, + &revlook, rvnfd, + options->timeout, + logging, logfile, + options->servnames, + fragment, &nomem); } } }
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/itrafmon.c | 655 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 328 insertions(+), 327 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c index 1e8dd7c..43063a3 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -1157,8 +1157,22 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, if (pkt_result != PACKET_OK) continue;
- if ((fromaddr.sll_protocol != ETH_P_IP) - && (fromaddr.sll_protocol != ETH_P_IPV6)) { + switch(fromaddr.sll_protocol) { + case ETH_P_IP: + ippacket = (struct iphdr *) packet; + iphlen = ippacket->ihl * 4; + ip6packet = NULL; + protocol = ippacket->protocol; + frag_off = ippacket->frag_off; + break; + case ETH_P_IPV6: + ip6packet = (struct ip6_hdr *) packet; + iphlen = 40; + ippacket = NULL; + protocol = ip6packet->ip6_nxt; + frag_off = 0; + break; + default: add_othp_entry(&othptbl, &table, 0, 0, NULL, NULL, NOT_IP, fromaddr.sll_protocol, fromaddr.sll_hatype, @@ -1167,366 +1181,353 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, 0, 0, logging, logfile, options->servnames, 0, &nomem); continue; - } else { - if (fromaddr.sll_protocol == ETH_P_IP) { - ippacket = (struct iphdr *) packet; - iphlen = ippacket->ihl * 4; - ip6packet = NULL; - protocol = ippacket->protocol; - frag_off = ippacket->frag_off; + } + + transpacket = + (struct tcphdr *) (packet + iphlen); + + if (protocol == IPPROTO_TCP) { + + if (ippacket != NULL) { + tcpentry = + in_table(&table, + ippacket->saddr, + ippacket->daddr, + NULL, NULL, + ntohs(sport), + ntohs(dport), + ifname, logging, + logfile, &nomem, + options); } else { - ip6packet = (struct ip6_hdr *) packet; - iphlen = 40; - ippacket = NULL; - protocol = ip6packet->ip6_nxt; - frag_off = 0; + tcpentry = + in_table(&table, 0, 0, + (uint8_t + *) (&ip6packet-> + ip6_src. + s6_addr), + (uint8_t + *) (&ip6packet-> + ip6_dst. + s6_addr), + ntohs(sport), + ntohs(dport), + ifname, logging, + logfile, &nomem, + options); } - transpacket = - (struct tcphdr *) (packet + iphlen); - - if (protocol == IPPROTO_TCP) { - - if (ippacket != NULL) { - tcpentry = - in_table(&table, - ippacket->saddr, - ippacket->daddr, - NULL, NULL, - ntohs(sport), - ntohs(dport), - ifname, logging, - logfile, &nomem, - options); - } else { - tcpentry = - in_table(&table, 0, 0, - (uint8_t - *) (&ip6packet-> - ip6_src. - s6_addr), - (uint8_t - *) (&ip6packet-> - ip6_dst. - s6_addr), - ntohs(sport), - ntohs(dport), - ifname, logging, - logfile, &nomem, - options); - }
- /* - * Add a new entry if it doesn't exist, and, - * to reduce the chances of stales, not a FIN. - */ + /* + * Add a new entry if it doesn't exist, and, + * to reduce the chances of stales, not a FIN. + */
- if ((ntohs(frag_off) & 0x3fff) == 0) { /* first frag only */ - if ((tcpentry == NULL) - && (!(transpacket->fin))) { - - /* - * Ok, so we have a packet. Add it if this connection - * is not yet closed, or if it is a SYN packet. - */ - - if (!nomem) { - wasempty = - (table. - head == - NULL); - if (ippacket != - NULL) - tcpentry - = - addentry - (&table, - (unsigned - long) - ippacket-> - saddr, - (unsigned - long) - ippacket-> - daddr, - NULL, - NULL, - sport, - dport, - ippacket-> - protocol, - ifname, - &revlook, - rvnfd, - options-> - servnames, - &nomem); - else - tcpentry - = - addentry - (&table, - 0, - 0, - (uint8_t - *) - (&ip6packet-> - ip6_src. - s6_addr), - (uint8_t - *) - (&ip6packet-> - ip6_dst. - s6_addr), - sport, - dport, - ip6packet-> - ip6_nxt, - ifname, - &revlook, - rvnfd, - options-> - servnames, - &nomem); - if (tcpentry != - NULL) { - printentry - (&table, - tcpentry-> - oth_connection, - screen_idx, - mode); + if ((ntohs(frag_off) & 0x3fff) == 0) { /* first frag only */ + if ((tcpentry == NULL) + && (!(transpacket->fin))) {
- if (wasempty) { - set_barptr - ((void *) &(table.barptr), table.firstvisible, &(table.firstvisible->starttime), &(table.firstvisible->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); - table. - baridx - = - 1; - } - - if ((table.barptr == tcpentry) || (table.barptr == tcpentry->oth_connection)) - set_barptr - ((void *) &(table.barptr), table.barptr, &(table.barptr->starttime), &(table.barptr->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); - } - } - } - } - /* - * If we had an addentry() success, we should have no - * problem here. Same thing if we had a table lookup - * success. - */ - - if (tcpentry != NULL) { /* - * Don't bother updating the entry if the connection - * has been previously reset. (Does this really - * happen in practice?) + * Ok, so we have a packet. Add it if this connection + * is not yet closed, or if it is a SYN packet. */
- if (! - (tcpentry-> - stat & FLAG_RST)) { - if (revlook) { - p_sstat = - tcpentry-> - s_fstat; - p_dstat = - tcpentry-> - d_fstat; - } - if (ippacket != NULL) - updateentry + if (!nomem) { + wasempty = + (table. + head == + NULL); + if (ippacket != + NULL) + tcpentry + = + addentry (&table, - tcpentry, - transpacket, - tpacket, - fromaddr.sll_hatype, - readlen, - br, + (unsigned + long) + ippacket-> + saddr, + (unsigned + long) ippacket-> - frag_off, - logging, + daddr, + NULL, + NULL, + sport, + dport, + ippacket-> + protocol, + ifname, &revlook, rvnfd, - options, - logfile, + options-> + servnames, &nomem); else - updateentry + tcpentry + = + addentry (&table, - tcpentry, - transpacket, - tpacket, - fromaddr.sll_hatype, - readlen, - readlen, 0, - logging, + 0, + 0, + (uint8_t + *) + (&ip6packet-> + ip6_src. + s6_addr), + (uint8_t + *) + (&ip6packet-> + ip6_dst. + s6_addr), + sport, + dport, + ip6packet-> + ip6_nxt, + ifname, &revlook, rvnfd, - options, - logfile, - &nomem); - /* - * Log first packet of a TCP connection except if - * it's a RST, which was already logged earlier in - * updateentry() - */ - - if ((tcpentry->pcount == - 1) - && - (!(tcpentry-> - stat & FLAG_RST)) - && (logging)) { - strcpy - (msgstring, - "first packet"); - if (transpacket->syn) - strcat - (msgstring, - " (SYN)"); - - writetcplog - (logging, - logfile, - tcpentry, - readlen, options-> - mac, - msgstring); - } - - if ((revlook) - && - (((p_sstat != - RESOLVED) - && (tcpentry-> - s_fstat == - RESOLVED)) - || - ((p_dstat != - RESOLVED) - && (tcpentry-> - d_fstat == - RESOLVED)))) { - clearaddr - (&table, - tcpentry, - screen_idx); - clearaddr - (&table, - tcpentry-> - oth_connection, - screen_idx); - } - printentry(&table, - tcpentry, - screen_idx, - mode); - - /* - * Special cases: Update other direction if it's - * an ACK in response to a FIN. - * - * -- or -- - * - * Addresses were just resolved for the other - * direction, so we should also do so here. - */ - - if (((tcpentry->oth_connection->finsent == 2) && /* FINed and ACKed */ - (ntohl - (transpacket-> - seq) == - tcpentry-> - oth_connection-> - finack)) - || ((revlook) - && - (((p_sstat != - RESOLVED) - && (tcpentry-> - s_fstat == - RESOLVED)) - || - ((p_dstat != - RESOLVED) - && (tcpentry-> - d_fstat == - RESOLVED))))) + servnames, + &nomem); + if (tcpentry != + NULL) { printentry (&table, tcpentry-> oth_connection, screen_idx, mode); + + if (wasempty) { + set_barptr + ((void *) &(table.barptr), table.firstvisible, &(table.firstvisible->starttime), &(table.firstvisible->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); + table. + baridx + = + 1; + } + + if ((table.barptr == tcpentry) || (table.barptr == tcpentry->oth_connection)) + set_barptr + ((void *) &(table.barptr), table.barptr, &(table.barptr->starttime), &(table.barptr->spanbr), sizeof(unsigned long), statwin, &statcleared, statx); + } } } - } else if (ippacket != NULL) { - fragment = - ((ntohs(ippacket->frag_off) & - 0x1fff) != 0); + } + /* + * If we had an addentry() success, we should have no + * problem here. Same thing if we had a table lookup + * success. + */
- if (ippacket->protocol == IPPROTO_ICMP) { + if (tcpentry != NULL) { + /* + * Don't bother updating the entry if the connection + * has been previously reset. (Does this really + * happen in practice?) + */
+ if (! + (tcpentry-> + stat & FLAG_RST)) { + if (revlook) { + p_sstat = + tcpentry-> + s_fstat; + p_dstat = + tcpentry-> + d_fstat; + } + if (ippacket != NULL) + updateentry + (&table, + tcpentry, + transpacket, + tpacket, + fromaddr.sll_hatype, + readlen, + br, + ippacket-> + frag_off, + logging, + &revlook, + rvnfd, + options, + logfile, + &nomem); + else + updateentry + (&table, + tcpentry, + transpacket, + tpacket, + fromaddr.sll_hatype, + readlen, + readlen, 0, + logging, + &revlook, + rvnfd, + options, + logfile, + &nomem); /* - * Cancel the corresponding TCP entry if an ICMP - * Destination Unreachable or TTL Exceeded message - * is received. + * Log first packet of a TCP connection except if + * it's a RST, which was already logged earlier in + * updateentry() */
- if (((struct icmphdr *) - transpacket)->type == - ICMP_DEST_UNREACH) - process_dest_unreach - (&table, (char *) - transpacket, - ifname, &nomem); + if ((tcpentry->pcount == + 1) + && + (!(tcpentry-> + stat & FLAG_RST)) + && (logging)) { + strcpy + (msgstring, + "first packet"); + if (transpacket->syn) + strcat + (msgstring, + " (SYN)"); + + writetcplog + (logging, + logfile, + tcpentry, + readlen, + options-> + mac, + msgstring); + }
- } - add_othp_entry(&othptbl, &table, - ippacket->saddr, - ippacket->daddr, NULL, - NULL, IS_IP, - ippacket->protocol, - fromaddr.sll_hatype, - (char *) tpacket, - (char *) transpacket, - readlen, ifname, - &revlook, rvnfd, - options->timeout, - logging, logfile, - options->servnames, - fragment, &nomem); + if ((revlook) + && + (((p_sstat != + RESOLVED) + && (tcpentry-> + s_fstat == + RESOLVED)) + || + ((p_dstat != + RESOLVED) + && (tcpentry-> + d_fstat == + RESOLVED)))) { + clearaddr + (&table, + tcpentry, + screen_idx); + clearaddr + (&table, + tcpentry-> + oth_connection, + screen_idx); + } + printentry(&table, + tcpentry, + screen_idx, + mode);
- } else { - if (ip6packet->ip6_nxt == - IPPROTO_ICMPV6) { - if (((struct icmp6_hdr *) - transpacket)->icmp6_type == - ICMP6_DST_UNREACH) - process_dest_unreach - (&table, (char *) - transpacket, - ifname, &nomem); + /* + * Special cases: Update other direction if it's + * an ACK in response to a FIN. + * + * -- or -- + * + * Addresses were just resolved for the other + * direction, so we should also do so here. + */ + + if (((tcpentry->oth_connection->finsent == 2) && /* FINed and ACKed */ + (ntohl + (transpacket-> + seq) == + tcpentry-> + oth_connection-> + finack)) + || ((revlook) + && + (((p_sstat != + RESOLVED) + && (tcpentry-> + s_fstat == + RESOLVED)) + || + ((p_dstat != + RESOLVED) + && (tcpentry-> + d_fstat == + RESOLVED))))) + printentry + (&table, + tcpentry-> + oth_connection, + screen_idx, + mode); } - add_othp_entry(&othptbl, &table, 0, 0, - &ip6packet->ip6_src, - &ip6packet->ip6_dst, - IS_IP, - ip6packet->ip6_nxt, - fromaddr.sll_hatype, - (char *) tpacket, - (char *) transpacket, - readlen, ifname, - &revlook, rvnfd, - options->timeout, - logging, logfile, - options->servnames, - fragment, &nomem); } + } else if (ippacket != NULL) { + fragment = + ((ntohs(ippacket->frag_off) & + 0x1fff) != 0); + + if (ippacket->protocol == IPPROTO_ICMP) { + + /* + * Cancel the corresponding TCP entry if an ICMP + * Destination Unreachable or TTL Exceeded message + * is received. + */ + + if (((struct icmphdr *) + transpacket)->type == + ICMP_DEST_UNREACH) + process_dest_unreach + (&table, (char *) + transpacket, + ifname, &nomem); + + } + add_othp_entry(&othptbl, &table, + ippacket->saddr, + ippacket->daddr, NULL, + NULL, IS_IP, + ippacket->protocol, + fromaddr.sll_hatype, + (char *) tpacket, + (char *) transpacket, + readlen, ifname, + &revlook, rvnfd, + options->timeout, + logging, logfile, + options->servnames, + fragment, &nomem); + + } else { + if (ip6packet->ip6_nxt == + IPPROTO_ICMPV6) { + if (((struct icmp6_hdr *) + transpacket)->icmp6_type == + ICMP6_DST_UNREACH) + process_dest_unreach + (&table, (char *) + transpacket, + ifname, &nomem); + } + add_othp_entry(&othptbl, &table, 0, 0, + &ip6packet->ip6_src, + &ip6packet->ip6_dst, + IS_IP, + ip6packet->ip6_nxt, + fromaddr.sll_hatype, + (char *) tpacket, + (char *) transpacket, + readlen, ifname, + &revlook, rvnfd, + options->timeout, + logging, logfile, + options->servnames, + fragment, &nomem); } }
unow is "unsigned long long", so use 1000000ULL instead of 1e+6
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/hostmon.c | 2 +- src/ifstats.c | 4 ++-- src/pktsize.c | 2 +- src/serv.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/hostmon.c b/src/hostmon.c index d61967d..fe00f28 100644 --- a/src/hostmon.c +++ b/src/hostmon.c @@ -788,7 +788,7 @@ void hostmon(const struct OPTIONS *options, int facilitytime, char *ifptr, do { gettimeofday(&tv, NULL); now = tv.tv_sec; - unow = tv.tv_sec * 1e+6 + tv.tv_usec; + unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 5) { printelapsedtime(statbegin, now, LINES - 3, 15, diff --git a/src/ifstats.c b/src/ifstats.c index 2da2176..9997a34 100644 --- a/src/ifstats.c +++ b/src/ifstats.c @@ -491,7 +491,7 @@ void ifstats(const struct OPTIONS *options, struct filterstate *ofilter, while (!exitloop) { gettimeofday(&tv, NULL); now = tv.tv_sec; - unow = tv.tv_sec * 1e+6 + tv.tv_usec; + unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 5) { updaterates(&table, options->actmode, starttime, @@ -890,7 +890,7 @@ void detstats(char *iface, const struct OPTIONS *options, int facilitytime, while (!exitloop) { gettimeofday(&tv, NULL); now = tv.tv_sec; - unow = tv.tv_sec * 1e+6 + tv.tv_usec; + unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
rate_interval = now - starttime;
diff --git a/src/pktsize.c b/src/pktsize.c index a1e747f..5bfbeaf 100644 --- a/src/pktsize.c +++ b/src/pktsize.c @@ -267,7 +267,7 @@ void packet_size_breakdown(struct OPTIONS *options, char *ifname, do { gettimeofday(&tv, NULL); now = tv.tv_sec; - unow = tv.tv_sec * 1e+6 + tv.tv_usec; + unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if (((options->updrate != 0) && (now - updtime >= options->updrate)) diff --git a/src/serv.c b/src/serv.c index cdb7738..5ace560 100644 --- a/src/serv.c +++ b/src/serv.c @@ -831,7 +831,7 @@ void servmon(char *ifname, struct porttab *ports, const struct OPTIONS *options, while (!exitloop) { gettimeofday(&tv, NULL); now = tv.tv_sec; - unow = tv.tv_sec * 1e+6 + tv.tv_usec; + unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if (now - timeint >= 5) { printelapsedtime(starttime, now, LINES - 4, 20,
Vitezslav Samel vitezslav@samel.cz writes:
Vitezslav Samel (3): itrafmon.c: ipmon(): invert one condition to make the source more readable itrafmon.c: ipmon(): change sequence of if()s into switch() use integer constant when dealing with integer variables
src/hostmon.c | 2 +- src/ifstats.c | 4 +- src/itrafmon.c | 740 ++++++++++++++++++++++++++++---------------------------- src/pktsize.c | 2 +- src/serv.c | 2 +- 5 files changed, 376 insertions(+), 374 deletions(-)
all applied. thanks
iptraf-ng@lists.fedorahosted.org