-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.c
83 lines (73 loc) · 2.15 KB
/
tester.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
/* test.c -- put multiconnect through it's paces
*/
#include "easyv6.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h> /* exit */
#include <unistd.h> /* fcntl */
#include <fcntl.h> /* fcntl */
#include <string.h> /* memset */
#include <sys/socket.h>
long long milliseconds(void);
void nbgai_cancelagain (void);
void drainsocket (int socket) {
int fcntlflags, count;
char bytes[100];
fcntlflags = fcntl(socket,F_GETFL,0);
fcntl(socket,F_SETFL,fcntlflags & (~O_NONBLOCK));
shutdown (socket,SHUT_WR);
while ((count=read(socket,bytes,100))>0) {
fwrite (bytes,count,1,stdout);
}
shutdown (socket,SHUT_RD);
close (socket);
return;
}
int main (int argc, char **argv) {
int socket;
char *host, *service;
int i, l;
struct CONNECTOPTIONS options;
char s[200], *address, *p;
host="addrtest.dirtside.com";
service="ssh";
if (argc>1) host=argv[1];
if (argc>2) service=argv[2];
memset (&options,0,sizeof(options));
options.reportpicked = 1;
for (i=0; i<3; i++) {
printf ("Now: %lld\n",milliseconds());
socket = connectbyname (host,service,10000,&options);
address = addrinfototext(options.picked,s,200);
if (address) printf ("IP Address: %s\n",address);
if (options.picked) {
freeaddrinfo(options.picked);
/*if (options.picked->ai_canonname) free (options.picked->ai_canonname);
if (options.picked->ai_addr) free (options.picked->ai_addr);
free (options.picked);*/
options.picked=NULL;
}
if (socket<0) {
printf ("Test failed at %lld: socket=%d, errno=%d, error=%s\n",
milliseconds(),socket,errno,strerror(errno));
} else drainsocket(socket);
}
nbgai_cancelagain();
l = listenbyname("3000",SOCK_STREAM,10);
fprintf (stdout,"Listening on port 3000...\n");
i = accept (l,NULL,NULL);
p=getpeernametext(i,s,200);
if (p) {
printf ("Connect from: %s port %s\n",p,s);
} else {
printf ("getpeernametext failed at %lld: socket=%d, errno=%d, error=%s\n",
milliseconds(),i,errno,strerror(errno));
}
p="You connected to the tester. Bye!\n";
write (i,p,strlen(p));
shutdown (i,SHUT_RDWR);
close (i);
close (l);
nbgai_cancelagain();
return 0;
}