comparison mcabber/libjabber/socket.c @ 417:c3ae9251c197

Sync libjabber with upstream Sync with jabberd-1.4.4.
author Mikael Berthe <mikael@lilotux.net>
date Thu, 01 Sep 2005 23:29:21 +0200
parents bf3d6e241714
children 5f43b532cc37
comparison
equal deleted inserted replaced
416:48e7808c4191 417:c3ae9251c197
11 * 11 *
12 * You should have received a copy of the GNU General Public License 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 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. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 * 15 *
16 * Jabber 16 * Copyrights
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ 17 *
18 * Portions created by or assigned to Jabber.com, Inc. are
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
21 *
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
23 *
24 * Acknowledgements
25 *
26 * Special thanks to the Jabber Open Source Contributors for their
27 * suggestions and support of Jabber.
28 *
29 */
30
31 /**
32 * @file socket.c
33 * @brief some misc functions to handle sockets
18 */ 34 */
19 35
20 #include "libxode.h" 36 #include "libxode.h"
21 #include "connwrap.h" 37 #include "connwrap.h"
22 38
23 /* socket.c 39 /**
24 *
25 * Simple wrapper to make socket creation easy. 40 * Simple wrapper to make socket creation easy.
26 * type = NETSOCKET_SERVER is local listening socket 41 *
27 * type = NETSOCKET_CLIENT is connection socket 42 * @param port port number of the socket
28 * type = NETSOCKET_UDP 43 * @param host hostname where to connect to or listen on
29 */ 44 * @param type type of socket (NETSOCKET_SERVER, NETSOCKET_CLIENT; or NETSOCKET_UDP)
30 45 * @return file handle of the new socket
46 *
47 * NETSOCKET_SERVER is local listening socket
48 * NETSOCKET_CLIENT is connection socket
49 */
31 int make_netsocket(u_short port, char *host, int type, int ssl) 50 int make_netsocket(u_short port, char *host, int type, int ssl)
32 { 51 {
33 int s, flag = 1; 52 int s, flag = 1;
34 struct sockaddr_in sa; 53 struct sockaddr_in sa;
35 struct in_addr *saddr; 54 struct in_addr *saddr;
39 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM; 58 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM;
40 59
41 bzero((void *)&sa,sizeof(struct sockaddr_in)); 60 bzero((void *)&sa,sizeof(struct sockaddr_in));
42 61
43 if((s = socket(AF_INET,socket_type,0)) < 0) 62 if((s = socket(AF_INET,socket_type,0)) < 0)
44 return(-1); 63 return(-1);
45 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0) 64 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0)
46 return(-1); 65 return(-1);
47 66
48 saddr = make_addr(host); 67 saddr = make_addr(host);
49 if(saddr == NULL) 68 if(saddr == NULL)
50 return(-1); 69 return(-1);
51 sa.sin_family = AF_INET; 70 sa.sin_family = AF_INET;
52 sa.sin_port = htons(port); 71 sa.sin_port = htons(port);
53 72
54 if(type == NETSOCKET_SERVER) 73 if(type == NETSOCKET_SERVER)
55 { 74 {
56 /* bind to specific address if specified */ 75 /* bind to specific address if specified */
57 if(host != NULL) 76 if(host != NULL)
58 sa.sin_addr.s_addr = saddr->s_addr; 77 sa.sin_addr.s_addr = saddr->s_addr;
59 78
60 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) 79 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
61 { 80 {
62 close(s); 81 close(s);
63 return(-1); 82 return(-1);
64 } 83 }
65 } 84 }
66 if(type == NETSOCKET_CLIENT) 85 if(type == NETSOCKET_CLIENT)
67 { 86 {
68 sa.sin_addr.s_addr = saddr->s_addr; 87 sa.sin_addr.s_addr = saddr->s_addr;
69 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0) 88 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0)
70 { 89 {
71 close(s); 90 close(s);
72 return(-1); 91 return(-1);
73 } 92 }
74 } 93 }
75 if(type == NETSOCKET_UDP) 94 if(type == NETSOCKET_UDP)
76 { 95 {
77 /* bind to all addresses for now */ 96 /* bind to all addresses for now */
78 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) 97 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
79 { 98 {
80 close(s); 99 close(s);
81 return(-1); 100 return(-1);
82 } 101 }
83 102
84 /* specify default recipient for read/write */ 103 /* specify default recipient for read/write */
85 sa.sin_addr.s_addr = saddr->s_addr; 104 sa.sin_addr.s_addr = saddr->s_addr;
86 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0) 105 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0)
87 { 106 {
88 close(s); 107 close(s);
89 return(-1); 108 return(-1);
90 } 109 }
91 } 110 }
92 111
93 112
94 return(s); 113 return(s);
95 } 114 }
129 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM; 148 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM;
130 149
131 bzero((void *)&sa,sizeof(struct sockaddr_in)); 150 bzero((void *)&sa,sizeof(struct sockaddr_in));
132 151
133 if((s = socket(AF_INET,socket_type,0)) < 0) 152 if((s = socket(AF_INET,socket_type,0)) < 0)
134 return(-1); 153 return(-1);
135 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0) 154 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0)
136 return(-1); 155 return(-1);
137 change_socket_to_nonblocking(s); 156 change_socket_to_nonblocking(s);
138 157
139 saddr = make_addr(host); 158 saddr = make_addr(host);
140 if(saddr == NULL) 159 if(saddr == NULL)
141 return(-1); 160 return(-1);
142 sa.sin_family = AF_INET; 161 sa.sin_family = AF_INET;
143 sa.sin_port = htons(port); 162 sa.sin_port = htons(port);
144 163
145 if(type == NETSOCKET_SERVER) 164 if(type == NETSOCKET_SERVER)
146 { 165 {
147 /* bind to specific address if specified */ 166 /* bind to specific address if specified */
148 if(host != NULL) 167 if(host != NULL)
149 sa.sin_addr.s_addr = saddr->s_addr; 168 sa.sin_addr.s_addr = saddr->s_addr;
150 169
151 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) 170 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
152 { 171 {
153 close(s); 172 close(s);
154 return(-1); 173 return(-1);
155 } 174 }
156 } 175 }
157 if(type == NETSOCKET_CLIENT) 176 if(type == NETSOCKET_CLIENT)
158 { 177 {
159 int rc; 178 int rc;
160 sa.sin_addr.s_addr = saddr->s_addr; 179 sa.sin_addr.s_addr = saddr->s_addr;
161 rc = cw_nb_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl, state); 180 rc = cw_nb_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl, state);
162 if (rc == -1 ) 181 if (rc == -1 )
163 { 182 {
164 close(s); 183 close(s);
165 return(-1); 184 return(-1);
166 } 185 }
167 } 186 }
168 if(type == NETSOCKET_UDP) 187 if(type == NETSOCKET_UDP)
169 { 188 {
170 /* bind to all addresses for now */ 189 /* bind to all addresses for now */
171 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) 190 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
172 { 191 {
173 close(s); 192 close(s);
174 return(-1); 193 return(-1);
175 } 194 }
176 195
177 /* specify default recipient for read/write */ 196 /* specify default recipient for read/write */
178 sa.sin_addr.s_addr = saddr->s_addr; 197 sa.sin_addr.s_addr = saddr->s_addr;
179 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0) 198 if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0)
180 { 199 {
181 close(s); 200 close(s);
182 return(-1); 201 return(-1);
183 } 202 }
184 } 203 }
185 204
186 205
187 return(s); 206 return(s);
188 } 207 }
191 { 210 {
192 struct hostent *hp; 211 struct hostent *hp;
193 static struct in_addr addr; 212 static struct in_addr addr;
194 char myname[MAXHOSTNAMELEN + 1]; 213 char myname[MAXHOSTNAMELEN + 1];
195 214
196 if(host == NULL || strlen(host) == 0) 215 if (host == NULL || strlen(host) == 0) {
197 { 216 gethostname(myname,MAXHOSTNAMELEN);
198 gethostname(myname,MAXHOSTNAMELEN); 217 hp = gethostbyname(myname);
199 hp = gethostbyname(myname); 218 if(hp != NULL) {
200 if(hp != NULL) 219 return (struct in_addr *) *hp->h_addr_list;
201 { 220 }
202 return (struct in_addr *) *hp->h_addr_list; 221 } else {
203 } 222 addr.s_addr = inet_addr(host);
204 }else{ 223 if(addr.s_addr != -1) {
205 addr.s_addr = inet_addr(host); 224 return &addr;
206 if(addr.s_addr != -1) 225 }
207 { 226 hp = gethostbyname(host);
208 return &addr; 227 if(hp != NULL) {
209 } 228 return (struct in_addr *) *hp->h_addr_list;
210 hp = gethostbyname(host); 229 }
211 if(hp != NULL)
212 {
213 return (struct in_addr *) *hp->h_addr_list;
214 }
215 } 230 }
216 return NULL; 231 return NULL;
217 } 232 }
218 233
219 /* Sets a file descriptor to close on exec. "flag" is 1 to close on exec, 0 to 234 /**
220 * leave open across exec. 235 * Sets a file descriptor to close on exec.
221 * -- EJB 7/31/2000 236 *
237 * @param fd the file descriptor
238 * @param flag 1 to close on exec, 0 to leave open across exec
239 *
240 * @deprecated this function is not used by jabberd14 and might be removed in future versions
222 */ 241 */
223 int set_fd_close_on_exec(int fd, int flag) 242 int set_fd_close_on_exec(int fd, int flag)
224 { 243 {
225 int oldflags = fcntl(fd,F_GETFL); 244 int oldflags = fcntl(fd,F_GETFL);
226 int newflags; 245 int newflags;
227 246
228 if(flag) 247 if(flag)
229 newflags = oldflags | FD_CLOEXEC; 248 newflags = oldflags | FD_CLOEXEC;
230 else 249 else
231 newflags = oldflags & (~FD_CLOEXEC); 250 newflags = oldflags & (~FD_CLOEXEC);
232 251
233 if(newflags==oldflags) 252 if(newflags==oldflags)
234 return 0; 253 return 0;
235 return fcntl(fd,F_SETFL,(long)newflags); 254 return fcntl(fd,F_SETFL,(long)newflags);
236 } 255 }
237 256