mirror of
https://github.com/nmap/nmap.git
synced 2026-01-27 00:29:03 +00:00
Whitespace.
astyle -a -p -H -s2 -m2 tcpip.cc
This commit is contained in:
124
tcpip.cc
124
tcpip.cc
@@ -156,9 +156,9 @@ char *getFinalPacketStats(char *buf, int buflen) {
|
||||
#endif
|
||||
PktCt.sendPackets,
|
||||
format_bytecount(PktCt.sendBytes, sendbytesasc,
|
||||
sizeof(sendbytesasc)), PktCt.recvPackets,
|
||||
sizeof(sendbytesasc)), PktCt.recvPackets,
|
||||
format_bytecount(PktCt.recvBytes, recvbytesasc,
|
||||
sizeof(recvbytesasc)));
|
||||
sizeof(recvbytesasc)));
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -245,8 +245,8 @@ void PacketTrace::traceND(pdirection pdir, const u8 *frame, u32 len,
|
||||
|
||||
if (len < sizeof(*ip6) + sizeof(*icmpv6)) {
|
||||
error("Packet tracer: ND packets must be at least %lu bytes long (is %lu).",
|
||||
(unsigned long) (sizeof(*ip6) + sizeof(*icmpv6)),
|
||||
(unsigned long) len);
|
||||
(unsigned long) (sizeof(*ip6) + sizeof(*icmpv6)),
|
||||
(unsigned long) len);
|
||||
return;
|
||||
}
|
||||
ip6 = (struct ip6_hdr *) frame;
|
||||
@@ -272,7 +272,7 @@ void PacketTrace::traceND(pdirection pdir, const u8 *frame, u32 len,
|
||||
} else {
|
||||
inet_ntop(AF_INET6, &msg->nd.icmpv6_target, tgt_is, sizeof(tgt_is));
|
||||
Snprintf(desc, sizeof(desc), "%s is at %s",
|
||||
tgt_is, eth_ntoa(&msg->nd.icmpv6_mac));
|
||||
tgt_is, eth_ntoa(&msg->nd.icmpv6_mac));
|
||||
}
|
||||
} else {
|
||||
error("Unknown ICMPV6 type in %s.", __func__);
|
||||
@@ -295,23 +295,23 @@ void PacketTrace::traceND(pdirection pdir, const u8 *frame, u32 len,
|
||||
IMPORTANT: This is a wrapper for function ippackethdrinfo(). Check
|
||||
nbase/nbase_net.c for details on the returned buffer. */
|
||||
static const char *nmap_format_ippacket(const u8 *packet, u32 len) {
|
||||
int detail=LOW_DETAIL;
|
||||
if(o.debugging==2){
|
||||
detail=MEDIUM_DETAIL;
|
||||
}else if(o.debugging>=3){
|
||||
detail=HIGH_DETAIL;
|
||||
}
|
||||
return ippackethdrinfo(packet, len, detail);
|
||||
int detail = LOW_DETAIL;
|
||||
if (o.debugging == 2) {
|
||||
detail = MEDIUM_DETAIL;
|
||||
} else if (o.debugging >= 3) {
|
||||
detail = HIGH_DETAIL;
|
||||
}
|
||||
return ippackethdrinfo(packet, len, detail);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* Takes an IP PACKET and prints it if packet tracing is enabled.
|
||||
'packet' must point to the IPv4 header. The direction must be
|
||||
PacketTrace::SENT or PacketTrace::RCVD . Optional 'now' argument
|
||||
makes this function slightly more efficient by avoiding a gettimeofday()
|
||||
call. */
|
||||
/* Takes an IP PACKET and prints it if packet tracing is enabled.
|
||||
'packet' must point to the IPv4 header. The direction must be
|
||||
PacketTrace::SENT or PacketTrace::RCVD . Optional 'now' argument
|
||||
makes this function slightly more efficient by avoiding a gettimeofday()
|
||||
call. */
|
||||
void PacketTrace::trace(pdirection pdir, const u8 *packet, u32 len,
|
||||
struct timeval *now) {
|
||||
struct timeval tv;
|
||||
@@ -437,8 +437,7 @@ const char *inet_socktop(struct sockaddr_storage *ss) {
|
||||
This function calls getaddrinfo and returns the same addrinfo linked list
|
||||
that getaddrinfo produces. Returns NULL for any error or failure to resolve.
|
||||
You need to call freeaddrinfo on the result if non-NULL. */
|
||||
struct addrinfo *resolve_all(const char *hostname, int pf)
|
||||
{
|
||||
struct addrinfo *resolve_all(const char *hostname, int pf) {
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result;
|
||||
int rc;
|
||||
@@ -458,8 +457,8 @@ struct addrinfo *resolve_all(const char *hostname, int pf)
|
||||
/* Send a pre-built IPv4 packet. Handles fragmentation and whether to send with
|
||||
an ethernet handle or a socket. */
|
||||
static int send_ipv4_packet(int sd, const struct eth_nfo *eth,
|
||||
const struct sockaddr_in *dst,
|
||||
const u8 *packet, unsigned int packetlen) {
|
||||
const struct sockaddr_in *dst,
|
||||
const u8 *packet, unsigned int packetlen) {
|
||||
struct ip *ip = (struct ip *) packet;
|
||||
int res;
|
||||
|
||||
@@ -467,10 +466,10 @@ static int send_ipv4_packet(int sd, const struct eth_nfo *eth,
|
||||
assert((int) packetlen > 0);
|
||||
|
||||
/* Fragmentation requested && packet is bigger than MTU */
|
||||
if(o.fragscan && !(ntohs(ip->ip_off) & IP_DF) &&
|
||||
(packetlen - ip->ip_hl * 4 > (unsigned int) o.fragscan)){
|
||||
if (o.fragscan && !(ntohs(ip->ip_off) & IP_DF) &&
|
||||
(packetlen - ip->ip_hl * 4 > (unsigned int) o.fragscan)) {
|
||||
res = send_frag_ip_packet(sd, eth, dst, packet, packetlen, o.fragscan);
|
||||
}else{
|
||||
} else {
|
||||
res = send_ip_packet_eth_or_sd(sd, eth, dst, packet, packetlen);
|
||||
}
|
||||
if (res != -1)
|
||||
@@ -480,8 +479,8 @@ static int send_ipv4_packet(int sd, const struct eth_nfo *eth,
|
||||
}
|
||||
|
||||
static int send_ipv6_packet(int sd, const struct eth_nfo *eth,
|
||||
const struct sockaddr_in6 *dst,
|
||||
const u8 *packet, unsigned int packetlen){
|
||||
const struct sockaddr_in6 *dst,
|
||||
const u8 *packet, unsigned int packetlen) {
|
||||
int res;
|
||||
|
||||
res = send_ipv6_packet_eth_or_sd(sd, eth, dst, packet, packetlen);
|
||||
@@ -492,8 +491,8 @@ static int send_ipv6_packet(int sd, const struct eth_nfo *eth,
|
||||
}
|
||||
|
||||
int send_ip_packet(int sd, const struct eth_nfo *eth,
|
||||
const struct sockaddr_storage *dst,
|
||||
const u8 *packet, unsigned int packetlen) {
|
||||
const struct sockaddr_storage *dst,
|
||||
const u8 *packet, unsigned int packetlen) {
|
||||
struct ip *ip = (struct ip *) packet;
|
||||
|
||||
/* Ensure there's enough to read ip->ip_v at least. */
|
||||
@@ -519,7 +518,7 @@ int send_ip_packet(int sd, const struct eth_nfo *eth,
|
||||
ipv4_pseudoheader_cksum, this knows about STUPID_SOLARIS_CHECKSUM_BUG and
|
||||
takes care of o.badsum. */
|
||||
static u16 ipv4_cksum(const struct in_addr *src, const struct in_addr *dst,
|
||||
u8 proto, const void *data, u16 len) {
|
||||
u8 proto, const void *data, u16 len) {
|
||||
u16 sum;
|
||||
|
||||
#if STUPID_SOLARIS_CHECKSUM_BUG
|
||||
@@ -540,7 +539,7 @@ static u16 ipv4_cksum(const struct in_addr *src, const struct in_addr *dst,
|
||||
/* Return an IPv6 pseudoheader checksum for the given protocol and data. Unlike
|
||||
ipv6_pseudoheader_cksum, this takes care of o.badsum. */
|
||||
static u16 ipv6_cksum(const struct in6_addr *src, const struct in6_addr *dst,
|
||||
u8 nxt, const void *data, u16 len) {
|
||||
u8 nxt, const void *data, u16 len) {
|
||||
u16 sum;
|
||||
|
||||
sum = ipv6_pseudoheader_cksum(src, dst, nxt, len, data);
|
||||
@@ -589,8 +588,7 @@ static inline int fill_ip_raw(struct ip *ip, int packetlen, const u8 *ipopt,
|
||||
|
||||
#if HAVE_IP_IP_SUM
|
||||
ip->ip_sum = 0;
|
||||
ip->ip_sum =
|
||||
in_cksum((unsigned short *) ip, sizeof(struct ip) + ipoptlen);
|
||||
ip->ip_sum = in_cksum((unsigned short *) ip, sizeof(struct ip) + ipoptlen);
|
||||
#endif
|
||||
return (sizeof(struct ip) + ipoptlen);
|
||||
}
|
||||
@@ -610,12 +608,12 @@ u8 *build_ip_raw(const struct in_addr *source,
|
||||
struct ip *ip = (struct ip *) packet;
|
||||
static int myttl = 0;
|
||||
|
||||
/* check that required fields are there and not too silly */
|
||||
/* check that required fields are there and not too silly */
|
||||
assert(source);
|
||||
assert(victim);
|
||||
assert(ipoptlen % 4 == 0);
|
||||
|
||||
/* Time to live */
|
||||
/* Time to live */
|
||||
if (ttl == -1) {
|
||||
myttl = (get_random_uint() % 23) + 37;
|
||||
} else {
|
||||
@@ -726,10 +724,10 @@ u8 *build_tcp_raw(const struct in_addr *source,
|
||||
u8 *ip;
|
||||
|
||||
tcp = (struct tcp_hdr *) build_tcp(sport, dport, seq, ack, reserved, flags,
|
||||
window, urp, tcpopt, tcpoptlen, data, datalen, &tcplen);
|
||||
window, urp, tcpopt, tcpoptlen, data, datalen, &tcplen);
|
||||
tcp->th_sum = ipv4_cksum(source, victim, IPPROTO_TCP, tcp, tcplen);
|
||||
ip = build_ip_raw(source, victim, IPPROTO_TCP, ttl, ipid, tos, df,
|
||||
ipopt, ipoptlen, (char *) tcp, tcplen, packetlen);
|
||||
ipopt, ipoptlen, (char *) tcp, tcplen, packetlen);
|
||||
free(tcp);
|
||||
|
||||
return ip;
|
||||
@@ -752,10 +750,10 @@ u8 *build_tcp_raw_ipv6(const struct in6_addr *source,
|
||||
u8 *ipv6;
|
||||
|
||||
tcp = (struct tcp_hdr *) build_tcp(sport, dport, seq, ack, reserved, flags,
|
||||
window, urp, tcpopt, tcpoptlen, data, datalen, &tcplen);
|
||||
window, urp, tcpopt, tcpoptlen, data, datalen, &tcplen);
|
||||
tcp->th_sum = ipv6_cksum(source, victim, IPPROTO_TCP, tcp, tcplen);
|
||||
ipv6 = build_ipv6_raw(source, victim, tc, flowlabel, IPPROTO_TCP, hoplimit,
|
||||
(char *) tcp, tcplen, packetlen);
|
||||
(char *) tcp, tcplen, packetlen);
|
||||
free(tcp);
|
||||
|
||||
return ipv6;
|
||||
@@ -858,7 +856,7 @@ u8 *build_udp_raw(const struct in_addr *source, const struct in_addr *victim,
|
||||
udp = (struct udp_hdr *) build_udp(sport, dport, data, datalen, &udplen);
|
||||
udp->uh_sum = ipv4_cksum(source, victim, IPPROTO_UDP, udp, udplen);
|
||||
ip = build_ip_raw(source, victim, IPPROTO_UDP, ttl, ipid, tos, df,
|
||||
ipopt, ipoptlen, (char *) udp, udplen, packetlen);
|
||||
ipopt, ipoptlen, (char *) udp, udplen, packetlen);
|
||||
free(udp);
|
||||
|
||||
return ip;
|
||||
@@ -881,7 +879,7 @@ u8 *build_udp_raw_ipv6(const struct in6_addr *source,
|
||||
udp = (struct udp_hdr *) build_udp(sport, dport, data, datalen, &udplen);
|
||||
udp->uh_sum = ipv6_cksum(source, victim, IPPROTO_UDP, udp, udplen);
|
||||
ipv6 = build_ipv6_raw(source, victim, tc, flowlabel, IPPROTO_UDP, hoplimit,
|
||||
(char *) udp, udplen, packetlen);
|
||||
(char *) udp, udplen, packetlen);
|
||||
free(udp);
|
||||
|
||||
return ipv6;
|
||||
@@ -983,7 +981,7 @@ u8 *build_sctp_raw(const struct in_addr *source,
|
||||
|
||||
sctp = build_sctp(sport, dport, vtag, chunks, chunkslen, data, datalen, &sctplen);
|
||||
ip = build_ip_raw(source, victim, IPPROTO_SCTP, ttl, ipid, tos, df,
|
||||
ipopt, ipoptlen, (char *) sctp, sctplen, packetlen);
|
||||
ipopt, ipoptlen, (char *) sctp, sctplen, packetlen);
|
||||
free(sctp);
|
||||
|
||||
return ip;
|
||||
@@ -999,7 +997,7 @@ u8 *build_sctp_raw_ipv6(const struct in6_addr *source,
|
||||
|
||||
sctp = build_sctp(sport, dport, vtag, chunks, chunkslen, data, datalen, &sctplen);
|
||||
ipv6 = build_ipv6_raw(source, victim, tc, flowlabel, IPPROTO_SCTP, hoplimit,
|
||||
(char *) sctp, sctplen, packetlen);
|
||||
(char *) sctp, sctplen, packetlen);
|
||||
free(sctp);
|
||||
|
||||
return ipv6;
|
||||
@@ -1110,12 +1108,12 @@ u8 *build_icmpv6_raw(const struct in6_addr *source,
|
||||
|
||||
icmpv6->icmpv6_cksum = 0;
|
||||
icmpv6->icmpv6_cksum = ipv6_pseudoheader_cksum(source, victim,
|
||||
IPPROTO_ICMPV6, icmplen, icmpv6);
|
||||
IPPROTO_ICMPV6, icmplen, icmpv6);
|
||||
if (o.badsum)
|
||||
icmpv6->icmpv6_cksum--;
|
||||
|
||||
ipv6 = build_ipv6_raw(source, victim, tc, flowlabel, IPPROTO_ICMPV6, hoplimit,
|
||||
packet, icmplen, packetlen);
|
||||
packet, icmplen, packetlen);
|
||||
|
||||
free(packet);
|
||||
return ipv6;
|
||||
@@ -1190,8 +1188,7 @@ int readtcppacket(const u8 *packet, int readdata) {
|
||||
|
||||
struct ip *ip = (struct ip *) packet;
|
||||
struct tcp_hdr *tcp = (struct tcp_hdr *) (packet + sizeof(struct ip));
|
||||
const unsigned char *data =
|
||||
packet + sizeof(struct ip) + sizeof(struct tcp_hdr);
|
||||
const unsigned char *data = packet + sizeof(struct ip) + sizeof(struct tcp_hdr);
|
||||
int tot_len;
|
||||
struct in_addr bullshit, bullshit2;
|
||||
char sourcehost[16];
|
||||
@@ -1205,7 +1202,7 @@ int readtcppacket(const u8 *packet, int readdata) {
|
||||
|
||||
bullshit.s_addr = ip->ip_src.s_addr;
|
||||
bullshit2.s_addr = ip->ip_dst.s_addr;
|
||||
/* this is gay */
|
||||
/* this is gay */
|
||||
realfrag = htons(ntohs(ip->ip_off) & IP_OFFMASK);
|
||||
tot_len = htons(ip->ip_len);
|
||||
strncpy(sourcehost, inet_ntoa(bullshit), 16);
|
||||
@@ -1480,7 +1477,7 @@ static bool validatepkt(u8 *ipc, unsigned *len) {
|
||||
should specify that in pcap_open_live() */
|
||||
/* to_usec is the timeout period in microseconds -- use 0 to skip the
|
||||
test and -1 to block forever. Note that we don't interrupt pcap, so
|
||||
low values (and 0) degenerate to the timeout specified
|
||||
low values (and 0) degenerate to the timeout specified
|
||||
in pcap_open_live() */
|
||||
/* If rcvdtime is non-null and a packet is returned, rcvd will be
|
||||
filled with the time that packet was captured from the wire by
|
||||
@@ -1532,7 +1529,7 @@ char *readip_pcap(pcap_t *pd, unsigned int *len, long to_usec,
|
||||
to_usec = 0;
|
||||
}
|
||||
|
||||
/* New packet capture device, need to recompute offset */
|
||||
/* New packet capture device, need to recompute offset */
|
||||
if ((datalink = pcap_datalink(pd)) < 0)
|
||||
fatal("Cannot obtain datalink information: %s", pcap_geterr(pd));
|
||||
|
||||
@@ -1715,7 +1712,7 @@ char *readip_pcap(pcap_t *pd, unsigned int *len, long to_usec,
|
||||
// sent the packet (which is according to gettimeofday() in nbase). For now, I will sadly have to
|
||||
// use gettimeofday() for Windows in this case
|
||||
// Actually I now allow .05 discrepancy. So maybe this isn't needed. I'll comment out for now.
|
||||
// Nope: it is still needed at least for Windows. Sometimes the time from he pcap header is a
|
||||
// Nope: it is still needed at least for Windows. Sometimes the time from he pcap header is a
|
||||
// COUPLE SECONDS before the gettimeofday() results :(.
|
||||
#if defined(WIN32) || defined(__amigaos__)
|
||||
gettimeofday(&tv_end, NULL);
|
||||
@@ -1746,7 +1743,7 @@ char *readip_pcap(pcap_t *pd, unsigned int *len, long to_usec,
|
||||
that can be used for packet tracing. This is intended to be used
|
||||
by Nmap only. Any other calling this should pass NULL instead. */
|
||||
int read_na_pcap(pcap_t *pd, u8 *sendermac, struct sockaddr_in6 *senderIP, long to_usec,
|
||||
struct timeval *rcvdtime, bool *has_mac) {
|
||||
struct timeval *rcvdtime, bool *has_mac) {
|
||||
struct ip *ip_tmp;
|
||||
struct icmpv6_hdr *icmp6_header;
|
||||
struct icmpv6_msg_nd *na;
|
||||
@@ -1772,30 +1769,29 @@ int read_na_pcap(pcap_t *pd, u8 *sendermac, struct sockaddr_in6 *senderIP, long
|
||||
}
|
||||
|
||||
do {
|
||||
ip_tmp = (struct ip *) readip_pcap(pd, &datalen, to_usec, rcvdtime,
|
||||
&linknfo, true);
|
||||
if(ip_tmp){ //Check Neighbor Advertisement Packet.
|
||||
ip_tmp = (struct ip *) readip_pcap(pd, &datalen, to_usec, rcvdtime, &linknfo, true);
|
||||
if (ip_tmp) { //Check Neighbor Advertisement Packet.
|
||||
/* OK, we got a packet. Most packet validity tests are taken care
|
||||
* of in readip_pcap, so this is simple
|
||||
*/
|
||||
data = ip_get_data(ip_tmp, &datalen, &hdr);
|
||||
if (data == NULL)
|
||||
continue;
|
||||
if (hdr.proto == IPPROTO_ICMPV6){
|
||||
if (hdr.proto == IPPROTO_ICMPV6) {
|
||||
icmp6_header = (struct icmpv6_hdr *)data;
|
||||
na = (struct icmpv6_msg_nd *) ((unsigned char*)data + ICMPV6_HDR_LEN);
|
||||
if (icmp6_header->icmpv6_type == ICMPV6_NEIGHBOR_ADVERTISEMENT &&
|
||||
icmp6_header->icmpv6_code == 0){
|
||||
icmp6_header->icmpv6_code == 0) {
|
||||
//Set target IPv6 address
|
||||
senderIP->sin6_family = AF_INET6;
|
||||
memcpy(&senderIP->sin6_addr.s6_addr, &na->icmpv6_target, 16);
|
||||
//Set MAC
|
||||
if (datalen == ICMPV6_HDR_LEN + sizeof(struct icmpv6_msg_nd)){
|
||||
if (na->icmpv6_option_type == 2 && na->icmpv6_option_length == 1){
|
||||
if (datalen == ICMPV6_HDR_LEN + sizeof(struct icmpv6_msg_nd)) {
|
||||
if (na->icmpv6_option_type == 2 && na->icmpv6_option_length == 1) {
|
||||
*has_mac = true;
|
||||
memcpy(sendermac, &na->icmpv6_mac, 6);
|
||||
}
|
||||
} else{
|
||||
} else {
|
||||
*has_mac = false;
|
||||
}
|
||||
}
|
||||
@@ -1946,17 +1942,16 @@ bool setTargetNextHopMAC(Target *target) {
|
||||
/* OK, the last choice is to send our own damn ARP request (and
|
||||
retransmissions if necessary) to determine the MAC */
|
||||
target->SourceSockAddr(&srcss, NULL);
|
||||
if (target->af() == AF_INET){
|
||||
if (target->af() == AF_INET) {
|
||||
if (doArp(target->deviceFullName(), target->SrcMACAddress(),
|
||||
&srcss, &targetss, mac, PacketTrace::traceArp)) {
|
||||
mac_cache_set(&targetss, mac);
|
||||
target->setNextHopMACAddress(mac);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (target->af() == AF_INET6){
|
||||
} else if (target->af() == AF_INET6) {
|
||||
if (doND(target->deviceFullName(), target->SrcMACAddress(),
|
||||
&srcss, &targetss, mac, PacketTrace::traceND)) {
|
||||
&srcss, &targetss, mac, PacketTrace::traceND)) {
|
||||
mac_cache_set(&targetss, mac);
|
||||
target->setNextHopMACAddress(mac);
|
||||
return true;
|
||||
@@ -1970,8 +1965,7 @@ bool setTargetNextHopMAC(Target *target) {
|
||||
|
||||
/* Like to getTargetNextHopMAC(), but for arbitrary hosts (not Targets) */
|
||||
bool getNextHopMAC(const char *iface, const u8 *srcmac, const struct sockaddr_storage *srcss,
|
||||
const struct sockaddr_storage *dstss, u8 *dstmac)
|
||||
{
|
||||
const struct sockaddr_storage *dstss, u8 *dstmac) {
|
||||
arp_t *a;
|
||||
struct arp_entry ae;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user