]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscanconnect.c
1 /* proxyscanconnect: handle connections etc. */
7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include "../core/error.h"
11 #include <sys/ioctl.h>
14 int createconnectsocket(struct irc_in_addr
*ip
, int socknum
) {
16 struct sockaddr_in sin
;
17 struct sockaddr_in6 sin6
;
26 if(irc_in_addr_is_ipv4(ip
)) {
28 proto
=u
.sin
.sin_family
=AF_INET
;
29 u
.sin
.sin_port
=htons(socknum
);
30 u
.sin
.sin_addr
.s_addr
=htonl(irc_in_addr_v4_to_int(ip
));
33 proto
=u
.sin6
.sin6_family
=AF_INET6
;
34 u
.sin6
.sin6_port
=htons(socknum
);
35 memcpy(&u
.sin6
.sin6_addr
.s6_addr
, ip
->in6_16
, sizeof(ip
->in6_16
));
38 if ((fd
=socket(proto
,SOCK_STREAM
,0))<0) {
39 Error("proxyscan",ERR_ERROR
,"Unable to create socket (%d)",errno
);
42 if (ioctl(fd
,FIONBIO
,&res
)!=0) {
44 Error("proxyscan",ERR_ERROR
,"Unable to make socket nonblocking");
47 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *) &opt
, sizeof(opt
))!=0) {
49 Error("proxyscan",ERR_ERROR
,"Unable to set SO_REUSEADDR on scan socket.");
53 opt
=IP_PORTRANGE_HIGH
;
54 if (setsockopt(fd
, IPPROTO_IP
, IP_PORTRANGE
, (char *) &opt
, sizeof(opt
))!=0) {
56 Error("proxyscan",ERR_WARNING
,"Error selecting high port range.");
60 if (connect(fd
,(const struct sockaddr
*) &u
, s
)) {
61 if (errno
!= EINPROGRESS
) {
63 Error("proxyscan",ERR_ERROR
,"Unable to connect socket (%d)",errno
);