OpenVPN
compat-inet_pton.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_PTON
31 
32 #include "compat.h"
33 
34 #ifdef _WIN32
35 
36 #include <windows.h>
37 #include <string.h>
38 
39 /*
40  * inet_ntop() and inet_pton() wrap-implementations using
41  * WSAAddressToString() and WSAStringToAddress() functions
42  *
43  * this is needed as long as we support running OpenVPN on WinXP
44  */
45 
46 
47 int
48 inet_pton(int af, const char *src, void *dst)
49 {
50  struct sockaddr_storage ss;
51  int size = sizeof(ss);
52  char src_copy[INET6_ADDRSTRLEN+1];
53 
54  ZeroMemory(&ss, sizeof(ss));
55  /* stupid non-const API */
56  strncpy(src_copy, src, INET6_ADDRSTRLEN+1);
57  src_copy[INET6_ADDRSTRLEN] = 0;
58 
59  if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
60  {
61  switch (af)
62  {
63  case AF_INET:
64  *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
65  return 1;
66 
67  case AF_INET6:
68  *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
69  return 1;
70  }
71  }
72  return 0;
73 }
74 
75 #else /* ifdef _WIN32 */
76 
77 #error no emulation for inet_ntop
78 
79 #endif /* ifdef _WIN32 */
80 
81 #endif /* ifndef HAVE_INET_PTON */
int inet_pton(int af, const char *src, void *dst)
char * dst
Definition: compat-lz4.h:455