3127
|
1 /* --------------------------------------------------------------------------
|
|
2 *
|
|
3 * License
|
|
4 *
|
|
5 * The contents of this file are subject to the Jabber Open Source License
|
|
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either
|
|
7 * source code or executable form, except in compliance with the JOSL. You
|
|
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
|
|
9 * http://www.opensource.org/.
|
|
10 *
|
|
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
|
|
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL
|
|
13 * for the specific language governing rights and limitations under the
|
|
14 * JOSL.
|
|
15 *
|
|
16 * Copyrights
|
|
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/.
|
2086
|
21 *
|
3127
|
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 * Alternatively, the contents of this file may be used under the terms of the
|
|
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
|
|
31 * the provisions of the GPL are applicable instead of those above. If you
|
|
32 * wish to allow use of your version of this file only under the terms of the
|
|
33 * GPL and not to allow others to use your version of this file under the JOSL,
|
|
34 * indicate your decision by deleting the provisions above and replace them
|
|
35 * with the notice and other provisions required by the GPL. If you do not
|
|
36 * delete the provisions above, a recipient may use your version of this file
|
|
37 * under either the JOSL or the GPL.
|
|
38 *
|
|
39 *
|
|
40 * --------------------------------------------------------------------------*/
|
2086
|
41
|
3127
|
42 #include "lib.h"
|
2086
|
43
|
|
44 /* socket.c
|
|
45 *
|
|
46 * Simple wrapper to make socket creation easy.
|
|
47 * type = NETSOCKET_SERVER is local listening socket
|
|
48 * type = NETSOCKET_CLIENT is connection socket
|
3127
|
49 * type = NETSOCKET_UDP is a UDP connection socket
|
2086
|
50 */
|
|
51
|
|
52 int make_netsocket(u_short port, char *host, int type)
|
|
53 {
|
|
54 int s, flag = 1;
|
|
55 struct sockaddr_in sa;
|
|
56 struct in_addr *saddr;
|
|
57 int socket_type;
|
|
58
|
|
59 /* is this a UDP socket or a TCP socket? */
|
|
60 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM;
|
|
61
|
|
62 bzero((void *)&sa,sizeof(struct sockaddr_in));
|
|
63
|
|
64 if((s = socket(AF_INET,socket_type,0)) < 0)
|
|
65 return(-1);
|
|
66 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0)
|
|
67 return(-1);
|
|
68
|
|
69 saddr = make_addr(host);
|
3127
|
70 if(saddr == NULL && type != NETSOCKET_UDP)
|
2086
|
71 return(-1);
|
|
72 sa.sin_family = AF_INET;
|
|
73 sa.sin_port = htons(port);
|
|
74
|
|
75 if(type == NETSOCKET_SERVER)
|
|
76 {
|
|
77 /* bind to specific address if specified */
|
|
78 if(host != NULL)
|
|
79 sa.sin_addr.s_addr = saddr->s_addr;
|
|
80
|
|
81 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
|
|
82 {
|
|
83 close(s);
|
|
84 return(-1);
|
|
85 }
|
|
86 }
|
|
87 if(type == NETSOCKET_CLIENT)
|
|
88 {
|
|
89 sa.sin_addr.s_addr = saddr->s_addr;
|
|
90 if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0)
|
|
91 {
|
|
92 close(s);
|
|
93 return(-1);
|
|
94 }
|
|
95 }
|
|
96 if(type == NETSOCKET_UDP)
|
|
97 {
|
|
98 /* bind to all addresses for now */
|
|
99 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
|
|
100 {
|
|
101 close(s);
|
|
102 return(-1);
|
|
103 }
|
|
104
|
3127
|
105 /* if specified, use a default recipient for read/write */
|
|
106 if(host != NULL && saddr != NULL)
|
2086
|
107 {
|
3127
|
108 sa.sin_addr.s_addr = saddr->s_addr;
|
|
109 if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0)
|
|
110 {
|
|
111 close(s);
|
|
112 return(-1);
|
|
113 }
|
2086
|
114 }
|
|
115 }
|
|
116
|
|
117
|
|
118 return(s);
|
|
119 }
|
|
120
|
|
121
|
|
122 struct in_addr *make_addr(char *host)
|
|
123 {
|
|
124 struct hostent *hp;
|
|
125 static struct in_addr addr;
|
|
126 char myname[MAXHOSTNAMELEN + 1];
|
|
127
|
|
128 if(host == NULL || strlen(host) == 0)
|
|
129 {
|
|
130 gethostname(myname,MAXHOSTNAMELEN);
|
|
131 hp = gethostbyname(myname);
|
|
132 if(hp != NULL)
|
|
133 {
|
|
134 return (struct in_addr *) *hp->h_addr_list;
|
|
135 }
|
|
136 }else{
|
|
137 addr.s_addr = inet_addr(host);
|
|
138 if(addr.s_addr != -1)
|
|
139 {
|
|
140 return &addr;
|
|
141 }
|
|
142 hp = gethostbyname(host);
|
|
143 if(hp != NULL)
|
|
144 {
|
|
145 return (struct in_addr *) *hp->h_addr_list;
|
|
146 }
|
|
147 }
|
|
148 return NULL;
|
|
149 }
|
|
150
|
|
151 /* Sets a file descriptor to close on exec. "flag" is 1 to close on exec, 0 to
|
|
152 * leave open across exec.
|
|
153 * -- EJB 7/31/2000
|
|
154 */
|
|
155 int set_fd_close_on_exec(int fd, int flag)
|
|
156 {
|
|
157 int oldflags = fcntl(fd,F_GETFL);
|
|
158 int newflags;
|
|
159
|
|
160 if(flag)
|
|
161 newflags = oldflags | FD_CLOEXEC;
|
|
162 else
|
|
163 newflags = oldflags & (~FD_CLOEXEC);
|
|
164
|
|
165 if(newflags==oldflags)
|
|
166 return 0;
|
|
167 return fcntl(fd,F_SETFL,(long)newflags);
|
|
168 }
|
|
169
|