Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/miniupnpc/src/connecthostport.c
9904 views
1
/* $Id: connecthostport.c,v 1.25 2025/05/24 15:59:08 nanard Exp $ */
2
/* vim: tabstop=4 shiftwidth=4 noexpandtab
3
* Project : miniupnp
4
* Web : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
5
* Author : Thomas Bernard
6
* Copyright (c) 2010-2025 Thomas Bernard
7
* This software is subject to the conditions detailed in the
8
* LICENCE file provided in this distribution. */
9
10
/* use getaddrinfo() or gethostbyname()
11
* uncomment the following line in order to use gethostbyname() */
12
#ifdef NO_GETADDRINFO
13
#define USE_GETHOSTBYNAME
14
#endif
15
16
#include <string.h>
17
#include <stdio.h>
18
#ifdef _WIN32
19
#define WIN32_LEAN_AND_MEAN
20
#include <winsock2.h>
21
#include <ws2tcpip.h>
22
#include <io.h>
23
#define MAXHOSTNAMELEN 64
24
#include "win32_snprintf.h"
25
#define herror
26
#define socklen_t int
27
#else /* #ifdef _WIN32 */
28
#include <unistd.h>
29
#include <sys/types.h>
30
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
31
#include <sys/time.h>
32
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
33
#include <sys/param.h>
34
#include <sys/select.h>
35
#include <errno.h>
36
#define closesocket close
37
#include <netdb.h>
38
#include <netinet/in.h>
39
/* defining MINIUPNPC_IGNORE_EINTR enable the ignore of interruptions
40
* during the connect() call */
41
#define MINIUPNPC_IGNORE_EINTR
42
#include <sys/socket.h>
43
#include <sys/select.h>
44
#endif /* #else _WIN32 */
45
46
#if defined(__amigaos__) || defined(__amigaos4__)
47
#define herror(A) printf("%s\n", A)
48
#endif
49
50
#include "connecthostport.h"
51
52
#ifndef MAXHOSTNAMELEN
53
#define MAXHOSTNAMELEN 64
54
#endif
55
56
/* connecthostport()
57
* return a socket connected (TCP) to the host and port
58
* or -1 in case of error */
59
SOCKET connecthostport(const char * host, unsigned short port,
60
unsigned int scope_id)
61
{
62
SOCKET s;
63
int n;
64
#ifdef USE_GETHOSTBYNAME
65
struct sockaddr_in dest;
66
struct hostent *hp;
67
#else /* #ifdef USE_GETHOSTBYNAME */
68
char tmp_host[MAXHOSTNAMELEN+1];
69
char port_str[8];
70
struct addrinfo *ai, *p;
71
struct addrinfo hints;
72
#endif /* #ifdef USE_GETHOSTBYNAME */
73
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
74
struct timeval timeout;
75
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
76
77
#ifdef USE_GETHOSTBYNAME
78
hp = gethostbyname(host);
79
if(hp == NULL)
80
{
81
herror(host);
82
return INVALID_SOCKET;
83
}
84
memcpy(&dest.sin_addr, hp->h_addr, sizeof(dest.sin_addr));
85
memset(dest.sin_zero, 0, sizeof(dest.sin_zero));
86
s = socket(PF_INET, SOCK_STREAM, 0);
87
if(ISINVALID(s))
88
{
89
PRINT_SOCKET_ERROR("socket");
90
return INVALID_SOCKET;
91
}
92
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
93
/* setting a 3 seconds timeout for the connect() call */
94
timeout.tv_sec = 3;
95
timeout.tv_usec = 0;
96
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
97
{
98
PRINT_SOCKET_ERROR("setsockopt SO_RCVTIMEO");
99
}
100
timeout.tv_sec = 3;
101
timeout.tv_usec = 0;
102
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
103
{
104
PRINT_SOCKET_ERROR("setsockopt SO_SNDTIMEO");
105
}
106
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
107
dest.sin_family = AF_INET;
108
dest.sin_port = htons(port);
109
n = connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr_in));
110
#ifdef MINIUPNPC_IGNORE_EINTR
111
/* EINTR The system call was interrupted by a signal that was caught
112
* EINPROGRESS The socket is nonblocking and the connection cannot
113
* be completed immediately. */
114
while(n < 0 && (errno == EINTR || errno == EINPROGRESS))
115
{
116
socklen_t len;
117
fd_set wset;
118
int err;
119
FD_ZERO(&wset);
120
FD_SET(s, &wset);
121
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
122
timeout.tv_sec = 3;
123
timeout.tv_usec = 0;
124
n = select(s + 1, NULL, &wset, NULL, &timeout);
125
#else
126
n = select(s + 1, NULL, &wset, NULL, NULL);
127
#endif
128
if(n < 0) {
129
if (errno == EINTR)
130
continue; /* try again */
131
else
132
break; /* EBADF, EFAULT, EINVAL */
133
}
134
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
135
if(n == 0) {
136
errno = ETIMEDOUT;
137
n = -1;
138
break;
139
}
140
#endif
141
len = sizeof(err);
142
if(getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
143
PRINT_SOCKET_ERROR("getsockopt");
144
closesocket(s);
145
return INVALID_SOCKET;
146
}
147
if(err != 0) {
148
errno = err;
149
n = -1;
150
}
151
}
152
#endif /* #ifdef MINIUPNPC_IGNORE_EINTR */
153
if(n<0)
154
{
155
PRINT_SOCKET_ERROR("connect");
156
closesocket(s);
157
return INVALID_SOCKET;
158
}
159
#else /* #ifdef USE_GETHOSTBYNAME */
160
/* use getaddrinfo() instead of gethostbyname() */
161
memset(&hints, 0, sizeof(hints));
162
/* hints.ai_flags = AI_ADDRCONFIG; */
163
#ifdef AI_NUMERICSERV
164
hints.ai_flags = AI_NUMERICSERV;
165
#endif
166
hints.ai_socktype = SOCK_STREAM;
167
hints.ai_family = AF_UNSPEC; /* AF_INET, AF_INET6 or AF_UNSPEC */
168
/* hints.ai_protocol = IPPROTO_TCP; */
169
snprintf(port_str, sizeof(port_str), "%hu", port);
170
if(host[0] == '[')
171
{
172
/* literal ip v6 address */
173
int i, j;
174
for(i = 0, j = 1; host[j] && (host[j] != ']') && i < MAXHOSTNAMELEN; i++, j++)
175
{
176
tmp_host[i] = host[j];
177
if(0 == strncmp(host+j, "%25", 3)) /* %25 is just url encoding for '%' */
178
j+=2; /* skip "25" */
179
}
180
tmp_host[i] = '\0';
181
}
182
else
183
{
184
strncpy(tmp_host, host, MAXHOSTNAMELEN);
185
}
186
tmp_host[MAXHOSTNAMELEN] = '\0';
187
n = getaddrinfo(tmp_host, port_str, &hints, &ai);
188
if(n != 0)
189
{
190
#ifdef _WIN32
191
fprintf(stderr, "getaddrinfo() error : %d\n", n);
192
#else
193
fprintf(stderr, "getaddrinfo() error : %s\n", gai_strerror(n));
194
#endif
195
return INVALID_SOCKET;
196
}
197
s = INVALID_SOCKET;
198
for(p = ai; p; p = p->ai_next)
199
{
200
if(!ISINVALID(s))
201
closesocket(s);
202
#ifdef DEBUG
203
printf("ai_family=%d ai_socktype=%d ai_protocol=%d (PF_INET=%d, PF_INET6=%d)\n",
204
p->ai_family, p->ai_socktype, p->ai_protocol, PF_INET, PF_INET6);
205
#endif
206
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
207
if(ISINVALID(s))
208
continue;
209
if(p->ai_addr->sa_family == AF_INET6 && scope_id > 0) {
210
struct sockaddr_in6 * addr6 = (struct sockaddr_in6 *)p->ai_addr;
211
addr6->sin6_scope_id = scope_id;
212
}
213
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
214
/* setting a 3 seconds timeout for the connect() call */
215
timeout.tv_sec = 3;
216
timeout.tv_usec = 0;
217
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
218
{
219
PRINT_SOCKET_ERROR("setsockopt");
220
}
221
timeout.tv_sec = 3;
222
timeout.tv_usec = 0;
223
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
224
{
225
PRINT_SOCKET_ERROR("setsockopt");
226
}
227
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
228
n = connect(s, p->ai_addr, MSC_CAST_INT p->ai_addrlen);
229
#ifdef MINIUPNPC_IGNORE_EINTR
230
/* EINTR The system call was interrupted by a signal that was caught
231
* EINPROGRESS The socket is nonblocking and the connection cannot
232
* be completed immediately. */
233
while(n < 0 && (errno == EINTR || errno == EINPROGRESS))
234
{
235
socklen_t len;
236
fd_set wset;
237
int err;
238
FD_ZERO(&wset);
239
FD_SET(s, &wset);
240
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
241
timeout.tv_sec = 3;
242
timeout.tv_usec = 0;
243
n = select(s + 1, NULL, &wset, NULL, &timeout);
244
#else
245
n = select(s + 1, NULL, &wset, NULL, NULL);
246
#endif
247
if(n < 0) {
248
if (errno == EINTR)
249
continue; /* try again */
250
else
251
break; /* EBADF, EFAULT, EINVAL */
252
}
253
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
254
if(n == 0) {
255
errno = ETIMEDOUT;
256
n = -1;
257
break;
258
}
259
#endif
260
len = sizeof(err);
261
if(getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
262
PRINT_SOCKET_ERROR("getsockopt");
263
closesocket(s);
264
freeaddrinfo(ai);
265
return INVALID_SOCKET;
266
}
267
if(err != 0) {
268
errno = err;
269
n = -1;
270
}
271
}
272
#endif /* #ifdef MINIUPNPC_IGNORE_EINTR */
273
if(n >= 0) /* connect() was successful */
274
break;
275
}
276
freeaddrinfo(ai);
277
if(ISINVALID(s))
278
{
279
PRINT_SOCKET_ERROR("socket");
280
return INVALID_SOCKET;
281
}
282
if(n < 0)
283
{
284
PRINT_SOCKET_ERROR("connect");
285
closesocket(s);
286
return INVALID_SOCKET;
287
}
288
#endif /* #ifdef USE_GETHOSTBYNAME */
289
return s;
290
}
291
292