1 /*
2 * hosts.c
3 *
4 * This source code implements methods and algorithms for internet connections.
5 *
6 * written by Joshua J. Drake
7 * copyright (c) 1998
8 *
9 * -= SuckSoft =-
10 */
11
12 #include "hosts.h"
13 #include "output.h"
14 #include "vars.h"
15 #include "ircaux.h"
16 #include "newio.h"
17
18 int
getTCPSock(void)19 getTCPSock(void)
20 {
21 return socket(AF_INET, SOCK_STREAM, 0);
22 }
23
24 /*
25 * resolve hostname and store it in addr.
26 * return 1 or 0 for success or failure
27 */
28 int
resolve_host(addr,host)29 resolve_host(addr, host)
30 struct in_addr *addr;
31 u_char *host;
32 {
33 struct hostent *he;
34 unsigned long ipaddr;
35
36 ipaddr = inet_addr(host);
37 if ((int)ipaddr != -1)
38 {
39 memcpy(&(addr->s_addr), &ipaddr, sizeof(addr->s_addr));
40 return 1;
41 }
42 he = gethostbyname(host);
43 if (he == (struct hostent *)NULL)
44 return 0;
45 memcpy(&(addr->s_addr), he->h_addr, sizeof(addr->s_addr));
46 return 1;
47 }
48
49 /*
50 * connect to a host via the tcp
51 *
52 * returns:
53 * 1 success
54 * 0 failure, tell please print the error
55 * -1 failure, error was already printed
56 */
57 int
tcpConnect(int s,struct in_addr baddr,struct sockaddr_in addr,int alen)58 tcpConnect(int s, struct in_addr baddr, struct sockaddr_in addr, int alen)
59 {
60 struct sockaddr_in la;
61
62 if (s < 0)
63 return 0;
64
65 /* try to bind if we have something to bind to. */
66 if (baddr.s_addr != INADDR_ANY)
67 {
68 memset(&la, 0, sizeof(la));
69 la.sin_family = AF_INET;
70 la.sin_addr = baddr;
71 if (bind(s, (struct sockaddr *)&la, sizeof(la)) == -1)
72 {
73 put_error("Unable to bind to address \"%s\": %s", inet_ntoa(baddr), strerror(errno));
74 new_close(s);
75 return -1;
76 }
77 }
78 /* if we have a socks proxy set, bounce through it. (blocking)
79 *
80 * this has moved to server.c... much better place for it...
81 */
82 #ifdef NON_BLOCKING_CONNECTS
83 if (set_non_blocking(s) < 0)
84 {
85 new_close(s);
86 put_error("Unable to set socket non-blocking: %s", strerror(errno));
87 return -1;
88 }
89 #endif
90 /* attempt to connect to remote host on the socket we've allocated.. */
91 if (connect(s, (struct sockaddr *)&addr, alen) < 0)
92 {
93 if (errno != EINPROGRESS)
94 {
95 new_close(s);
96 return 0;
97 }
98 }
99 return s;
100 }
101