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.
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.
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.
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
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
31 int make_netsocket(u_short port
, char *host
, int type
, int ssl
)
34 struct sockaddr_in sa
;
35 struct in_addr
*saddr
;
38 /* is this a UDP socket or a TCP socket? */
39 socket_type
= (type
== NETSOCKET_UDP
)?SOCK_DGRAM
:SOCK_STREAM
;
41 bzero((void *)&sa
,sizeof(struct sockaddr_in
));
43 if((s
= socket(AF_INET
,socket_type
,0)) < 0)
45 if(setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&flag
, sizeof(flag
)) < 0)
48 saddr
= make_addr(host
);
51 sa
.sin_family
= AF_INET
;
52 sa
.sin_port
= htons(port
);
54 if(type
== NETSOCKET_SERVER
)
56 /* bind to specific address if specified */
58 sa
.sin_addr
.s_addr
= saddr
->s_addr
;
60 if(bind(s
,(struct sockaddr
*)&sa
,sizeof sa
) < 0)
66 if(type
== NETSOCKET_CLIENT
)
68 sa
.sin_addr
.s_addr
= saddr
->s_addr
;
69 if(cw_connect(s
,(struct sockaddr
*)&sa
,sizeof sa
,ssl
) < 0)
75 if(type
== NETSOCKET_UDP
)
77 /* bind to all addresses for now */
78 if(bind(s
,(struct sockaddr
*)&sa
,sizeof sa
) < 0)
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)
97 void change_socket_to_blocking(int s
)
98 /* make socket blocking */
101 val
= fcntl(s
, F_GETFL
, 0);
102 fcntl(s
, F_SETFL
, val
& (~O_NONBLOCK
));
105 void change_socket_to_nonblocking(int s
)
106 /* make socket non-blocking */
109 val
= fcntl(s
, F_GETFL
, 0);
110 fcntl(s
, F_SETFL
, val
| O_NONBLOCK
);
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
121 int make_nb_netsocket(u_short port
, char *host
, int type
, int ssl
, int* state
)
124 struct sockaddr_in sa
;
125 struct in_addr
*saddr
;
128 /* is this a UDP socket or a TCP socket? */
129 socket_type
= (type
== NETSOCKET_UDP
)?SOCK_DGRAM
:SOCK_STREAM
;
131 bzero((void *)&sa
,sizeof(struct sockaddr_in
));
133 if((s
= socket(AF_INET
,socket_type
,0)) < 0)
135 if(setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&flag
, sizeof(flag
)) < 0)
137 change_socket_to_nonblocking(s
);
139 saddr
= make_addr(host
);
142 sa
.sin_family
= AF_INET
;
143 sa
.sin_port
= htons(port
);
145 if(type
== NETSOCKET_SERVER
)
147 /* bind to specific address if specified */
149 sa
.sin_addr
.s_addr
= saddr
->s_addr
;
151 if(bind(s
,(struct sockaddr
*)&sa
,sizeof sa
) < 0)
157 if(type
== NETSOCKET_CLIENT
)
160 sa
.sin_addr
.s_addr
= saddr
->s_addr
;
161 rc
= cw_nb_connect(s
,(struct sockaddr
*)&sa
,sizeof sa
,ssl
, state
);
168 if(type
== NETSOCKET_UDP
)
170 /* bind to all addresses for now */
171 if(bind(s
,(struct sockaddr
*)&sa
,sizeof sa
) < 0)
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)
190 struct in_addr
*make_addr(char *host
)
193 static struct in_addr addr
;
194 char myname
[MAXHOSTNAMELEN
+ 1];
196 if(host
== NULL
|| strlen(host
) == 0)
198 gethostname(myname
,MAXHOSTNAMELEN
);
199 hp
= gethostbyname(myname
);
202 return (struct in_addr
*) *hp
->h_addr_list
;
205 addr
.s_addr
= inet_addr(host
);
206 if(addr
.s_addr
!= -1)
210 hp
= gethostbyname(host
);
213 return (struct in_addr
*) *hp
->h_addr_list
;
219 /* Sets a file descriptor to close on exec. "flag" is 1 to close on exec, 0 to
220 * leave open across exec.
223 int set_fd_close_on_exec(int fd
, int flag
)
225 int oldflags
= fcntl(fd
,F_GETFL
);
229 newflags
= oldflags
| FD_CLOEXEC
;
231 newflags
= oldflags
& (~FD_CLOEXEC
);
233 if(newflags
==oldflags
)
235 return fcntl(fd
,F_SETFL
,(long)newflags
);