mirror of
https://github.com/nmap/nmap.git
synced 2025-12-08 21:51:28 +00:00
Factor out an nmap_raw_socket function.
This does the common setup of a raw socket that we use everywhere.
This commit is contained in:
10
FPEngine.cc
10
FPEngine.cc
@@ -184,17 +184,9 @@ void FPNetworkControl::init(const char *ifname, devtype iftype) {
|
|||||||
fatal("dnet: failed to open device %s", ifname);
|
fatal("dnet: failed to open device %s", ifname);
|
||||||
this->rawsd = -1;
|
this->rawsd = -1;
|
||||||
} else {
|
} else {
|
||||||
#ifdef WIN32
|
|
||||||
win32_fatal_raw_sockets(ifname);
|
|
||||||
#endif
|
|
||||||
if (this->rawsd >= 0)
|
if (this->rawsd >= 0)
|
||||||
close(this->rawsd);
|
close(this->rawsd);
|
||||||
if ((this->rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
|
rawsd = nmap_raw_socket(ifname);
|
||||||
pfatal("Couldn't obtain raw socket in %s", __func__);
|
|
||||||
broadcast_socket(this->rawsd);
|
|
||||||
#ifndef WIN32
|
|
||||||
sethdrinclude(this->rawsd);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* De-register existing callers */
|
/* De-register existing callers */
|
||||||
|
|||||||
10
idle_scan.cc
10
idle_scan.cc
@@ -382,16 +382,8 @@ static void initialize_idleproxy(struct idle_proxy_info *proxy, char *proxyName,
|
|||||||
proxy->rawsd = -1;
|
proxy->rawsd = -1;
|
||||||
proxy->ethptr = &proxy->eth;
|
proxy->ethptr = &proxy->eth;
|
||||||
} else {
|
} else {
|
||||||
#ifdef WIN32
|
proxy->rawsd = nmap_raw_socket(proxy->host.deviceName());
|
||||||
win32_fatal_raw_sockets(proxy->host.deviceName());
|
|
||||||
#endif
|
|
||||||
if ((proxy->rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
|
|
||||||
pfatal("socket troubles in %s", __func__);
|
|
||||||
unblock_socket(proxy->rawsd);
|
unblock_socket(proxy->rawsd);
|
||||||
broadcast_socket(proxy->rawsd);
|
|
||||||
#ifndef WIN32
|
|
||||||
sethdrinclude(proxy->rawsd);
|
|
||||||
#endif
|
|
||||||
proxy->eth.ethsd = NULL;
|
proxy->eth.ethsd = NULL;
|
||||||
proxy->ethptr = NULL;
|
proxy->ethptr = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,14 +187,10 @@ static int ethernet_send (lua_State *L)
|
|||||||
static int ip_open (lua_State *L)
|
static int ip_open (lua_State *L)
|
||||||
{
|
{
|
||||||
nse_dnet_udata *udata = (nse_dnet_udata *) nseU_checkudata(L, 1, DNET_METATABLE, "dnet");
|
nse_dnet_udata *udata = (nse_dnet_udata *) nseU_checkudata(L, 1, DNET_METATABLE, "dnet");
|
||||||
udata->sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
udata->sock = nmap_raw_socket(NULL);
|
||||||
if (udata->sock == -1)
|
if (udata->sock == -1)
|
||||||
return luaL_error(L, "failed to open raw socket: %s (errno %d)",
|
return luaL_error(L, "failed to open raw socket: %s (errno %d)",
|
||||||
socket_strerror(socket_errno()), socket_errno());
|
socket_strerror(socket_errno()), socket_errno());
|
||||||
broadcast_socket(udata->sock);
|
|
||||||
#ifndef WIN32
|
|
||||||
sethdrinclude(udata->sock);
|
|
||||||
#endif
|
|
||||||
return nseU_success(L);
|
return nseU_success(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
osscan2.cc
11
osscan2.cc
@@ -1268,17 +1268,8 @@ HostOsScan::HostOsScan(Target *t) {
|
|||||||
fatal("%s: Failed to open ethernet device (%s)", __func__, t->deviceName());
|
fatal("%s: Failed to open ethernet device (%s)", __func__, t->deviceName());
|
||||||
rawsd = -1;
|
rawsd = -1;
|
||||||
} else {
|
} else {
|
||||||
/* Init our raw socket */
|
rawsd = nmap_raw_socket(t->deviceName());
|
||||||
#ifdef WIN32
|
|
||||||
win32_fatal_raw_sockets(t->deviceName());
|
|
||||||
#endif
|
|
||||||
if ((rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 )
|
|
||||||
pfatal("socket troubles in %s", __func__);
|
|
||||||
unblock_socket(rawsd);
|
unblock_socket(rawsd);
|
||||||
broadcast_socket(rawsd);
|
|
||||||
#ifndef WIN32
|
|
||||||
sethdrinclude(rawsd);
|
|
||||||
#endif
|
|
||||||
ethsd = NULL;
|
ethsd = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1662,20 +1662,11 @@ void UltraScanInfo::Init(vector<Target *> &Targets, struct scan_lists *pts, styp
|
|||||||
fatal("dnet: Failed to open device %s", Targets[0]->deviceName());
|
fatal("dnet: Failed to open device %s", Targets[0]->deviceName());
|
||||||
rawsd = -1;
|
rawsd = -1;
|
||||||
} else {
|
} else {
|
||||||
/* Initialize a raw socket */
|
rawsd = nmap_raw_socket(Targets[0]->deviceName());
|
||||||
#ifdef WIN32
|
|
||||||
win32_fatal_raw_sockets(Targets[0]->deviceName());
|
|
||||||
#endif
|
|
||||||
if ((rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 )
|
|
||||||
pfatal("socket troubles in %s", __func__);
|
|
||||||
/* We do not wan't to unblock the socket since we want to wait
|
/* We do not wan't to unblock the socket since we want to wait
|
||||||
if kernel send buffers fill up rather than get ENOBUF, and
|
if kernel send buffers fill up rather than get ENOBUF, and
|
||||||
we won't be receiving on the socket anyway
|
we won't be receiving on the socket anyway
|
||||||
unblock_socket(rawsd);*/
|
unblock_socket(rawsd);*/
|
||||||
broadcast_socket(rawsd);
|
|
||||||
#ifndef WIN32
|
|
||||||
sethdrinclude(rawsd);
|
|
||||||
#endif
|
|
||||||
ethsd = NULL;
|
ethsd = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
tcpip.cc
24
tcpip.cc
@@ -138,6 +138,30 @@ static PacketCounter PktCt;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Create a raw socket and do things that always apply to raw sockets:
|
||||||
|
* Emit a fatal error on Windows.
|
||||||
|
* Set SO_BROADCAST.
|
||||||
|
* Set IP_HDRINCL.
|
||||||
|
The socket is created with address family AF_INET, but may be usable for
|
||||||
|
AF_INET6, depending on the operating system.
|
||||||
|
|
||||||
|
The argument warning_device_name is used *only* in the Windows fatal error
|
||||||
|
message, and does not affect any socket characteristics. */
|
||||||
|
int nmap_raw_socket(const char *warning_device_name) {
|
||||||
|
int rawsd;
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
win32_fatal_raw_sockets(Targets[0]->deviceName());
|
||||||
|
#endif
|
||||||
|
if ((rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 )
|
||||||
|
pfatal("socket troubles in %s", __func__);
|
||||||
|
broadcast_socket(rawsd);
|
||||||
|
#ifndef WIN32
|
||||||
|
sethdrinclude(rawsd);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return rawsd;
|
||||||
|
}
|
||||||
|
|
||||||
/* Fill buf (up to buflen -- truncate if necessary but always
|
/* Fill buf (up to buflen -- truncate if necessary but always
|
||||||
terminate) with a short representation of the packet stats.
|
terminate) with a short representation of the packet stats.
|
||||||
|
|||||||
1
tcpip.h
1
tcpip.h
@@ -200,6 +200,7 @@ extern "C" {
|
|||||||
#define INET_ADDRSTRLEN 16
|
#define INET_ADDRSTRLEN 16
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
int nmap_raw_socket(const char *warning_device_name);
|
||||||
|
|
||||||
/* Used for tracing all packets sent or received (eg the
|
/* Used for tracing all packets sent or received (eg the
|
||||||
--packet-trace option) */
|
--packet-trace option) */
|
||||||
|
|||||||
@@ -851,16 +851,7 @@ TracerouteState::TracerouteState(std::vector<Target *> &targets) {
|
|||||||
fatal("dnet: failed to open device %s", targets[0]->deviceName());
|
fatal("dnet: failed to open device %s", targets[0]->deviceName());
|
||||||
rawsd = -1;
|
rawsd = -1;
|
||||||
} else {
|
} else {
|
||||||
#ifdef WIN32
|
rawsd = nmap_raw_socket(targets[0]->deviceName());
|
||||||
win32_fatal_raw_sockets(targets[0]->deviceName());
|
|
||||||
#endif
|
|
||||||
rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
|
||||||
if (rawsd == -1)
|
|
||||||
pfatal("traceroute: socket troubles");
|
|
||||||
broadcast_socket(rawsd);
|
|
||||||
#ifndef WIN32
|
|
||||||
sethdrinclude(rawsd);
|
|
||||||
#endif
|
|
||||||
ethsd = NULL;
|
ethsd = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user