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