From 08efb5ce1a79b8e04e4b9092ba16fe912dd9ae75 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 3 May 2013 19:31:09 +0000 Subject: [PATCH] Revert r30833, removal of obtainRawSocket. This was prematurely merged from nmap-npingchanges. obtainRawSocket is still used here. --- nping/utils_net.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++ nping/utils_net.h | 1 + 2 files changed, 57 insertions(+) diff --git a/nping/utils_net.cc b/nping/utils_net.cc index 2011ef9f3..6d0b053ab 100644 --- a/nping/utils_net.cc +++ b/nping/utils_net.cc @@ -431,6 +431,13 @@ int getPacketStrInfo(const char *proto, const u8 *packet, u32 len, u8 *dstbuff, dstbuff[dstlen-1]=0; /* Just to be sure, NULL-terminate the last position*/ }else if( !strcasecmp(proto, "ARP") || !strcasecmp(proto, "RARP") ){ return arppackethdrinfo(packet, len, dstbuff, dstlen); + }else if( !strcasecmp(proto, "IPv6_NO_HEADER") || o.ipv6UsingSocket() ){ + if( o.getMode()==TCP ) + return tcppackethdrinfo(packet, len, dstbuff, dstlen, detail, ss_src, ss_dst); + else if ( o.getMode()==UDP ) + return udppackethdrinfo(packet, len, dstbuff, dstlen, detail, ss_src, ss_dst); + else + nping_fatal(QT_3, "getPacketStrInfo(): Unable to determinate transport layer protocol"); }else{ nping_fatal(QT_3, "getPacketStrInfo(): Unkwnown protocol"); } @@ -1442,6 +1449,55 @@ u16 *getDstPortFromUDPHeader(u8 *pkt, size_t pktLen){ } /* End of getDstPortFromUDPHeader() */ +int obtainRawSocket(){ + int rawipsd=0; + int protocol=0; + int one=1; + + if( o.ipv6() ){ + switch( o.getMode() ){ + + case TCP: + protocol = IPPROTO_TCP; + break; + + case UDP: + protocol = IPPROTO_UDP; + break; + + case ICMP: + protocol = IPPROTO_ICMPV6; + break; + + case ARP: + nping_warning(QT_2,"Warning: createRawSocket() should not be called in ARP mode."); + return 0; + break; + + default: + nping_fatal(QT_3, "createRawSocket(): NpingOps::getMode() does not return a valid mode. Please report this bug."); + break; + + } + if ((rawipsd = socket(AF_INET6, SOCK_RAW, protocol)) < 0 ) + nping_fatal(QT_3,"Couldn't acquire IPv6 raw socket. Are you root?"); + + }else{ + if ((rawipsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 ) + nping_fatal(QT_3,"Couldn't acquire IPv4 raw socket. Are you root?"); + /* Tell the kernel we are including our own IP Header (call to + * setsockopt passing option IP_HDRINCL) */ + sethdrinclude(rawipsd); + } + + /* Allow broadcast addresses */ + if (setsockopt(rawipsd, SOL_SOCKET, SO_BROADCAST, (const char *)&one, sizeof(int)) == -1) + nping_warning(QT_2,"Failed to set SO_BROADCAST on raw socket."); + + return rawipsd; +} /* End of obtainRawSocket() */ + + /** This function parses Linux file /proc/net/if_inet6 and returns a list of network interfaces that are configured for IPv6. @param ifbuff should be a buffer big enough to hold info for max_ifaces diff --git a/nping/utils_net.h b/nping/utils_net.h index b3506705f..6f43d116e 100644 --- a/nping/utils_net.h +++ b/nping/utils_net.h @@ -150,6 +150,7 @@ u16 *getSrcPortFromIPPacket(u8 *pkt, size_t pktLen); u16 *getDstPortFromIPPacket(u8 *pkt, size_t pktLen); u16 *getDstPortFromTCPHeader(u8 *pkt, size_t pktLen); u16 *getDstPortFromUDPHeader(u8 *pkt, size_t pktLen); +int obtainRawSocket(); #define DEVNAMELEN 16 #define PATH_PROC_IFINET6 "/proc/net/if_inet6"