1 // use rcb to compile: rcb rocksock_test.c
7 * License: LGPL 2.1+ with static linking exception
18 #include "../rocksock.h"
20 //RcB: LINK "-lpthread"
29 static pthread_mutex_t mutex
;
31 static void* scanHost(void* arg
) {
36 rocksock_set_timeout(soc
, 1500);
38 if (!rocksock_connect(soc
, d
->host
, d
->port
, 0)){
41 //printf("%s\n", soc->lasterror.errormsg);
45 rocksock_disconnect(soc
);
48 pthread_mutex_lock(&mutex
);
50 pthread_mutex_unlock(&mutex
);
54 int running(pthread_t
* t
, int max
) {
56 for (; i
<= max
; i
++) {
62 int joinThreads(pthread_t
* threads
, int max
) {
67 if (!pthread_join(threads
[i
], &exitstate
)) {
76 int scanRange(const char *ip
, int port
, int max_threads
) {
78 int maxthreads
= max_threads
> 254 ? 254 : max_threads
;
79 pthread_t threads
[255] = {0};
80 threaddata data
[255] = {0};
84 pthread_attr_init(&attr
);
85 pthread_attr_getstacksize(&attr
, &stack
);
87 pthread_attr_setstacksize(&attr
, stack
);
89 pthread_mutex_init(&mutex
, NULL
);
93 while(x
< 255 && running((pthread_t
*) threads
, x
) < maxthreads
) {
94 threaddata
* d
= &data
[x
];
95 snprintf(d
->host
, sizeof(d
->host
), "%s.%d", ip
, x
);
98 if (!pthread_create(&threads
[x
], &attr
, scanHost
, &data
[x
])) {
105 joinThreads(threads
, x
);
108 joinThreads(threads
, x
);
110 pthread_attr_destroy(&attr
);
113 for (; i
< 255; i
++) {
114 if (data
[i
].status
> 0) dprintf(1, "%s\n", data
[i
].host
);
117 pthread_mutex_destroy(&mutex
);
121 int main(int argc
, char** argv
) {
123 dprintf(2, "multithreaded subnet portscanner\n"
126 "subnetA port maxthreads\n", argv
[0]);
130 int port
= atoi(argv
[2]);
132 int maxthreads
= atoi(argv
[3]);
134 scanRange(ip
, port
, maxthreads
);