Skip to content

Commit 306e2c6

Browse files
committed
Copy the patch from the SVN.
1 parent 6279872 commit 306e2c6

File tree

8 files changed

+243
-206
lines changed

8 files changed

+243
-206
lines changed

nsock/include/nsock.h

Lines changed: 8 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -443,6 +443,14 @@ int nsock_iod_set_localaddr(nsock_iod iod, struct sockaddr_storage *ss, size_t s
443
* destroyed */
443
* destroyed */
444
int nsock_iod_set_ipoptions(nsock_iod iod, void *ipopts, size_t ipoptslen);
444
int nsock_iod_set_ipoptions(nsock_iod iod, void *ipopts, size_t ipoptslen);
445

445

446+
/* Sets IPv4 TTL to apply before connect(). */
447+
int nsock_iod_set_ttl(nsock_iod nsi, int ttl);
448+
449+
/* Sets connection lingering options to apply before connect(). It makes a copy
450+
* of the options, so you can free() yours if necessary. This copy is freed
451+
* when the iod is destroyed */
452+
int nsock_iod_set_linger(nsock_iod nsi, struct linger lingeropts);
453+
446
/* Returns that host/port/protocol information for the last communication (or
454
/* Returns that host/port/protocol information for the last communication (or
447
* comm. attempt) this nsi has been involved with. By "involved" with I mean
455
* comm. attempt) this nsi has been involved with. By "involved" with I mean
448
* interactions like establishing (or trying to) a connection or sending a UDP
456
* interactions like establishing (or trying to) a connection or sending a UDP

nsock/src/nsock_connect.c

Lines changed: 18 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -104,6 +104,21 @@ static int mksock_set_ipopts(struct npool *ms, struct niod *iod) {
104
return 0;
104
return 0;
105
}
105
}
106

106

107+
static int mksock_set_ttl(struct npool *ms, struct niod *iod) {
108+
int rc;
109+
110+
errno = 0;
111+
rc = setsockopt(iod->sd, IPPROTO_IP, IP_TTL, (const char *) &(iod->ttl),
112+
iod->ttl);
113+
if (rc == -1) {
114+
int err = socket_errno();
115+
116+
nsock_log_error("Setting of TTL failed (IOD #%li): %s (%d)",
117+
iod->id, socket_strerror(err), err);
118+
}
119+
return 0;
120+
}
121+
107
static int mksock_bind_device(struct npool *ms, struct niod *iod) {
122
static int mksock_bind_device(struct npool *ms, struct niod *iod) {
108
int rc;
123
int rc;
109

124

@@ -159,6 +174,9 @@ static int nsock_make_socket(struct npool *ms, struct niod *iod, int family, int
159
if (iod->ipoptslen && family == AF_INET)
174
if (iod->ipoptslen && family == AF_INET)
160
mksock_set_ipopts(ms, iod);
175
mksock_set_ipopts(ms, iod);
161

176

177+
if (iod->ttl != -1)
178+
mksock_set_ttl(ms, iod);
179+
162
if (ms->device)
180
if (ms->device)
163
mksock_bind_device(ms, iod);
181
mksock_bind_device(ms, iod);
164

182

nsock/src/nsock_event.c

Lines changed: 12 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -460,7 +460,18 @@ struct nevent *event_new(struct npool *nsp, enum nse_type type,
460
#endif
460
#endif
461

461

462
if (timeout_msecs != -1) {
462
if (timeout_msecs != -1) {
463-
assert(timeout_msecs >= 0);
463+
/* assert(timeout_msecs >= 0); */
464+
465+
/* FIXME: port scanning behind SOCKS4 fails on the assertion above, most
466+
likely because of problems with the reliability of gettimeofday. This
467+
conditional statement is a kludge meant to make SOCKS4 port scanning
468+
work. This should be removed and the underlying problem actually
469+
solved.
470+
471+
Also see here: http://seclists.org/nmap-dev/2015/q2/375
472+
*/
473+
if (timeout_msecs < 0)
474+
timeout_msecs = 0;
464
TIMEVAL_MSEC_ADD(nse->timeout, nsock_tod, timeout_msecs);
475
TIMEVAL_MSEC_ADD(nse->timeout, nsock_tod, timeout_msecs);
465
}
476
}
466

477

nsock/src/nsock_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -295,6 +295,12 @@ struct niod {
295
void *ipopts;
295
void *ipopts;
296
int ipoptslen;
296
int ipoptslen;
297

297

298+
int ttl;
299+
300+
/* structure used to trigger connection lingering */
301+
struct linger lingeropts;
302+
int lingeropts_set;
303+
298
/* Pointer to mspcap struct (used only if pcap support is included) */
304
/* Pointer to mspcap struct (used only if pcap support is included) */
299
void *pcap;
305
void *pcap;
300

306

nsock/src/nsock_iod.c

Lines changed: 25 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -136,6 +136,9 @@ nsock_iod nsock_iod_new2(nsock_pool nsockp, int sd, void *userdata) {
136
nsi->ipopts = NULL;
136
nsi->ipopts = NULL;
137
nsi->ipoptslen = 0;
137
nsi->ipoptslen = 0;
138

138

139+
nsi->ttl = -1;
140+
nsi->lingeropts_set = 0;
141+
139
#if HAVE_OPENSSL
142
#if HAVE_OPENSSL
140
nsi->ssl_session = NULL;
143
nsi->ssl_session = NULL;
141
#endif
144
#endif
@@ -403,6 +406,28 @@ int nsock_iod_set_ipoptions(nsock_iod iod, void *opts, size_t optslen) {
403
return 0;
406
return 0;
404
}
407
}
405

408

409+
/* Sets IPv4 TTL to apply before connect(). */
410+
int nsock_iod_set_ttl(nsock_iod iod, int ttl) {
411+
struct niod *nsi = (struct niod *)iod;
412+
413+
assert(nsi);
414+
415+
nsi->ttl = ttl;
416+
return 0;
417+
}
418+
419+
/* Sets connection lingering options to apply before connect(). It makes a copy
420+
* of the options, so you can free() yours if necessary. This copy is freed
421+
* when the iod is destroyed */
422+
int nsock_iod_set_linger(nsock_iod nsi, struct linger lingeropts_arg)
423+
{
424+
struct niod *iod = (struct niod *)nsi;
425+
assert(iod);
426+
iod->lingeropts = lingeropts_arg;
427+
iod->lingeropts_set = 1;
428+
return 0;
429+
}
430+
406
/* I didn't want to do this. Its an ugly hack, but I suspect it will be
431
/* I didn't want to do this. Its an ugly hack, but I suspect it will be
407
* necessary. I certainly can't reproduce in nsock EVERYTHING you might want
432
* necessary. I certainly can't reproduce in nsock EVERYTHING you might want
408
* to do with a socket. So I'm offering you this function to obtain the socket
433
* to do with a socket. So I'm offering you this function to obtain the socket

scan_engine.cc

Lines changed: 17 additions & 9 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -835,7 +835,10 @@ static void set_default_port_state(std::vector<Target *> &targets, stype scantyp
835
case ACK_SCAN:
835
case ACK_SCAN:
836
case WINDOW_SCAN:
836
case WINDOW_SCAN:
837
case CONNECT_SCAN:
837
case CONNECT_SCAN:
838-
(*target)->ports.setDefaultPortState(IPPROTO_TCP, PORT_FILTERED);
838+
if (o.proxy_chain)
839+
(*target)->ports.setDefaultPortState(IPPROTO_TCP, PORT_CLOSEDFILTERED);
840+
else
841+
(*target)->ports.setDefaultPortState(IPPROTO_TCP, PORT_FILTERED);
839
break;
842
break;
840
case SCTP_INIT_SCAN:
843
case SCTP_INIT_SCAN:
841
(*target)->ports.setDefaultPortState(IPPROTO_SCTP, PORT_FILTERED);
844
(*target)->ports.setDefaultPortState(IPPROTO_SCTP, PORT_FILTERED);
@@ -1567,11 +1570,10 @@ void HostScanStats::destroyOutstandingProbe(std::list<UltraProbe *>::iterator pr
1567
num_probes_waiting_retransmit--;
1570
num_probes_waiting_retransmit--;
1568
}
1571
}
1569

1572

1570-
/* Remove it from scan watch lists, if it exists on them. */
1571-
if (probe->type == UltraProbe::UP_CONNECT && probe->CP()->sd > 0)
1572-
USI->gstats->CSI->clearSD(probe->CP()->sd);
1573-
1574
probes_outstanding.erase(probeI);
1573
probes_outstanding.erase(probeI);
1574+
if (o.debugging > 8)
1575+
log_write(LOG_PLAIN, "HostScanStats::destroyOutstandingProbe[%p]"
1576+
" - Deleting probe dport=%d\n", probe, probe->dport());
1575
delete probe;
1577
delete probe;
1576
}
1578
}
1577

1579

@@ -1686,12 +1688,15 @@ void HostScanStats::markProbeTimedout(std::list<UltraProbe *>::iterator probeI)
1686
/* I'll leave it in the queue in case some response ever does come */
1688
/* I'll leave it in the queue in case some response ever does come */
1687
num_probes_waiting_retransmit++;
1689
num_probes_waiting_retransmit++;
1688

1690

1689-
if (probe->type == UltraProbe::UP_CONNECT && probe->CP()->sd >= 0 ) {
1691+
if (probe->type == UltraProbe::UP_CONNECT && probe->CP()->connected ) {
1690
/* Free the socket as that is a valuable resource, though it is a shame
1692
/* Free the socket as that is a valuable resource, though it is a shame
1691
late responses will not be permitted */
1693
late responses will not be permitted */
1692-
USI->gstats->CSI->clearSD(probe->CP()->sd);
1694+
if (probe->CP()->connected) {
1693-
close(probe->CP()->sd);
1695+
if (o.debugging > 8)
1694-
probe->CP()->sd = -1;
1696+
log_write(LOG_PLAIN, "HostScanStats::markProbeTimedout: nsock_iod_delete (probe->dport()=%d)\n", probe->dport());
1697+
nsock_iod_delete(probe->CP()->sock_nsi, NSOCK_PENDING_SILENT);
1698+
probe->CP()->connected = false;
1699+
}
1695
}
1700
}
1696
}
1701
}
1697

1702

@@ -1953,6 +1958,9 @@ void HostScanStats::moveProbeToBench(std::list<UltraProbe *>::iterator probeI) {
1953
probe_bench.push_back(*probe->pspec());
1958
probe_bench.push_back(*probe->pspec());
1954
probes_outstanding.erase(probeI);
1959
probes_outstanding.erase(probeI);
1955
num_probes_waiting_retransmit--;
1960
num_probes_waiting_retransmit--;
1961+
if (o.debugging > 8)
1962+
log_write(LOG_PLAIN, "HostScanStats::moveProbeToBench[%p]"
1963+
" - Deleting probe dport=%d\n", probe, probe->dport());
1956
delete probe;
1964
delete probe;
1957
}
1965
}
1958

1966

scan_engine.h

Lines changed: 6 additions & 14 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -135,6 +135,7 @@
135
#include "tcpip.h"
135
#include "tcpip.h"
136
#include <list>
136
#include <list>
137
#include <vector>
137
#include <vector>
138+
#include "nsock.h"
138

139

139
struct probespec_tcpdata {
140
struct probespec_tcpdata {
140
u16 dport;
141
u16 dport;
@@ -218,7 +219,10 @@ class ConnectProbe {
218
public:
219
public:
219
ConnectProbe();
220
ConnectProbe();
220
~ConnectProbe();
221
~ConnectProbe();
221-
int sd; /* Socket descriptor used for connection. -1 if not valid. */
222+
bool connected;
223+
nsock_iod sock_nsi;
224+
int connect_result;
225+
bool self_connect;
222
};
226
};
223

227

224
struct IPExtraProbeData_icmp {
228
struct IPExtraProbeData_icmp {
@@ -359,21 +363,9 @@ class ConnectScanInfo {
359
ConnectScanInfo();
363
ConnectScanInfo();
360
~ConnectScanInfo();
364
~ConnectScanInfo();
361

365

362-
/* Watch a socket descriptor (add to fd_sets and maxValidSD). Returns
363-
true if the SD was absent from the list, false if you tried to
364-
watch an SD that was already being watched. */
365-
bool watchSD(int sd);
366-
367-
/* Clear SD from the fd_sets and maxValidSD. Returns true if the SD
368-
was in the list, false if you tried to clear an sd that wasn't
369-
there in the first place. */
370-
bool clearSD(int sd);
371-
int maxValidSD; /* The maximum socket descriptor in any of the fd_sets */
372-
fd_set fds_read;
373-
fd_set fds_write;
374-
fd_set fds_except;
375
int numSDs; /* Number of socket descriptors being watched */
366
int numSDs; /* Number of socket descriptors being watched */
376
int maxSocketsAllowed; /* No more than this many sockets may be created @once */
367
int maxSocketsAllowed; /* No more than this many sockets may be created @once */
368+
nsock_pool nsp;
377
};
369
};
378

370

379
class HostScanStats;
371
class HostScanStats;

0 commit comments

Comments
 (0)