1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Moved function max_sd() to libnetutil. Also, two new helper functions have been added.

This commit is contained in:
luis
2010-10-20 15:57:50 +00:00
parent 00e4f1637d
commit d0a4d0ad50
4 changed files with 101 additions and 47 deletions

View File

@@ -131,6 +131,10 @@
#define NETINET_IP_H #define NETINET_IP_H
#endif #endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include "netutil.h" #include "netutil.h"
#define NBASE_MAX_ERR_STR_LEN 1024 /* Max length of an error message */ #define NBASE_MAX_ERR_STR_LEN 1024 /* Max length of an error message */
@@ -3725,3 +3729,82 @@ char *grab_next_host_spec(FILE *inputfd, bool random, int argc, char **fakeargv)
return host_spec; return host_spec;
} }
/** Tries to increase the open file descriptor limit for this process.
* @param "desired" is the number of desired max open descriptors. Pass a
* negative value to set the maximum allowed.
* @return the number of max open descriptors that could be set, or 0 in case
* of failure.
* @warning if "desired" is less than the current limit, no action is
* performed. This function may only be used to increase the limit, not to
* decrease it. */
int set_max_open_descriptors(int desired_max) {
#ifndef WIN32
struct rlimit r;
int maxfds=-1;
int flag=0;
#if (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE))
#ifdef RLIMIT_NOFILE
flag=RLIMIT_NOFILE; /* Linux */
#else
flag=RLIMIT_OFILE; /* BSD */
#endif
if (!getrlimit(flag, &r)) {
/* If current limit is less than the desired, try to increase it */
if(r.rlim_cur < (rlim_t)desired_max){
if(desired_max<0){
r.rlim_cur=r.rlim_max; /* Set maximum */
}else{
r.rlim_cur = MIN( (int)r.rlim_max, desired_max );
}
if (setrlimit(flag, &r))
; // netutil_debug("setrlimit(%d, %p) failed", flag, r);
if (!getrlimit(flag, &r)) {
maxfds = r.rlim_cur;
return maxfds;
}else {
return 0;
}
}
}
#endif /* (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE)) */
#endif /* !WIN32 */
return 0;
}
/** Returns the open file descriptor limit for this process.
* @return the number of max open descriptors or 0 in case of failure. */
int get_max_open_descriptors() {
#ifndef WIN32
struct rlimit r;
int flag=0;
#if (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE))
#ifdef RLIMIT_NOFILE
flag=RLIMIT_NOFILE; /* Linux */
#else
flag=RLIMIT_OFILE; /* BSD */
#endif
if (!getrlimit(flag, &r)) {
return (int)r.rlim_cur;
}
#endif /* (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE)) */
#endif /* !WIN32 */
return 0;
}
/* Maximize the open file descriptor limit for this process go up to the
max allowed */
int max_sd() {
return set_max_open_descriptors(-1);
}

View File

@@ -474,4 +474,22 @@ char *grab_next_host_spec(FILE *inputfd, bool random, int argc, char **fakeargv)
int DnetName2PcapName(const char *dnetdev, char *pcapdev, int pcapdevlen); int DnetName2PcapName(const char *dnetdev, char *pcapdev, int pcapdevlen);
#endif #endif
/** Tries to increase the open file descriptor limit for this process.
* @param "desired" is the number of desired max open descriptors. Pass a
* negative value to set the maximum allowed.
* @return the number of max open descriptors that could be set, or 0 in case
* of failure.
* @warning if "desired" is less than the current limit, no action is
* performed. This function may only be used to increase the limit, not to
* decrease it. */
int set_max_open_descriptors(int desired_max);
/** Returns the open file descriptor limit for this process.
* @return the number of max open descriptors or 0 in case of failure. */
int get_max_open_descriptors();
/* Maximize the open file descriptor limit for this process go up to the
max allowed */
int max_sd();
#endif /* _NETUTIL_H_ */ #endif /* _NETUTIL_H_ */

View File

@@ -108,10 +108,6 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if HAVE_UNISTD_H #if HAVE_UNISTD_H
/* #include <sys/unistd.h> */ /* #include <sys/unistd.h> */
#include <unistd.h> #include <unistd.h>
@@ -1665,45 +1661,6 @@ void max_rcvbuf(int sd) {
#endif /* WIN32 */ #endif /* WIN32 */
} }
/* Maximize the open file descriptor limit for this process go up to the
max allowed */
int max_sd() {
#ifndef WIN32
struct rlimit r;
static int maxfds = -1;
if (maxfds > 0)
return maxfds;
#if(defined(RLIMIT_NOFILE))
if (!getrlimit(RLIMIT_NOFILE, &r)) {
r.rlim_cur = r.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &r))
if (o.debugging)
perror("setrlimit RLIMIT_NOFILE failed");
if (!getrlimit(RLIMIT_NOFILE, &r)) {
maxfds = r.rlim_cur;
return maxfds;
} else
return 0;
}
#endif
#if(defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE))
if (!getrlimit(RLIMIT_OFILE, &r)) {
r.rlim_cur = r.rlim_max;
if (setrlimit(RLIMIT_OFILE, &r))
if (o.debugging)
perror("setrlimit RLIMIT_OFILE failed");
if (!getrlimit(RLIMIT_OFILE, &r)) {
maxfds = r.rlim_cur;
return maxfds;
} else
return 0;
}
#endif
#endif /* WIN32 */
return 0;
}
/* Give broadcast permission to a socket */ /* Give broadcast permission to a socket */
void broadcast_socket(int sd) { void broadcast_socket(int sd) {

View File

@@ -639,10 +639,6 @@ int gettcpopt_ts(struct tcp_hdr *tcp, u32 *timestamp, u32 *echots);
/* Maximize the receive buffer of a socket descriptor (up to 500K) */ /* Maximize the receive buffer of a socket descriptor (up to 500K) */
void max_rcvbuf(int sd); void max_rcvbuf(int sd);
/* Maximize the open file descriptor limit for this process go up to the
max allowed */
int max_sd();
/* Give broadcast permission to a socket */ /* Give broadcast permission to a socket */
void broadcast_socket(int sd); void broadcast_socket(int sd);