-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns.c
210 lines (174 loc) · 5.01 KB
/
ns.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
/* ns - example of very simple getaddrinfo()
*
* Copyright (C) 2021 Joachim Wiberg <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <poll.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/nameser.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#define info(fmt, args...) printf(fmt "\n", ##args); fflush(stdout)
#define SOCKET_TIMEOUT 10000
#define SERVICE_PORT 80
static int soerror(int sd)
{
int code = 0;
socklen_t len = sizeof(code);
if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &code, &len))
return 1;
return errno = code;
}
/*
* In the wonderful world of network programming the manual states that
* EINPROGRESS is only a possible error on non-blocking sockets. Real world
* experience, however, suggests otherwise. Simply poll() for completion and
* then continue. --Joachim
*/
static int check_error(int sd, int msec)
{
struct pollfd pfd = { sd, POLLOUT, 0 };
if (EINPROGRESS == errno) {
info("Waiting (%d sec) for three-way handshake to complete ...", msec / 1000);
if (poll (&pfd, 1, msec) > 0 && !soerror(sd)) {
info("Connected.");
return 0;
}
}
return 1;
}
/* timeout in msec */
static void set_timeouts(int sd, int timeout)
{
struct timeval sv;
memset(&sv, 0, sizeof(sv));
sv.tv_sec = timeout / 1000;
sv.tv_usec = (timeout % 1000) * 1000;
if (-1 == setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &sv, sizeof(sv)))
warn("Failed setting receive timeout socket option");
if (-1 == setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, &sv, sizeof(sv)))
warn("Failed setting send timeout socket option");
}
static int usage(int rc)
{
printf("Usage: ns [-ch?] [FQDN] [PORT]\n"
"\n"
"Options:\n"
" -c Attempt to connect\n"
" -h,-? This help text\n"
"\n");
return rc;
}
int main(int argc, char *argv[])
{
struct addrinfo hints, *servinfo, *ai;
char host[NI_MAXHOST];
char buf[10];
char *port = buf;
struct sockaddr *sa;
int try_connect = 0;
socklen_t len;
int tries = 0;
int rc = 0;
char *arg;
int c, sd;
while ((c = getopt(argc, argv, "ch?")) != EOF) {
switch (c) {
case 'c':
try_connect = 1;
break;
case 'h': case '?':
return usage(0);
default:
return usage(1);
}
}
if (optind >= argc)
return usage(1);
arg = argv[optind++];
if (optind < argc)
port = argv[optind++];
else
snprintf(buf, sizeof(buf), "%d", SERVICE_PORT);
/* Clear DNS cache before calling getaddrinfo(). */
res_init();
/* Obtain address(es) matching host/port */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Stream socket */
hints.ai_flags = AI_NUMERICSERV; /* No service name lookup */
rc = getaddrinfo(arg, port, &hints, &servinfo);
if (rc != 0 || !servinfo) {
warnx("Failed resolving hostname %s: %s", arg, gai_strerror(rc));
return 1;
}
ai = servinfo;
while (1) {
sd = socket(ai->ai_family, SOCK_STREAM, 0);
if (sd == -1)
err(1, "Error creating client socket");
/* Now we try connecting to the server, on connect fail, try next DNS record */
sa = ai->ai_addr;
len = ai->ai_addrlen;
/* Reverse lookup just to double-check */
if (getnameinfo(sa, len, host, sizeof(host), NULL, 0, NI_NUMERICHOST))
goto next;
info("Found %s on address %s:%s", arg, host, port);
if (!try_connect)
break;
set_timeouts(sd, SOCKET_TIMEOUT);
if (connect(sd, sa, len) && check_error(sd, SOCKET_TIMEOUT)) {
next:
tries++;
ai = ai->ai_next;
if (ai) {
if (errno == EINPROGRESS)
warnx("Failed connecting to %s, retrying ...", host);
else
warn("Failed connecting to %s", host);
close(sd);
continue;
}
warn("Failed connecting to %s", arg);
rc = 1;
}
break;
}
close(sd);
freeaddrinfo(servinfo);
return rc;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/