OpenVPN
compat-inet_ntop.c
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2011 - David Sommerseth <davids@redhat.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #ifndef HAVE_INET_NTOP
31 
32 #include "compat.h"
33 
34 #ifdef _WIN32
35 
36 #include <windows.h>
37 
38 /*
39  * inet_ntop() and inet_pton() wrap-implementations using
40  * WSAAddressToString() and WSAStringToAddress() functions
41  *
42  * this is needed as long as we support running OpenVPN on WinXP
43  */
44 
45 const char *
46 inet_ntop(int af, const void *src, char *dst, socklen_t size)
47 {
48  struct sockaddr_storage ss;
49  unsigned long s = size;
50 
51  ZeroMemory(&ss, sizeof(ss));
52  ss.ss_family = af;
53 
54  switch (af)
55  {
56  case AF_INET:
57  ((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
58  break;
59 
60  case AF_INET6:
61  ((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
62  break;
63 
64  default:
65  return NULL;
66  }
67  /* cannot direclty use &size because of strict aliasing rules */
68  return (WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0) ?
69  dst : NULL;
70 }
71 
72 #else /* ifdef _WIN32 */
73 
74 #error no emulation for inet_ntop
75 
76 #endif /* ifdef _WIN32 */
77 
78 #endif /* ifndef HAVE_INET_NTOP */
const char * inet_ntop(int af, const void *src, char *dst, socklen_t size)