4 * Copyright 2019, Gerald Combs
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
19 #include <wsutil/inet_addr.h>
22 #include <wsutil/win32-utils.h>
23 #define in_port_t uint16_t
32 WORD wVersionRequested
;
35 wVersionRequested
= MAKEWORD(2, 2);
36 err
= WSAStartup(wVersionRequested
, &wsaData
);
38 errmsg
= ws_strdup_printf("Couldn't initialize Windows Sockets: %s",
46 ws_cleanup_sockets(void)
49 /* XXX - any reason to check the error return? */
55 ws_socket_ptoa(struct sockaddr_storage
*dst
, const char *src
,
58 int ret
= -1, af
= -1;
60 char *addr_str
= NULL
, *port_str
= NULL
;
69 addr_src
= g_strdup(src
);
71 /* Is it an IPv6/IPv4 literal address enclosed in braces? */
72 if (*addr_src
== '[') {
73 addr_str
= addr_src
+ 1;
74 if ((p
= strchr(addr_str
, ']')) == NULL
) {
82 else if (*p
!= '\0') {
86 if (ws_inet_pton6(addr_str
, &addr
.ip6
)) {
89 else if (ws_inet_pton4(addr_str
, &addr
.ip4
)) {
98 /* It is an IPv4 dotted decimal. */
100 if ((p
= strchr(addr_str
, ':')) != NULL
) {
104 if (ws_inet_pton4(addr_str
, &addr
.ip4
)) {
113 if (port_str
!= NULL
&& *port_str
!= '\0') {
114 num
= strtol(port_str
, &endptr
, 10);
115 /* We want the entire string to be a valid decimal representation. */
116 if (endptr
== port_str
|| *endptr
!= '\0' || num
< 0 || num
> UINT16_MAX
) {
123 port
= g_htons(def_port
);
126 /* sockaddr_storage is guaranteed to fit any sockaddr type. */
127 if (af
== AF_INET6
) {
128 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)dst
;
129 memset(sa
, 0, sizeof(struct sockaddr_in6
));
130 sa
->sin6_family
= AF_INET6
;
131 sa
->sin6_port
= port
;
132 memcpy(&sa
->sin6_addr
, &addr
.ip6
, sizeof(struct in6_addr
));
135 else if (af
== AF_INET
) {
136 struct sockaddr_in
*sa
= (struct sockaddr_in
*)dst
;
137 memset(sa
, 0, sizeof(struct sockaddr_in
));
138 sa
->sin_family
= AF_INET
;
140 memcpy(&sa
->sin_addr
, &addr
.ip4
, sizeof(struct in_addr
));
144 ws_assert_not_reached();