Debian Codegraph @

src/oscompat.c

raw
1 #include "pycurl.h"
2
3 #if defined(WIN32)
4 PYCURL_INTERNAL curl_socket_t
5 dup_winsock(curl_socket_t sock, const struct curl_sockaddr *address)
6 {
7 int rv;
8 WSAPROTOCOL_INFO pi;
9
10 rv = WSADuplicateSocket(sock, GetCurrentProcessId(), &pi);
11 if (rv) {
12 return CURL_SOCKET_BAD;
13 }
14
15 /* not sure if WSA_FLAG_OVERLAPPED is needed, but it does not seem to hurt */
16 return WSASocket(address->family, address->socktype, address->protocol, &pi, 0, WSA_FLAG_OVERLAPPED);
17 }
18 #endif
19
20 #if defined(WIN32) && ((_WIN32_WINNT < 0x0600) || (NTDDI_VERSION < NTDDI_VISTA))
21 /*
22 * Only Winsock on Vista+ has inet_ntop().
23 */
24 PYCURL_INTERNAL const char *
25 pycurl_inet_ntop (int family, void *addr, char *string, size_t string_size)
26 {
27 SOCKADDR *sa;
28 int sa_len;
29 /* both size_t and DWORD should be unsigned ints */
30 DWORD string_size_dword = (DWORD) string_size;
31
32 if (family == AF_INET6) {
33 struct sockaddr_in6 sa6;
34 memset(&sa6, 0, sizeof(sa6));
35 sa6.sin6_family = AF_INET6;
36 memcpy(&sa6.sin6_addr, addr, sizeof(sa6.sin6_addr));
37 sa = (SOCKADDR*) &sa6;
38 sa_len = sizeof(sa6);
39 } else if (family == AF_INET) {
40 struct sockaddr_in sa4;
41 memset(&sa4, 0, sizeof(sa4));
42 sa4.sin_family = AF_INET;
43 memcpy(&sa4.sin_addr, addr, sizeof(sa4.sin_addr));
44 sa = (SOCKADDR*) &sa4;
45 sa_len = sizeof(sa4);
46 } else {
47 errno = EAFNOSUPPORT;
48 return NULL;
49 }
50 if (WSAAddressToString(sa, sa_len, NULL, string, &string_size_dword))
51 return NULL;
52 return string;
53 }
54 #endif
55
56 /* vi:ts=4:et:nowrap
57 */