]>
Commit | Line | Data |
---|---|---|
1 | /* proxyscanconnect: handle connections etc. */ | |
2 | ||
3 | #include "proxyscan.h" | |
4 | #include <stdlib.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <netinet/in.h> | |
8 | #include <errno.h> | |
9 | #include "../core/error.h" | |
10 | #include <sys/ioctl.h> | |
11 | #include <string.h> | |
12 | ||
13 | int createconnectsocket(long ip, int socknum) { | |
14 | int fd; | |
15 | struct sockaddr_in sin; | |
16 | int res=1; | |
17 | unsigned int opt=1; | |
18 | ||
19 | memset(&sin,0,sizeof(sin)); | |
20 | ||
21 | sin.sin_family=AF_INET; | |
22 | sin.sin_port=htons(socknum); | |
23 | sin.sin_addr.s_addr=htonl(ip); | |
24 | ||
25 | if ((fd=socket(AF_INET,SOCK_STREAM,0))<0) { | |
26 | Error("proxyscan",ERR_ERROR,"Unable to create socket (%d)",errno); | |
27 | return -1; | |
28 | } | |
29 | if (ioctl(fd,FIONBIO,&res)!=0) { | |
30 | Error("proxyscan",ERR_ERROR,"Unable to make socket nonblocking"); | |
31 | return -1; | |
32 | } | |
33 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &opt, sizeof(opt))!=0) { | |
34 | Error("proxyscan",ERR_ERROR,"Unable to set SO_REUSEADDR on scan socket."); | |
35 | return -1; | |
36 | } | |
37 | #ifdef __FreeBSD__ | |
38 | opt=IP_PORTRANGE_HIGH; | |
39 | if (setsockopt(fd, IPPROTO_IP, IP_PORTRANGE, (char *) &opt, sizeof(opt))!=0) { | |
40 | Error("proxyscan",ERR_WARNING,"Error selecting high port range."); | |
41 | } | |
42 | #endif | |
43 | if (connect(fd,(const struct sockaddr *) &sin, sizeof(sin))) { | |
44 | if (errno != EINPROGRESS) { | |
45 | Error("proxyscan",ERR_ERROR,"Unable to connect socket (%d)",errno); | |
46 | return -1; | |
47 | } | |
48 | } | |
49 | return fd; | |
50 | } | |
51 |