/
compat.c
281 lines (244 loc) · 5.98 KB
/
compat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2012 Intel Corporation.
*
* Authors: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "openconnect-internal.h"
#ifdef HAVE_SUNOS_BROKEN_TIME
/*
* On SunOS, time() goes backwards. Thankfully, gethrtime() doesn't.
* https://www.illumos.org/issues/1871 and, for Solaris 11, CR7121035.
*/
#include <sys/time.h>
time_t openconnect__time(time_t *t)
{
time_t s = gethrtime() / 1000000000LL;
if (t)
*t = s;
return s;
}
#endif
#ifndef HAVE_ASPRINTF
static int oc_vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap2;
char *res = NULL;
int len = 160, len2;
int ret = 0;
int errno_save = -ENOMEM;
res = malloc(160);
if (!res)
goto err;
/* Use a copy of 'ap', preserving it in case we need to retry into
a larger buffer. 160 characters should be sufficient for most
strings in openconnect. */
#ifdef HAVE_VA_COPY
va_copy(ap2, ap);
#elif defined(HAVE___VA_COPY)
__va_copy(ap2, ap);
#else
#error No va_copy()!
/* You could try this. */
ap2 = ap;
/* Or this */
*ap2 = *ap;
#endif
len = vsnprintf(res, 160, fmt, ap2);
va_end(ap2);
if (len < 0) {
printf_err:
errno_save = errno;
free(res);
res = NULL;
goto err;
}
if (len >= 0 && len < 160)
goto out;
free(res);
res = malloc(len+1);
if (!res)
goto err;
len2 = vsnprintf(res, len+1, fmt, ap);
if (len2 < 0 || len2 > len)
goto printf_err;
ret = 0;
goto out;
err:
errno = errno_save;
ret = -1;
out:
*strp = res;
return ret;
}
int openconnect__asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = oc_vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
#endif
#ifndef HAVE_GETLINE
ssize_t openconnect__getline(char **lineptr, size_t *n, FILE *stream)
{
int len = 0;
if (!*lineptr) {
*n = 2;
*lineptr = malloc(*n);
if (!*lineptr)
return -1;
}
while (fgets((*lineptr) + len, (*n) - len, stream)) {
len += strlen((*lineptr) + len);
if ((*lineptr)[len-1] == '\n')
break;
*n *= 2;
realloc_inplace(*lineptr, *n);
if (!*lineptr)
return -1;
}
if (len)
return len;
return -1;
}
#endif
#ifndef HAVE_STRCASESTR
char *openconnect__strcasestr(const char *haystack, const char *needle)
{
int hlen = strlen(haystack);
int nlen = strlen(needle);
int i, j;
for (i = 0; i < hlen - nlen + 1; i++) {
for (j = 0; j < nlen; j++) {
if (tolower(haystack[i + j]) !=
tolower(needle[j]))
break;
}
if (j == nlen)
return (char *)haystack + i;
}
return NULL;
}
#endif
#ifndef HAVE_SETENV
int openconnect__setenv(const char *name, const char *value, int overwrite)
{
char *buf = alloca(strlen(name) + strlen(value) + 2);
sprintf(buf, "%s=%s", name, value);
putenv(buf);
return 0;
}
#endif
#ifndef HAVE_UNSETENV
void openconnect__unsetenv(const char *name)
{
char *buf = alloca(strlen(name) + 2);
sprintf(buf, "%s=", name);
putenv(buf);
}
#endif
#ifndef HAVE_INET_ATON
int openconnect__inet_aton(const char *cp, struct in_addr *addr)
{
addr->s_addr = inet_addr(cp);
return (addr->s_addr == 0xffffffff) ? 0 : 1;
}
#endif
#ifdef _WIN32
int openconnect__win32_neterrno()
{
switch (WSAGetLastError()) {
case WSAEINTR: return EINTR;
case WSAEWOULDBLOCK: return EWOULDBLOCK;
case WSAEINPROGRESS: return EINPROGRESS;
case WSAEALREADY: return EALREADY;
case WSAENOTSOCK: return ENOTSOCK;
case WSAEDESTADDRREQ: return EDESTADDRREQ;
case WSAEMSGSIZE: return EMSGSIZE;
case WSAEPROTOTYPE: return EPROTOTYPE;
case WSAENOPROTOOPT: return ENOPROTOOPT;
case WSAEPROTONOSUPPORT:return EPROTONOSUPPORT;
case WSAEOPNOTSUPP: return EOPNOTSUPP;
case WSAEPFNOSUPPORT: return EAFNOSUPPORT;
case WSAEAFNOSUPPORT: return EAFNOSUPPORT;
case WSAEADDRINUSE: return EADDRINUSE;
case WSAEADDRNOTAVAIL: return EADDRNOTAVAIL;
case WSAENETDOWN: return ENETDOWN;
case WSAENETUNREACH: return ENETUNREACH;
case WSAENETRESET: return ENETRESET;
case WSAECONNABORTED: return ECONNABORTED;
case WSAECONNRESET: return ECONNRESET;
case WSAENOBUFS: return ENOBUFS;
case WSAEISCONN: return EISCONN;
case WSAENOTCONN: return ENOTCONN;
case WSAETIMEDOUT: return ETIMEDOUT;
case WSAECONNREFUSED: return ECONNREFUSED;
case WSAELOOP: return ELOOP;
case WSAENAMETOOLONG: return ENAMETOOLONG;
case WSAEHOSTUNREACH: return EHOSTUNREACH;
case WSAENOTEMPTY: return ENOTEMPTY;
case WSAEINVAL: return EINVAL;
case WSAEFAULT: return EFAULT;
case 0: return 0;
default: return EIO;
}
}
void openconnect__win32_sock_init()
{
WSADATA data;
if (WSAStartup (MAKEWORD(1, 1), &data) != 0) {
fprintf(stderr, _("ERROR: Cannot initialize sockets\n"));
exit(1);
}
}
#ifdef OPENCONNECT_GNUTLS
int openconnect__win32_sock_poll(gnutls_transport_ptr_t ptr, unsigned int ms)
{
fd_set rfds;
struct timeval tv;
int fd = (int)(long)ptr;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = ms * 1000;
while(tv.tv_usec >= 1000000) {
tv.tv_usec -= 1000000;
tv.tv_sec++;
}
return select(fd+1, &rfds, NULL, NULL, &tv);
}
ssize_t openconnect__win32_sock_read(gnutls_transport_ptr_t ptr, void *data, size_t size)
{
return recv((long)ptr, data, size, 0);
}
ssize_t openconnect__win32_sock_write(gnutls_transport_ptr_t ptr, const void *data, size_t size)
{
return send((long)ptr, data, size, 0);
}
#endif /* OPENCONNECT_GNUTLS */
#endif /* _WIN32 */