25
|
1 /* |
|
2 * This program is free software; you can redistribute it and/or modify |
|
3 * it under the terms of the GNU General Public License as published by |
|
4 * the Free Software Foundation; either version 2 of the License, or |
|
5 * (at your option) any later version. |
|
6 * |
|
7 * This program is distributed in the hope that it will be useful, |
|
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 * GNU General Public License for more details. |
|
11 * |
|
12 * You should have received a copy of the GNU General Public License |
|
13 * along with this program; if not, write to the Free Software |
|
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
15 * |
|
16 * Jabber |
|
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ |
|
18 */ |
|
19 |
|
20 #include "libxode.h" |
|
21 #include "connwrap.h" |
|
22 |
|
23 /* socket.c |
|
24 * |
|
25 * Simple wrapper to make socket creation easy. |
|
26 * type = NETSOCKET_SERVER is local listening socket |
|
27 * type = NETSOCKET_CLIENT is connection socket |
|
28 * type = NETSOCKET_UDP |
|
29 */ |
|
30 |
|
31 int make_netsocket(u_short port, char *host, int type, int ssl) |
|
32 { |
|
33 int s, flag = 1; |
|
34 struct sockaddr_in sa; |
|
35 struct in_addr *saddr; |
|
36 int socket_type; |
|
37 |
|
38 /* is this a UDP socket or a TCP socket? */ |
|
39 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM; |
|
40 |
|
41 bzero((void *)&sa,sizeof(struct sockaddr_in)); |
|
42 |
|
43 if((s = socket(AF_INET,socket_type,0)) < 0) |
|
44 return(-1); |
|
45 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0) |
|
46 return(-1); |
|
47 |
|
48 saddr = make_addr(host); |
|
49 if(saddr == NULL) |
|
50 return(-1); |
|
51 sa.sin_family = AF_INET; |
|
52 sa.sin_port = htons(port); |
|
53 |
|
54 if(type == NETSOCKET_SERVER) |
|
55 { |
|
56 /* bind to specific address if specified */ |
|
57 if(host != NULL) |
|
58 sa.sin_addr.s_addr = saddr->s_addr; |
|
59 |
|
60 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) |
|
61 { |
|
62 close(s); |
|
63 return(-1); |
|
64 } |
|
65 } |
|
66 if(type == NETSOCKET_CLIENT) |
|
67 { |
|
68 sa.sin_addr.s_addr = saddr->s_addr; |
|
69 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0) |
|
70 { |
|
71 close(s); |
|
72 return(-1); |
|
73 } |
|
74 } |
|
75 if(type == NETSOCKET_UDP) |
|
76 { |
|
77 /* bind to all addresses for now */ |
|
78 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) |
|
79 { |
|
80 close(s); |
|
81 return(-1); |
|
82 } |
|
83 |
|
84 /* specify default recipient for read/write */ |
|
85 sa.sin_addr.s_addr = saddr->s_addr; |
|
86 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0) |
|
87 { |
|
88 close(s); |
|
89 return(-1); |
|
90 } |
|
91 } |
|
92 |
|
93 |
|
94 return(s); |
|
95 } |
|
96 |
|
97 void change_socket_to_blocking(int s) |
|
98 /* make socket blocking */ |
|
99 { |
|
100 int val; |
|
101 val = fcntl(s, F_GETFL, 0); |
|
102 fcntl(s, F_SETFL, val & (~O_NONBLOCK)); |
|
103 } |
|
104 |
|
105 void change_socket_to_nonblocking(int s) |
|
106 /* make socket non-blocking */ |
|
107 { |
|
108 int val; |
|
109 val = fcntl(s, F_GETFL, 0); |
|
110 fcntl(s, F_SETFL, val | O_NONBLOCK); |
|
111 } |
|
112 |
|
113 /* socket.c |
|
114 * |
|
115 * Simple wrapper to make non-blocking client socket creation easy. |
|
116 * type = NETSOCKET_SERVER is local listening socket |
|
117 * type = NETSOCKET_CLIENT is connection socket |
|
118 * type = NETSOCKET_UDP |
|
119 */ |
|
120 |
|
121 int make_nb_netsocket(u_short port, char *host, int type, int ssl, int* state) |
|
122 { |
|
123 int s, flag = 1; |
|
124 struct sockaddr_in sa; |
|
125 struct in_addr *saddr; |
|
126 int socket_type; |
|
127 |
|
128 /* is this a UDP socket or a TCP socket? */ |
|
129 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM; |
|
130 |
|
131 bzero((void *)&sa,sizeof(struct sockaddr_in)); |
|
132 |
|
133 if((s = socket(AF_INET,socket_type,0)) < 0) |
|
134 return(-1); |
|
135 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0) |
|
136 return(-1); |
|
137 change_socket_to_nonblocking(s); |
|
138 |
|
139 saddr = make_addr(host); |
|
140 if(saddr == NULL) |
|
141 return(-1); |
|
142 sa.sin_family = AF_INET; |
|
143 sa.sin_port = htons(port); |
|
144 |
|
145 if(type == NETSOCKET_SERVER) |
|
146 { |
|
147 /* bind to specific address if specified */ |
|
148 if(host != NULL) |
|
149 sa.sin_addr.s_addr = saddr->s_addr; |
|
150 |
|
151 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) |
|
152 { |
|
153 close(s); |
|
154 return(-1); |
|
155 } |
|
156 } |
|
157 if(type == NETSOCKET_CLIENT) |
|
158 { |
|
159 int rc; |
|
160 sa.sin_addr.s_addr = saddr->s_addr; |
|
161 rc = cw_nb_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl, state); |
|
162 if (rc == -1 ) |
|
163 { |
|
164 close(s); |
|
165 return(-1); |
|
166 } |
|
167 } |
|
168 if(type == NETSOCKET_UDP) |
|
169 { |
|
170 /* bind to all addresses for now */ |
|
171 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) |
|
172 { |
|
173 close(s); |
|
174 return(-1); |
|
175 } |
|
176 |
|
177 /* specify default recipient for read/write */ |
|
178 sa.sin_addr.s_addr = saddr->s_addr; |
|
179 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0) |
|
180 { |
|
181 close(s); |
|
182 return(-1); |
|
183 } |
|
184 } |
|
185 |
|
186 |
|
187 return(s); |
|
188 } |
|
189 |
|
190 struct in_addr *make_addr(char *host) |
|
191 { |
|
192 struct hostent *hp; |
|
193 static struct in_addr addr; |
|
194 char myname[MAXHOSTNAMELEN + 1]; |
|
195 |
|
196 if(host == NULL || strlen(host) == 0) |
|
197 { |
|
198 gethostname(myname,MAXHOSTNAMELEN); |
|
199 hp = gethostbyname(myname); |
|
200 if(hp != NULL) |
|
201 { |
|
202 return (struct in_addr *) *hp->h_addr_list; |
|
203 } |
|
204 }else{ |
|
205 addr.s_addr = inet_addr(host); |
|
206 if(addr.s_addr != -1) |
|
207 { |
|
208 return &addr; |
|
209 } |
|
210 hp = gethostbyname(host); |
|
211 if(hp != NULL) |
|
212 { |
|
213 return (struct in_addr *) *hp->h_addr_list; |
|
214 } |
|
215 } |
|
216 return NULL; |
|
217 } |
|
218 |
|
219 /* Sets a file descriptor to close on exec. "flag" is 1 to close on exec, 0 to |
|
220 * leave open across exec. |
|
221 * -- EJB 7/31/2000 |
|
222 */ |
|
223 int set_fd_close_on_exec(int fd, int flag) |
|
224 { |
|
225 int oldflags = fcntl(fd,F_GETFL); |
|
226 int newflags; |
|
227 |
|
228 if(flag) |
|
229 newflags = oldflags | FD_CLOEXEC; |
|
230 else |
|
231 newflags = oldflags & (~FD_CLOEXEC); |
|
232 |
|
233 if(newflags==oldflags) |
|
234 return 0; |
|
235 return fcntl(fd,F_SETFL,(long)newflags); |
|
236 } |
|
237 |