suricata
win32-misc.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Jan Jezek <jjezek@kerio.com>
22  *
23  * Misc Windows utility functions
24  */
25 
26 #ifdef OS_WIN32
27 
28 #include "suricata-common.h"
29 #include "win32-misc.h"
30 #include "direct.h"
31 #include "util-ip.h"
32 
33 void setenv(const char *name, const char *value, int overwrite)
34 {
35  if (overwrite || NULL == getenv(name)) {
36  char *str = SCMalloc(strlen(name) + strlen(value) + 2);
37  if (unlikely(str == NULL))
38  return;
39  snprintf(str, strlen(name) + strlen(value) + 1, "%s=%s", name, value);
40  putenv(str);
41  SCFree(str);
42  }
43 }
44 
45 void unsetenv(const char *name)
46 {
47  char *str = SCMalloc(strlen(name) + 2);
48  if (unlikely(str == NULL))
49  return;
50  snprintf(str, strlen(name) + 1, "%s=", name);
51  putenv(str);
52  SCFree(str);
53 }
54 
55 /* these functions have been defined on Vista and later */
56 #if NTDDI_VERSION < NTDDI_VISTA
57 const char* inet_ntop(int af, const void *src, char *dst, uint32_t cnt)
58 {
59  if (af == AF_INET)
60  {
61  struct sockaddr_in in;
62  memset(&in, 0, sizeof(in));
63  in.sin_family = AF_INET;
64  memcpy(&in.sin_addr, src, sizeof(struct in_addr));
65  if (0 == getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST))
66  return dst;
67  }
68  else if (af == AF_INET6)
69  {
70  struct sockaddr_in6 in6;
71  memset(&in6, 0, sizeof(in6));
72  in6.sin6_family = AF_INET6;
73  memcpy(&in6.sin6_addr, src, sizeof(struct in_addr6));
74  if (0 == getnameinfo((struct sockaddr *)&in6, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST))
75  return dst;
76  }
77  return NULL;
78 }
79 
80 int inet_pton(int af, const char *src, void *dst)
81 {
82  struct addrinfo hints;
83  memset(&hints, 0, sizeof(struct addrinfo));
84  hints.ai_family = af;
85 
86  /* as getaddrinfo below seems more liberal that inet_pton on Linux,
87  * add this check here that does a guess at the validity of the
88  * input address. */
89  if (af == AF_INET) {
90  if (!IPv4AddressStringIsValid(src))
91  return -1;
92  } else if (af == AF_INET6) {
93  if (!IPv6AddressStringIsValid(src))
94  return -1;
95  }
96 
97  struct addrinfo* result = NULL;
98  if (0 != getaddrinfo(src, NULL, &hints, &result))
99  return -1;
100 
101  if (result) {
102  if (result->ai_family == AF_INET) {
103  struct sockaddr_in* in = (struct sockaddr_in*)result->ai_addr;
104  memcpy(dst, &in->sin_addr, 4);
105  }
106  else if (result->ai_family == AF_INET6) {
107  struct sockaddr_in6* in6 = (struct sockaddr_in6*)result->ai_addr;
108  memcpy(dst, &in6->sin6_addr, 16);
109  }
110  else {
111  freeaddrinfo(result);
112  return -1;
113  }
114 
115  freeaddrinfo(result);
116  return 1;
117  }
118 
119  return -1;
120 }
121 #endif
122 
123 #endif /* OS_WIN32 */
#define unlikely(expr)
Definition: util-optimize.h:35
bool IPv4AddressStringIsValid(const char *str)
determine if a string is a valid ipv4 address
Definition: util-ip.c:33
uint16_t src
void unsetenv(const char *name)
#define str(s)
uint16_t dst
void setenv(const char *name, const char *value, int overwrite)
bool IPv6AddressStringIsValid(const char *str)
determine if a string is a valid ipv6 address
Definition: util-ip.c:80
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
uint16_t af
Definition: decode-gre.h:404