mirror of
https://github.com/nmap/nmap.git
synced 2026-06-29 04:33:23 +00:00
Consolidate inet_ntop_ez-like code from throughout
Some checks failed
nmap multiplatform autobuilds / build (arm64, gcc, ubuntu-latest-gcc-arm64, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, freebsd-15-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-15-clang, macos-15) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-26-clang, macos-26) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, netbsd-10-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, openbsd-7-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, solaris-11-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, ubuntu-latest-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (egcc, openbsd-7-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, freebsd-15-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, netbsd-10-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, solaris-11-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, ubuntu-latest-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (msvc, windows-latest-msvc, windows-latest) (push) Has been cancelled
Some checks failed
nmap multiplatform autobuilds / build (arm64, gcc, ubuntu-latest-gcc-arm64, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, freebsd-15-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-15-clang, macos-15) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-26-clang, macos-26) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, netbsd-10-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, openbsd-7-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, solaris-11-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, ubuntu-latest-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (egcc, openbsd-7-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, freebsd-15-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, netbsd-10-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, solaris-11-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, ubuntu-latest-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (msvc, windows-latest-msvc, windows-latest) (push) Has been cancelled
This commit is contained in:
parent
a8283c0b36
commit
d8c9880cd5
12 changed files with 29 additions and 45 deletions
|
|
@ -139,6 +139,8 @@ int sockaddr_storage_equal(const struct sockaddr_storage *a,
|
|||
thread in the process). If there is a weird error (like sslen being
|
||||
too short) then NULL will be returned. */
|
||||
const char *inet_ntop_ez(const struct sockaddr_storage *ss, size_t sslen);
|
||||
/* Same, but no extra length checks */
|
||||
const char *inet_socktop(const struct sockaddr_storage *ss);
|
||||
|
||||
|
||||
#if !HAVE_GETNAMEINFO || !HAVE_GETADDRINFO
|
||||
|
|
|
|||
|
|
@ -191,6 +191,11 @@ const char *inet_ntop_ez(const struct sockaddr_storage *ss, size_t sslen) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Same as inet_ntop_ez, but assumes sslen==sizeof(sockaddr_storage) */
|
||||
const char *inet_socktop(const struct sockaddr_storage *ss) {
|
||||
return inet_ntop_ez(ss, sizeof(*ss));
|
||||
}
|
||||
|
||||
/* Create a new socket inheritable by subprocesses. On non-Windows systems it's
|
||||
just a normal socket. */
|
||||
int inheritable_socket(int af, int style, int protocol) {
|
||||
|
|
|
|||
|
|
@ -269,7 +269,7 @@ static void connect_report(nsock_iod nsi)
|
|||
}
|
||||
}
|
||||
|
||||
/* Just like inet_socktop, but it puts IPv6 addresses in square brackets. */
|
||||
/* Just like socktop, but it puts IPv6 addresses in square brackets. */
|
||||
static const char *sock_to_url(char *host_str, unsigned short port)
|
||||
{
|
||||
static char buf[512];
|
||||
|
|
@ -391,7 +391,7 @@ static int do_proxy_http(void)
|
|||
target = o.target;
|
||||
} else {
|
||||
/* addr is now populated with either sockaddr_in or sockaddr_in6 */
|
||||
Strncpy(addrstr, inet_socktop(&addr), sizeof(addrstr));
|
||||
Strncpy(addrstr, inet_socktop_safe(&addr), sizeof(addrstr));
|
||||
target = addrstr;
|
||||
if (o.verbose && getaddrfamily(o.target) == -1)
|
||||
loguser("Host %s locally resolved to %s.\n", o.target, target);
|
||||
|
|
@ -548,7 +548,7 @@ static int do_proxy_socks4(void)
|
|||
}
|
||||
|
||||
if (o.verbose) {
|
||||
loguser("Connected to proxy %s:%hu\n", inet_socktop(&targetaddrs->addr),
|
||||
loguser("Connected to proxy %s:%hu\n", inet_socktop_safe(&targetaddrs->addr),
|
||||
inet_port(&targetaddrs->addr));
|
||||
}
|
||||
|
||||
|
|
@ -588,7 +588,7 @@ static int do_proxy_socks4(void)
|
|||
socks4msg.address = addr.in.sin_addr.s_addr;
|
||||
if (o.verbose && getaddrfamily(o.target) == -1)
|
||||
loguser("Host %s locally resolved to %s.\n", o.target,
|
||||
inet_socktop(&addr));
|
||||
inet_socktop_safe(&addr));
|
||||
}
|
||||
|
||||
if (send(sd, (char *)&socks4msg, offsetof(struct socks4_data, data) + datalen, 0) < 0) {
|
||||
|
|
@ -643,7 +643,7 @@ static int do_proxy_socks5(void)
|
|||
}
|
||||
|
||||
if (o.verbose) {
|
||||
loguser("Connected to proxy %s:%hu\n", inet_socktop(&targetaddrs->addr),
|
||||
loguser("Connected to proxy %s:%hu\n", inet_socktop_safe(&targetaddrs->addr),
|
||||
inet_port(&targetaddrs->addr));
|
||||
}
|
||||
|
||||
|
|
@ -815,7 +815,7 @@ static int do_proxy_socks5(void)
|
|||
dstlen = addrlen;
|
||||
if (o.verbose && getaddrfamily(o.target) == -1)
|
||||
loguser("Host %s locally resolved to %s.\n", o.target,
|
||||
inet_socktop(&addr));
|
||||
inet_socktop_safe(&addr));
|
||||
}
|
||||
|
||||
memcpy(socks5msg2.dst + dstlen, &proxyport, 2);
|
||||
|
|
@ -1190,7 +1190,7 @@ static void connect_handler(nsock_pool nsp, nsock_event evt, void *data)
|
|||
zmem(&peer, sizeof(peer.storage));
|
||||
nsock_iod_get_communication_info(cs.sock_nsi, NULL, NULL, NULL,
|
||||
&peer.sockaddr, sizeof(peer.storage));
|
||||
loguser("Connection to %s failed: %s.\n", inet_socktop(&peer),
|
||||
loguser("Connection to %s failed: %s.\n", inet_socktop_safe(&peer),
|
||||
(status == NSE_STATUS_TIMEOUT)
|
||||
? nse_status2str(status)
|
||||
: socket_strerror(nse_errorcode(evt)));
|
||||
|
|
|
|||
|
|
@ -668,7 +668,7 @@ int ssl_handshake(struct fdinfo *sinfo)
|
|||
|
||||
if (o.verbose) {
|
||||
loguser("Failed SSL connection from %s: %s\n",
|
||||
inet_socktop(&sinfo->remoteaddr),
|
||||
inet_socktop_safe(&sinfo->remoteaddr),
|
||||
ERR_error_string(ERR_get_error(), NULL));
|
||||
}
|
||||
return NCAT_SSL_HANDSHAKE_FAILED;
|
||||
|
|
|
|||
21
ncat/util.c
21
ncat/util.c
|
|
@ -363,10 +363,10 @@ const char *socktop(const union sockaddr_u *su, socklen_t ss_len)
|
|||
break;
|
||||
#endif
|
||||
case AF_INET:
|
||||
Snprintf(buf, sizeof(buf), "%s:%hu", inet_socktop(su), inet_port(su));
|
||||
Snprintf(buf, sizeof(buf), "%s:%hu", inet_socktop_safe(su), inet_port(su));
|
||||
break;
|
||||
case AF_INET6:
|
||||
Snprintf(buf, sizeof(buf), "[%s]:%hu", inet_socktop(su), inet_port(su));
|
||||
Snprintf(buf, sizeof(buf), "[%s]:%hu", inet_socktop_safe(su), inet_port(su));
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
|
|
@ -379,21 +379,10 @@ const char *socktop(const union sockaddr_u *su, socklen_t ss_len)
|
|||
IPv6 IP address string. Since a static buffer is returned, this is
|
||||
not thread-safe and can only be used once in calls like printf()
|
||||
*/
|
||||
const char *inet_socktop(const union sockaddr_u *su)
|
||||
const char *inet_socktop_safe(const union sockaddr_u *su)
|
||||
{
|
||||
static char buf[INET6_ADDRSTRLEN + 1];
|
||||
void *addr;
|
||||
|
||||
if (su->storage.ss_family == AF_INET)
|
||||
addr = (void *) &su->in.sin_addr;
|
||||
#if HAVE_IPV6
|
||||
else if (su->storage.ss_family == AF_INET6)
|
||||
addr = (void *) &su->in6.sin6_addr;
|
||||
#endif
|
||||
else
|
||||
bye("Invalid address family passed to inet_socktop().");
|
||||
|
||||
if (inet_ntop(su->storage.ss_family, addr, buf, sizeof(buf)) == NULL) {
|
||||
const char *buf = inet_socktop(&su->storage);
|
||||
if (buf == NULL) {
|
||||
bye("Failed to convert address to presentation format! Error: %s.",
|
||||
strerror(socket_errno()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ int strbuf_sprintf(char **buf, size_t *size, size_t *offset, const char *fmt, ..
|
|||
int addr_is_local(const union sockaddr_u *su);
|
||||
|
||||
const char *socktop(const union sockaddr_u *su, socklen_t ss_len);
|
||||
const char *inet_socktop(const union sockaddr_u *su);
|
||||
const char *inet_socktop_safe(const union sockaddr_u *su);
|
||||
|
||||
unsigned short inet_port(const union sockaddr_u *su);
|
||||
|
||||
|
|
|
|||
2
nmap.cc
2
nmap.cc
|
|
@ -2178,7 +2178,7 @@ int nmap_main(int argc, char *argv[]) {
|
|||
currenths->setSourceSockAddr(&ss, sslen);
|
||||
if (! sourceaddrwarning) {
|
||||
error("WARNING: We could not determine for sure which interface to use, so we are guessing %s . If this is wrong, use -S <my_IP_address>.",
|
||||
inet_socktop(&ss));
|
||||
inet_socktop_safe(&ss));
|
||||
sourceaddrwarning = 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -843,7 +843,7 @@ static int l_resolve(lua_State *L)
|
|||
if (addr->ai_addrlen > sizeof(ss))
|
||||
continue;
|
||||
memcpy(&ss, addr->ai_addr, addr->ai_addrlen);
|
||||
nseU_appendfstr(L, -1, "%s", inet_socktop(&ss));
|
||||
nseU_appendfstr(L, -1, "%s", inet_socktop_safe(&ss));
|
||||
}
|
||||
|
||||
if (addrs != NULL)
|
||||
|
|
|
|||
|
|
@ -436,7 +436,7 @@ static void init_socket(int sd, const HostScanStats *hss, UltraScanInfo *USI) {
|
|||
if (o.spoofsource && !bind_failed) {
|
||||
o.SourceSockAddr(&ss, &sslen);
|
||||
if (::bind(sd, (struct sockaddr*)&ss, sslen) != 0) {
|
||||
error("%s: Problem binding source address (%s), errno: %d", __func__, inet_socktop(&ss), socket_errno());
|
||||
error("%s: Problem binding source address (%s), errno: %d", __func__, inet_socktop_safe(&ss), socket_errno());
|
||||
perror("bind");
|
||||
bind_failed = 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ int load_exclude_file(struct addrset *excludelist, FILE *fp) {
|
|||
for (size_t i = 0; i < req.ssv.size(); i++) {
|
||||
const struct sockaddr_storage &ss = req.ssv[i];
|
||||
assert(ss.ss_family == o.af());
|
||||
Snprintf(host_spec, sizeof(host_spec), "%s%s", inet_socktop(&ss),
|
||||
Snprintf(host_spec, sizeof(host_spec), "%s%s", inet_socktop_safe(&ss),
|
||||
slash);
|
||||
if(!addrset_add_spec(excludelist, host_spec, o.af(), 0)){
|
||||
error("Invalid address specification: %s", host_spec);
|
||||
|
|
|
|||
18
tcpip.cc
18
tcpip.cc
|
|
@ -361,21 +361,9 @@ void PacketTrace::traceConnect(u8 proto, const struct sockaddr *sock,
|
|||
/* Converts an IP address given in a sockaddr_storage to an IPv4 or
|
||||
IPv6 IP address string. Since a static buffer is returned, this is
|
||||
not thread-safe and can only be used once in calls like printf() */
|
||||
const char *inet_socktop(const struct sockaddr_storage *ss) {
|
||||
static char buf[INET6_ADDRSTRLEN];
|
||||
const struct sockaddr_in *sin = (struct sockaddr_in *) ss;
|
||||
#if HAVE_IPV6
|
||||
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss;
|
||||
#endif
|
||||
|
||||
if (inet_ntop(sin->sin_family, (sin->sin_family == AF_INET) ?
|
||||
(char *) &sin->sin_addr :
|
||||
#if HAVE_IPV6
|
||||
(char *) &sin6->sin6_addr,
|
||||
#else
|
||||
(char *) NULL,
|
||||
#endif /* HAVE_IPV6 */
|
||||
buf, sizeof(buf)) == NULL) {
|
||||
const char *inet_socktop_safe(const struct sockaddr_storage *ss) {
|
||||
const char *buf = inet_socktop(ss);
|
||||
if (buf == NULL) {
|
||||
fatal("Failed to convert target address to presentation format in %s!?! Error: %s", __func__, strerror(socket_errno()));
|
||||
}
|
||||
return buf;
|
||||
|
|
|
|||
2
tcpip.h
2
tcpip.h
|
|
@ -135,7 +135,7 @@ class PacketCounter {
|
|||
IPv6 IP address string. Since a static buffer is returned, this is
|
||||
not thread-safe and can only be used once in calls like printf()
|
||||
*/
|
||||
const char *inet_socktop(const struct sockaddr_storage *ss);
|
||||
const char *inet_socktop_safe(const struct sockaddr_storage *ss);
|
||||
|
||||
/* Takes a destination address (dst) and tries to determine the
|
||||
source address and interface necessary to route to this address.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue