Commit | Line | Data |
2986a63f |
1 | |
2 | /* |
3 | * Copyright © 2001 Novell, Inc. All Rights Reserved. |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. |
7 | * |
8 | */ |
9 | |
10 | /* |
11 | * FILENAME : nw5sck.c |
12 | * DESCRIPTION : Socket related functions. |
13 | * Author : SGP |
14 | * Date : January 2001. |
15 | * |
16 | */ |
17 | |
18 | |
19 | |
20 | #include "EXTERN.h" |
21 | #include "perl.h" |
22 | |
23 | #if defined(PERL_OBJECT) |
24 | #define NO_XSLOCKS |
25 | #include "XSUB.h" |
26 | #endif |
27 | |
28 | #include "nw5iop.h" |
29 | #include "nw5sck.h" |
30 | #include <fcntl.h> |
31 | #include <sys/stat.h> |
32 | |
33 | static struct servent* nw_savecopyservent(struct servent*d, |
34 | struct servent*s, |
35 | const char *proto); |
36 | |
37 | |
38 | u_long |
39 | nw_htonl(u_long hostlong) |
40 | { |
41 | return htonl(hostlong); |
42 | } |
43 | |
44 | u_short |
45 | nw_htons(u_short hostshort) |
46 | { |
47 | return htons(hostshort); |
48 | } |
49 | |
50 | u_long |
51 | nw_ntohl(u_long netlong) |
52 | { |
53 | return ntohl(netlong); |
54 | } |
55 | |
56 | u_short |
57 | nw_ntohs(u_short netshort) |
58 | { |
59 | return ntohs(netshort); |
60 | } |
61 | |
62 | SOCKET |
63 | nw_accept(SOCKET s, struct sockaddr *addr, int *addrlen) |
64 | { |
65 | return ((SOCKET)(accept(s, addr, addrlen))); |
66 | } |
67 | |
68 | int |
69 | nw_bind(SOCKET s, const struct sockaddr *addr, int addrlen) |
70 | { |
71 | return ((int)bind(s, (struct sockaddr *)addr, addrlen)); |
72 | |
73 | } |
74 | |
75 | int |
76 | nw_connect(SOCKET s, const struct sockaddr *addr, int addrlen) |
77 | { |
78 | return((int)connect(s, (struct sockaddr *)addr, addrlen)); |
79 | } |
80 | |
81 | void |
82 | nw_endhostent() |
83 | { |
84 | endhostent(); |
85 | } |
86 | |
87 | void |
88 | nw_endnetent() |
89 | { |
90 | endnetent(); |
91 | } |
92 | |
93 | void |
94 | nw_endprotoent() |
95 | { |
96 | endprotoent(); |
97 | } |
98 | |
99 | void |
100 | nw_endservent() |
101 | { |
102 | endservent(); |
103 | } |
104 | |
105 | struct hostent * |
106 | nw_gethostent() |
107 | { |
108 | return(gethostent()); |
109 | } |
110 | |
111 | struct netent * |
112 | nw_getnetent(void) |
113 | { |
114 | return ((struct netent *) getnetent()); |
115 | } |
116 | |
117 | struct protoent * |
118 | nw_getprotoent(void) |
119 | { |
120 | return ((struct protoent *) getprotoent()); |
121 | } |
122 | |
123 | struct hostent * |
124 | nw_gethostbyname(const char *name) |
125 | { |
126 | return(gethostbyname((char*)name)); |
127 | } |
128 | |
129 | int |
130 | nw_gethostname(char *name, int len) |
131 | { |
132 | return(gethostname(name, len)); |
133 | } |
134 | |
135 | struct hostent * |
136 | nw_gethostbyaddr(const char *addr, int len, int type) |
137 | { |
138 | return(gethostbyaddr((char*)addr, len, type)); |
139 | } |
140 | |
141 | struct netent * |
142 | nw_getnetbyaddr(long net, int type) |
143 | { |
144 | return(getnetbyaddr(net,type)); |
145 | } |
146 | |
147 | struct netent * |
148 | nw_getnetbyname(char *name) |
149 | { |
150 | return (struct netent *)getnetbyname(name); |
151 | } |
152 | |
153 | int |
154 | nw_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen) |
155 | { |
156 | return((int)getpeername(s, addr, addrlen)); |
157 | } |
158 | |
159 | struct protoent * |
160 | nw_getprotobyname(const char *name) |
161 | { |
162 | return ((struct protoent *)getprotobyname((char*)name)); |
163 | } |
164 | |
165 | struct protoent * |
166 | nw_getprotobynumber(int num) |
167 | { |
168 | return ((struct protoent *)getprotobynumber(num)); |
169 | } |
170 | |
171 | struct servent * |
172 | nw_getservbyname(const char *name, const char *proto) |
173 | { |
174 | dTHXo; |
175 | struct servent *r; |
176 | |
177 | r = getservbyname((char*)name, (char*)proto); |
178 | if (r) { |
179 | /*r = nw_savecopyservent(&nw_servent, r, proto);*/ |
180 | } |
181 | return r; |
182 | } |
183 | |
184 | |
185 | struct servent * |
186 | nw_getservbyport(int port, const char *proto) |
187 | { |
188 | dTHXo; |
189 | struct servent *r; |
190 | |
191 | r = getservbyport(port, (char*)proto); |
192 | if (r) { |
193 | //r = nw_savecopyservent(&nw_servent, r, proto); |
194 | } |
195 | return r; |
196 | } |
197 | |
198 | struct servent * |
199 | nw_getservent(void) |
200 | { |
201 | return (struct servent *) getservent(); |
202 | } |
203 | |
204 | void |
205 | nw_sethostent(int stayopen) |
206 | { |
207 | sethostent(stayopen); |
208 | } |
209 | |
210 | void |
211 | nw_setnetent(int stayopen) |
212 | { |
213 | setnetent(stayopen); |
214 | } |
215 | |
216 | void |
217 | nw_setprotoent(int stayopen) |
218 | { |
219 | setprotoent(stayopen); |
220 | } |
221 | |
222 | void |
223 | nw_setservent(int stayopen) |
224 | { |
225 | setservent(stayopen); |
226 | } |
227 | |
228 | int |
225a5dca |
229 | nw_setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen) |
230 | { |
231 | return setsockopt(s, level, optname, optval, optlen); |
232 | } |
233 | |
234 | int |
2986a63f |
235 | nw_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen) |
236 | { |
237 | return getsockname(s, addr, addrlen); |
238 | } |
239 | |
240 | int |
241 | nw_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen) |
242 | { |
243 | return ((int)getsockopt(s, level, optname, optval, optlen)); |
244 | } |
245 | |
246 | unsigned long |
247 | nw_inet_addr(const char *cp) |
248 | { |
249 | return inet_addr((char*)cp); |
250 | } |
251 | |
252 | static struct servent* |
253 | nw_savecopyservent(struct servent*d, struct servent*s, const char *proto) |
254 | { |
255 | d->s_name = s->s_name; |
256 | d->s_aliases = s->s_aliases; |
257 | d->s_port = s->s_port; |
258 | #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */ |
259 | if (/*!IsWin95() && */s->s_proto && strlen(s->s_proto)) |
260 | d->s_proto = s->s_proto; |
261 | else |
262 | #endif |
263 | if (proto && strlen(proto)) |
264 | d->s_proto = (char *)proto; |
265 | else |
266 | d->s_proto = "tcp"; |
267 | |
268 | return d; |
269 | } |
270 | |
271 | SOCKET |
272 | nw_socket(int af, int type, int protocol) |
273 | { |
274 | SOCKET s; |
275 | |
276 | #ifndef USE_SOCKETS_AS_HANDLES |
277 | s = socket(af, type, protocol); |
278 | #else |
279 | //StartSockets(); |
280 | if((s = socket(af, type, protocol)) == INVALID_SOCKET) |
281 | //errno = WSAGetLastError(); |
282 | else |
283 | s = s; |
284 | #endif /* USE_SOCKETS_AS_HANDLES */ |
285 | |
286 | return s; |
287 | } |
288 | |
289 | int |
290 | nw_listen(SOCKET s, int backlog) |
291 | { |
292 | return(listen(s, backlog)); |
293 | } |
294 | |
295 | int |
296 | nw_send(SOCKET s, const char *buf, int len, int flags) |
297 | { |
298 | return(send(s,(char*)buf,len,flags)); |
299 | } |
300 | |
301 | int |
302 | nw_recv(SOCKET s, char *buf, int len, int flags) |
303 | { |
304 | return (recv(s, buf, len, flags)); |
305 | } |
306 | |
307 | int |
308 | nw_sendto(SOCKET s, const char *buf, int len, int flags, |
309 | const struct sockaddr *to, int tolen) |
310 | { |
311 | return(sendto(s, (char*)buf, len, flags, (struct sockaddr *)to, tolen)); |
312 | } |
313 | |
314 | int |
315 | nw_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen) |
316 | { |
317 | int r; |
318 | int frombufsize = *fromlen; |
319 | |
320 | r = recvfrom(s, buf, len, flags, from, fromlen); |
321 | //Not sure if the is required - chksgp |
322 | if (r && frombufsize == *fromlen) |
323 | (void)nw_getpeername(s, from, fromlen); |
324 | return r; |
325 | } |
326 | |
327 | int |
328 | nw_select(int nfds, fd_set* rd, fd_set* wr, fd_set* ex, const struct timeval* timeout) |
329 | { |
330 | return(select(nfds, rd, wr, ex, (struct timeval*)timeout)); |
331 | } |
332 | |
333 | int |
334 | nw_shutdown(SOCKET s, int how) |
335 | { |
336 | return (shutdown(s, how)); |
337 | } |
338 | |