1 | #include <sys/types.h> |
---|
2 | #include <sys/socket.h> |
---|
3 | #include <netinet/in.h> |
---|
4 | #include <fcntl.h> |
---|
5 | #include <sys/time.h> |
---|
6 | #include <errno.h> |
---|
7 | #include <pthread.h> |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | int gport = 0; |
---|
11 | int gtimeout = 5; |
---|
12 | |
---|
13 | void *conn_thread(void *data) |
---|
14 | { |
---|
15 | /* |
---|
16 | int flags, ret; |
---|
17 | fd_set socks; |
---|
18 | struct timeval timeout; |
---|
19 | */ |
---|
20 | int sock; |
---|
21 | struct sockaddr_in addr; |
---|
22 | char* ip = data; |
---|
23 | |
---|
24 | /* |
---|
25 | timeout.tv_sec = gtimeout; |
---|
26 | timeout.tv_usec = 0; |
---|
27 | */ |
---|
28 | |
---|
29 | addr.sin_addr.s_addr = inet_addr(ip); |
---|
30 | //addr.sin_addr.s_addr = inet_addr("212.152.181.199"); |
---|
31 | addr.sin_port = htons(gport); |
---|
32 | addr.sin_family = AF_INET; |
---|
33 | |
---|
34 | sock = socket(AF_INET, SOCK_STREAM, 0); |
---|
35 | if(sock == -1) |
---|
36 | { |
---|
37 | perror("socket() failed"); |
---|
38 | free(ip); |
---|
39 | return (char *)1; |
---|
40 | } |
---|
41 | |
---|
42 | /* |
---|
43 | FD_ZERO(&socks); |
---|
44 | FD_SET(sock, &socks); |
---|
45 | |
---|
46 | flags = fcntl(sock, F_GETFL, 0); |
---|
47 | fcntl(sock, F_SETFL, flags|O_NONBLOCK); |
---|
48 | */ |
---|
49 | |
---|
50 | connect(sock, (struct sockaddr *) &addr, sizeof(addr)); |
---|
51 | if(errno == 0) |
---|
52 | { |
---|
53 | printf("%s (%d) ok\n", ip, gport); |
---|
54 | free(ip); |
---|
55 | close(sock); |
---|
56 | return 0; |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | //printf("%s (%d) nok - %s\n", ip, gport, strerror(errno)); |
---|
61 | free(ip); |
---|
62 | close(sock); |
---|
63 | return (char *)1; |
---|
64 | } |
---|
65 | |
---|
66 | /* |
---|
67 | if(errno != 115) |
---|
68 | { |
---|
69 | perror("connect() failed"); |
---|
70 | free(ip); |
---|
71 | close(sock); |
---|
72 | return (char *)1; |
---|
73 | } |
---|
74 | |
---|
75 | ret = select(sock + 1, 0, &socks, 0, &timeout); |
---|
76 | if(ret < 0) |
---|
77 | { |
---|
78 | perror("select() failed"); |
---|
79 | free(ip); |
---|
80 | close(sock); |
---|
81 | return (char *)1; |
---|
82 | } |
---|
83 | if(ret == 0) |
---|
84 | { |
---|
85 | printf("%s (%d) nok\n", ip, gport); |
---|
86 | free(ip); |
---|
87 | close(sock); |
---|
88 | return (char *)1; |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | printf("%s (%d) ok\n", ip, gport); |
---|
93 | free(ip); |
---|
94 | close(sock); |
---|
95 | return 0; |
---|
96 | } |
---|
97 | */ |
---|
98 | } |
---|
99 | |
---|
100 | main(int argc, char *argv[]) |
---|
101 | { |
---|
102 | int i; |
---|
103 | char *ip; |
---|
104 | pthread_t th[256]; |
---|
105 | |
---|
106 | if(argc != 6) |
---|
107 | { |
---|
108 | printf("usage: timeout ip port start end\n"); |
---|
109 | printf("bsp: 5 192.168.0 21 0 255\n"); |
---|
110 | return 1; |
---|
111 | } |
---|
112 | |
---|
113 | gtimeout = atoi(argv[1]); |
---|
114 | gport = atoi(argv[3]); |
---|
115 | |
---|
116 | for(i=atoi(argv[4]); i<=atoi(argv[5]); i++) |
---|
117 | { |
---|
118 | ip = malloc(16); |
---|
119 | snprintf(ip, 16, "%s.%d", argv[2], i); |
---|
120 | //printf("ip=%s, %p\n", ip, ip); |
---|
121 | |
---|
122 | pthread_create(th + i, 0, conn_thread, ip); |
---|
123 | } |
---|
124 | sleep(gtimeout); |
---|
125 | |
---|
126 | /* |
---|
127 | for(i=atoi(argv[4]); i<=atoi(argv[5]); i++) |
---|
128 | { |
---|
129 | pthread_join(th[i], NULL); |
---|
130 | } |
---|
131 | */ |
---|
132 | } |
---|