Skip to content

Commit

Permalink
Merged all use of 'sockaddr_str_port()'
Browse files Browse the repository at this point in the history
'sockaddr_str()' and 'sockaddr_str2()' into
a new 'ws_sockaddr_ntop()' function in 'in_addr.c'.
  • Loading branch information
gvanem committed May 20, 2022
1 parent d7414c6 commit 1c64926
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 134 deletions.
13 changes: 4 additions & 9 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "asn.h"
#include "iana.h"
#include "idna.h"
#include "in_addr.h"
#include "hosts.h"
#include "wsock_trace.h"
#include "inet_util.h"
Expand Down Expand Up @@ -1891,7 +1892,7 @@ void dump_wsamsg (const WSAMSG *msg, int rc)
if (rc != SOCKET_ERROR)
{
if (!IsBadReadPtr(msg->name, sizeof(msg->name)))
remote = sockaddr_str_port (msg->name);
remote = ws_sockaddr_ntop (msg->name);

if (!IsBadReadPtr(&msg->Control, sizeof(WSABUF)) && msg->Control.len)
have_control = TRUE;
Expand Down Expand Up @@ -2075,7 +2076,6 @@ void dump_addrinfo (const char *name, const struct addrinfo *ai)
{
for ( ; ai; ai = ai->ai_next)
{
const int *addr_len;
const char *comment;

C_indent (g_cfg.trace_indent+2);
Expand All @@ -2090,10 +2090,8 @@ void dump_addrinfo (const char *name, const struct addrinfo *ai)
else comment = "";

C_indent (g_cfg.trace_indent+2);
addr_len = (const int*)&ai->ai_addrlen;

C_printf ("ai_canonname: %s, ai_addr: %s%s\n",
ai->ai_canonname, sockaddr_str2(ai->ai_addr, addr_len), comment);
ai->ai_canonname, ws_sockaddr_ntop(ai->ai_addr), comment);
}
C_puts ("~0");
}
Expand All @@ -2102,7 +2100,6 @@ void dump_addrinfoW (const wchar_t *name, const struct addrinfoW *ai)
{
for ( ; ai; ai = ai->ai_next)
{
const int *addr_len;
const char *comment;

C_indent (g_cfg.trace_indent+2);
Expand All @@ -2117,10 +2114,8 @@ void dump_addrinfoW (const wchar_t *name, const struct addrinfoW *ai)
else comment = "";

C_indent (g_cfg.trace_indent+2);
addr_len = (const int*)&ai->ai_addrlen;

C_printf ("ai_canonname: %" WCHAR_FMT ", ai_addr: %s%s\n",
ai->ai_canonname, sockaddr_str2(ai->ai_addr, addr_len), comment);
ai->ai_canonname, ws_sockaddr_ntop(ai->ai_addr), comment);
}
C_puts ("~0");
}
Expand Down
74 changes: 74 additions & 0 deletions src/in_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,77 @@ static int _ws_inet_pton6 (const char *src, u_char *dst, int *err)
*err = WSAENAMETOOLONG;
return (0);
}

/**
* \struct fake_sockaddr_un
* This is in `<afunix.h>` on recent SDK's.
*/
struct fake_sockaddr_un {
short sun_family; /* AF_UNIX */
char sun_path [108]; /* pathname */
};
#define sockaddr_un fake_sockaddr_un

/**
* Instead of calling `WSAAddressToStringA()` for `AF_INET`, `AF_INET6`
* and `AF_UNIX` addresses, we do it ourself.
*
* This function returns the address *and* the port if the
* `sockaddr_in::sin_port` (or `sockaddr_in6::sin6_port`) is set.
* Like:
* \li `127.0.0.1:1234` or
* \li `[aa:bb::ff]:1234`
*
* \param[in] sa the `struct sockaddr *` to format a string from.
*/
char *ws_sockaddr_ntop (const struct sockaddr *sa)
{
const struct sockaddr_in *sa4 = (const struct sockaddr_in*) sa;
const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6*) sa;
const struct sockaddr_un *su = (const struct sockaddr_un*) sa;
static char buf [MAX_IP6_SZ+MAX_PORT_SZ+3];
char *end;

if (!sa4)
return ("<NULL>");

if (sa4->sin_family == AF_INET)
{
_ws_inet_ntop4 ((u_char*)&sa4->sin_addr, buf, sizeof(buf), NULL);
if (sa4->sin_port)
{
end = strchr (buf, '\0');
*end++ = ':';
_itoa (swap16(sa4->sin_port), end, 10);
}
return (buf);
}

if (sa4->sin_family == AF_INET6)
{
buf[0] = '[';
_ws_inet_ntop6 ((u_char*)&sa6->sin6_addr, buf+1, sizeof(buf)-1, NULL);
end = strchr (buf, '\0');
*end++ = ']';
if (sa6->sin6_port)
{
*end++ = ':';
_itoa (swap16(sa6->sin6_port), end, 10);
}
return (buf);
}

if (sa4->sin_family == AF_UNIX)
{
const wchar_t *path = (const wchar_t*) &su->sun_path;

if (!su->sun_path[0])
strcpy (buf, "abstract");
else if (su->sun_path[0] && su->sun_path[1])
_strlcpy (buf, su->sun_path, sizeof(buf));
else if (WideCharToMultiByte(CP_ACP, 0, path, (int)wcslen(path), buf, (int)sizeof(buf), NULL, NULL) == 0)
strcpy (buf, "??");
return (buf);
}
return ("??");
}
1 change: 1 addition & 0 deletions src/in_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ extern char *ws_inet_ntop (int family, const void *addr, char *result, size_t s
extern char *ws_inet_ntop2 (int family, const void *addr);
extern int ws_inet_pton (int family, const char *addr, void *result, int *err);
extern int ws_inet_pton2 (int family, const char *addr, void *result);
extern char *ws_sockaddr_ntop (const struct sockaddr *sa);

#endif

7 changes: 0 additions & 7 deletions src/ws_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,4 @@ const struct LoadTable *find_ws2_func_by_name (const char *func)
ARGSUSED (func);
return (NULL);
}

const char *sockaddr_str2 (const struct sockaddr *sa, const int *sa_len)
{
ARGSUSED (sa);
ARGSUSED (sa_len);
return (NULL);
}
#endif
127 changes: 13 additions & 114 deletions src/wsock_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,107 +690,6 @@ static const char *get_error (SOCK_RC_TYPE rc, int local_err)
return ("No error");
}

/**
* `WSAAddressToStringA()` returns the address *and* the port.<br>
* Like: `127.0.0.1:1234`
*
* \param[in] sa the `struct sockaddr *` to return the address from.
* \param[in] sa_len the length of the `struct sockaddr` structure.
*/
const char *sockaddr_str (const struct sockaddr *sa, const int *sa_len)
{
static char buf [MAX_IP6_SZ+MAX_PORT_SZ+1];
DWORD size = sizeof(buf);
DWORD len = sa_len ? *(DWORD*)sa_len : (DWORD)sizeof(*sa);

WSAERROR_PUSH();
if ((*p_WSAAddressToStringA)((SOCKADDR*)sa, len, NULL, buf, &size))
strcpy (buf, "??");
WSAERROR_POP();
return (buf);
}

/**
* Instead of calling `WSAAddressToStringA()` for AF_INET/AF_INET6 addresses,
* we do it ourself using `sockaddr_str_port()`.
*
* \param[in] sa the `struct sockaddr *` to format and return from.
* \param[in] sa_len the length of the `struct sockaddr` structure.
*/
const char *sockaddr_str2 (const struct sockaddr *sa, const int *sa_len)
{
const char *p = sockaddr_str_port (sa);

if (!p)
return sockaddr_str (sa, sa_len);
return (p);
}

/**
* \struct fake_sockaddr_un
* This is in `<afunix.h>` on recent SDK's.
*/
struct fake_sockaddr_un {
short sun_family; /* AF_UNIX */
char sun_path [108]; /* pathname */
};
#define sockaddr_un fake_sockaddr_un

/**
* This returns the address *and* the port in the `buf`. <br>
* Like:
* \li `"127.0.0.1:1234"` for an `AF_INET` sockaddr. And
* \li `"[0F::80::]:1234"` for an `AF_INET6` sockaddr.
*/
const char *sockaddr_str_port (const struct sockaddr *sa)
{
const struct sockaddr_in *sa4 = (const struct sockaddr_in*) sa;
const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6*) sa;
const struct sockaddr_un *su = (const struct sockaddr_un*) sa;
static char buf [MAX_IP6_SZ+MAX_PORT_SZ+3];
char *end;

if (!sa4)
return ("<NULL>");

if (sa4->sin_family == AF_INET)
{
snprintf (buf, sizeof(buf), "%u.%u.%u.%u:%d",
sa4->sin_addr.S_un.S_un_b.s_b1,
sa4->sin_addr.S_un.S_un_b.s_b2,
sa4->sin_addr.S_un.S_un_b.s_b3,
sa4->sin_addr.S_un.S_un_b.s_b4,
swap16(sa4->sin_port));
return (buf);
}

if (sa4->sin_family == AF_INET6)
{
buf[0] = '[';
ws_inet_ntop (AF_INET6, &sa6->sin6_addr, buf+1, sizeof(buf)-1, NULL);
end = strchr (buf, '\0');
*end++ = ']';
*end++ = ':';
_itoa (swap16(sa6->sin6_port), end, 10);
return (buf);
}

if (sa4->sin_family == AF_UNIX)
{
const wchar_t *path = (const wchar_t*) &su->sun_path;

if (!su->sun_path[0])
strcpy (buf, "abstract");
else if (su->sun_path[0] && su->sun_path[1])
_strlcpy (buf, su->sun_path, sizeof(buf));
else if (WideCharToMultiByte(CP_ACP, 0, path, (int)wcslen(path), buf, (int)sizeof(buf), NULL, NULL) == 0)
strcpy (buf, "??");
return (buf);
}

return (NULL);
}

static __inline const char *uint_ptr_hexval (UINT_PTR val, char *buf)
{
int i, j;
Expand Down Expand Up @@ -1157,7 +1056,7 @@ EXPORT int WINAPI WSAConnect (SOCKET s, const struct sockaddr *name, int namelen
ENTER_CRIT();

WSTRACE ("WSAConnect (%s, %s, 0x%p, 0x%p, ...) --> %s",
socket_number(s), sockaddr_str2(name, &namelen),
socket_number(s), ws_sockaddr_ntop(name),
caller_data, callee_data, socket_or_error(rc));

#if 0
Expand Down Expand Up @@ -1434,7 +1333,7 @@ EXPORT SOCKET WINAPI WSAAccept (SOCKET s, struct sockaddr *addr, int *addr_len,
ENTER_CRIT();

WSTRACE ("WSAAccept (%s, %s, 0x%p, 0x%p) --> %s",
socket_number(s), sockaddr_str2(addr, addr_len),
socket_number(s), ws_sockaddr_ntop(addr),
condition, (const void*)callback_data, socket_or_error(rc));

if (!exclude_this)
Expand Down Expand Up @@ -1503,7 +1402,7 @@ EXPORT SOCKET WINAPI accept (SOCKET s, struct sockaddr *addr, int *addr_len)
/*
* If `rc == INVALID_SOCKET`, the `addr` is not filled. Hence simply print it's pointer address.
*/
rc == INVALID_SOCKET ? ptr_or_error(addr) : sockaddr_str2(addr, addr_len),
rc == INVALID_SOCKET ? ptr_or_error(addr) : ws_sockaddr_ntop(addr),
socket_or_error(rc));

if (!exclude_this)
Expand Down Expand Up @@ -1537,12 +1436,12 @@ EXPORT int WINAPI bind (SOCKET s, const struct sockaddr *addr, int addr_len)
if (addr->sa_family == AF_UNIX)
{
WSTRACE ("bind (%s, \"%s\") --> %s",
socket_number(s), sockaddr_str_port(addr), get_error(rc, 0));
socket_number(s), ws_sockaddr_ntop(addr), get_error(rc, 0));
}
else
{
WSTRACE ("bind (%s, %s) --> %s",
socket_number(s), sockaddr_str2 (addr, &addr_len), get_error(rc, 0));
socket_number(s), ws_sockaddr_ntop(addr), get_error(rc, 0));
}

if (!exclude_this)
Expand Down Expand Up @@ -1618,7 +1517,7 @@ EXPORT int WINAPI connect (SOCKET s, const struct sockaddr *addr, int addr_len)
rc = (*p_connect) (s, addr, addr_len);

WSTRACE ("connect (%s, %s, fam %s) --> %s",
socket_number(s), sockaddr_str2(addr, &addr_len),
socket_number(s), ws_sockaddr_ntop(addr),
socket_family(sa->sin_family), get_error(rc, 0));

if (!exclude_this)
Expand Down Expand Up @@ -1899,7 +1798,7 @@ EXPORT int WINAPI recvfrom (SOCKET s, char *buf, int buf_len, int flags, struct

WSTRACE ("recvfrom (%s, 0x%p, %d, %s, %s) --> %s",
socket_number(s), buf, buf_len, socket_flags(flags),
sockaddr_str2(from, from_len), res);
ws_sockaddr_ntop(from), res);

if (rc > 0 && g_cfg.dump_data)
dump_data (buf, rc);
Expand Down Expand Up @@ -1994,7 +1893,7 @@ EXPORT int WINAPI sendto (SOCKET s, const char *buf, int buf_len, int flags, con

WSTRACE ("sendto (%s, 0x%p, %d, %s, %s) --> %s",
socket_number(s), buf, buf_len, socket_flags(flags),
sockaddr_str2(to, &to_len), res);
ws_sockaddr_ntop(to), res);

if (g_cfg.dump_data)
dump_data (buf, buf_len);
Expand Down Expand Up @@ -2133,7 +2032,7 @@ EXPORT int WINAPI WSARecvFrom (SOCKET s, WSABUF *bufs, DWORD num_bufs, DWORD *nu

WSTRACE ("WSARecvFrom (%s, 0x%p, %lu, %s, <%s>, %s, 0x%p, 0x%p) --> %s",
socket_number(s), bufs, DWORD_CAST(num_bufs), nbytes, flg,
sockaddr_str2(from, from_len), ov, func, res);
ws_sockaddr_ntop(from), ov, func, res);

if (rc == NO_ERROR && g_cfg.dump_data)
{
Expand Down Expand Up @@ -2320,7 +2219,7 @@ EXPORT int WINAPI WSASendTo (SOCKET s, WSABUF *bufs, DWORD num_bufs, DWORD *num_

WSTRACE ("WSASendTo (%s, 0x%p, %lu, %s, <%s>, %s, 0x%p, 0x%p) --> %s",
socket_number(s), bufs, DWORD_CAST(num_bufs), nbytes, socket_flags(flags),
sockaddr_str2(to, &to_len), ov, func, res);
ws_sockaddr_ntop(to), ov, func, res);

if (g_cfg.dump_data)
dump_wsabuf (bufs, num_bufs);
Expand Down Expand Up @@ -3159,7 +3058,7 @@ EXPORT int WINAPI getpeername (SOCKET s, struct sockaddr *name, int *name_len)
ENTER_CRIT();

WSTRACE ("getpeername (%s, %s) --> %s",
socket_number(s), sockaddr_str2(name, name_len), get_error(rc, 0));
socket_number(s), ws_sockaddr_ntop(name), get_error(rc, 0));

if (!exclude_this)
{
Expand Down Expand Up @@ -3190,7 +3089,7 @@ EXPORT int WINAPI getsockname (SOCKET s, struct sockaddr *name, int *name_len)
ENTER_CRIT();

WSTRACE ("getsockname (%s, %s) --> %s",
socket_number(s), sockaddr_str2(name, name_len), get_error(rc, 0));
socket_number(s), ws_sockaddr_ntop(name), get_error(rc, 0));

if (!exclude_this)
{
Expand Down Expand Up @@ -3263,7 +3162,7 @@ EXPORT int WINAPI getnameinfo (const struct sockaddr *sa, socklen_t sa_len,
ENTER_CRIT();

WSTRACE ("getnameinfo (%s, ..., %s) --> %s",
sockaddr_str2(sa, &sa_len), getnameinfo_flags_decode(flags), get_error(rc, 0));
ws_sockaddr_ntop(sa), getnameinfo_flags_decode(flags), get_error(rc, 0));

if (!exclude_this)
{
Expand Down
4 changes: 0 additions & 4 deletions src/wsock_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ extern const struct LoadTable *find_ws2_func_by_name (const char *func);

extern int WSAError_save_restore (int pop);

extern const char *sockaddr_str (const struct sockaddr *sa, const int *sa_len);
extern const char *sockaddr_str2 (const struct sockaddr *sa, const int *sa_len);
extern const char *sockaddr_str_port (const struct sockaddr *sa);

#endif

0 comments on commit 1c64926

Please sign in to comment.