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 * Date Modified: June 26th 2001.
16 */
17
18
19
20 #include "EXTERN.h"
21 #include "perl.h"
22
23 #include "nw5iop.h"
24 #include "nw5sck.h"
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 // This is defined here since arpa\inet.h defines this array as an extern,
29 // and arpa\inet.h gets included by the inet_ntoa call.
30 char nwinet_scratch[18] = {'\0'};
31
32
33 u_long
nw_htonl(u_long hostlong)34 nw_htonl(u_long hostlong)
35 {
36 return htonl(hostlong);
37 }
38
39 u_short
nw_htons(u_short hostshort)40 nw_htons(u_short hostshort)
41 {
42 return htons(hostshort);
43 }
44
45 u_long
nw_ntohl(u_long netlong)46 nw_ntohl(u_long netlong)
47 {
48 return ntohl(netlong);
49 }
50
51 u_short
nw_ntohs(u_short netshort)52 nw_ntohs(u_short netshort)
53 {
54 return ntohs(netshort);
55 }
56
57 SOCKET
nw_accept(SOCKET s,struct sockaddr * addr,int * addrlen)58 nw_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
59 {
60 return ((SOCKET)(accept(s, addr, addrlen)));
61 }
62
63 int
nw_bind(SOCKET s,const struct sockaddr * addr,int addrlen)64 nw_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
65 {
66 return ((int)bind(s, (struct sockaddr *)addr, addrlen));
67
68 }
69
70 int
nw_connect(SOCKET s,const struct sockaddr * addr,int addrlen)71 nw_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
72 {
73 return((int)connect(s, (struct sockaddr *)addr, addrlen));
74 }
75
76 void
nw_endhostent()77 nw_endhostent()
78 {
79 endhostent();
80 }
81
82 void
nw_endnetent()83 nw_endnetent()
84 {
85 endnetent();
86 }
87
88 void
nw_endprotoent()89 nw_endprotoent()
90 {
91 endprotoent();
92 }
93
94 void
nw_endservent()95 nw_endservent()
96 {
97 endservent();
98 }
99
100 struct hostent *
nw_gethostent()101 nw_gethostent()
102 {
103 return(gethostent());
104 }
105
106 struct netent *
nw_getnetent(void)107 nw_getnetent(void)
108 {
109 return ((struct netent *) getnetent());
110 }
111
112 struct protoent *
nw_getprotoent(void)113 nw_getprotoent(void)
114 {
115 return ((struct protoent *) getprotoent());
116 }
117
118 struct hostent *
nw_gethostbyname(const char * name)119 nw_gethostbyname(const char *name)
120 {
121 return(gethostbyname((char*)name));
122 }
123
124 int
nw_gethostname(char * name,int len)125 nw_gethostname(char *name, int len)
126 {
127 return(gethostname(name, len));
128 }
129
130 struct hostent *
nw_gethostbyaddr(const char * addr,int len,int type)131 nw_gethostbyaddr(const char *addr, int len, int type)
132 {
133 return(gethostbyaddr((char*)addr, len, type));
134 }
135
136 struct netent *
nw_getnetbyaddr(long net,int type)137 nw_getnetbyaddr(long net, int type)
138 {
139 return(getnetbyaddr(net,type));
140 }
141
142 struct netent *
nw_getnetbyname(char * name)143 nw_getnetbyname(char *name)
144 {
145 return (struct netent *)getnetbyname(name);
146 }
147
148 int
nw_getpeername(SOCKET s,struct sockaddr * addr,int * addrlen)149 nw_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
150 {
151 return((int)getpeername(s, addr, addrlen));
152 }
153
154 struct protoent *
nw_getprotobyname(const char * name)155 nw_getprotobyname(const char *name)
156 {
157 return ((struct protoent *)getprotobyname((char*)name));
158 }
159
160 struct protoent *
nw_getprotobynumber(int num)161 nw_getprotobynumber(int num)
162 {
163 return ((struct protoent *)getprotobynumber(num));
164 }
165
166 struct servent *
nw_getservbyname(const char * name,const char * proto)167 nw_getservbyname(const char *name, const char *proto)
168 {
169 return (struct servent *)getservbyname((char*)name, (char*)proto);
170 }
171
172
173 struct servent *
nw_getservbyport(int port,const char * proto)174 nw_getservbyport(int port, const char *proto)
175 {
176 return (struct servent *)getservbyport(port, (char*)proto);
177 }
178
179 struct servent *
nw_getservent(void)180 nw_getservent(void)
181 {
182 return (struct servent *) getservent();
183 }
184
185 void
nw_sethostent(int stayopen)186 nw_sethostent(int stayopen)
187 {
188 #ifdef HAS_SETHOSTENT
189 sethostent(stayopen);
190 #endif
191 }
192
193 void
nw_setnetent(int stayopen)194 nw_setnetent(int stayopen)
195 {
196 #ifdef HAS_SETNETENT
197 setnetent(stayopen);
198 #endif
199 }
200
201 void
nw_setprotoent(int stayopen)202 nw_setprotoent(int stayopen)
203 {
204 #ifdef HAS_SETPROTENT
205 setprotoent(stayopen);
206 #endif
207 }
208
209 void
nw_setservent(int stayopen)210 nw_setservent(int stayopen)
211 {
212 #ifdef HAS_SETSERVENT
213 setservent(stayopen);
214 #endif
215 }
216
217 int
nw_setsockopt(SOCKET s,int level,int optname,const char * optval,int optlen)218 nw_setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen)
219 {
220 return setsockopt(s, level, optname, (char*)optval, optlen);
221 }
222
223 int
nw_getsockname(SOCKET s,struct sockaddr * addr,int * addrlen)224 nw_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
225 {
226 return getsockname(s, addr, addrlen);
227 }
228
229 int
nw_getsockopt(SOCKET s,int level,int optname,char * optval,int * optlen)230 nw_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
231 {
232 return ((int)getsockopt(s, level, optname, optval, optlen));
233 }
234
235 unsigned long
nw_inet_addr(const char * cp)236 nw_inet_addr(const char *cp)
237 {
238 return inet_addr((char*)cp);
239 }
240
241 char *
nw_inet_ntoa(struct in_addr in)242 nw_inet_ntoa(struct in_addr in)
243 {
244 return inet_ntoa(in);
245 }
246
247 SOCKET
nw_socket(int af,int type,int protocol)248 nw_socket(int af, int type, int protocol)
249 {
250 SOCKET s;
251
252 #ifndef USE_SOCKETS_AS_HANDLES
253 s = socket(af, type, protocol);
254 #else
255 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
256 //errno = WSAGetLastError();
257 else
258 s = s;
259 #endif /* USE_SOCKETS_AS_HANDLES */
260
261 return s;
262 }
263
264 int
nw_listen(SOCKET s,int backlog)265 nw_listen(SOCKET s, int backlog)
266 {
267 return(listen(s, backlog));
268 }
269
270 int
nw_send(SOCKET s,const char * buf,int len,int flags)271 nw_send(SOCKET s, const char *buf, int len, int flags)
272 {
273 return(send(s,(char*)buf,len,flags));
274 }
275
276 int
nw_recv(SOCKET s,char * buf,int len,int flags)277 nw_recv(SOCKET s, char *buf, int len, int flags)
278 {
279 return (recv(s, buf, len, flags));
280 }
281
282 int
nw_sendto(SOCKET s,const char * buf,int len,int flags,const struct sockaddr * to,int tolen)283 nw_sendto(SOCKET s, const char *buf, int len, int flags,
284 const struct sockaddr *to, int tolen)
285 {
286 return(sendto(s, (char*)buf, len, flags, (struct sockaddr *)to, tolen));
287 }
288
289 int
nw_recvfrom(SOCKET s,char * buf,int len,int flags,struct sockaddr * from,int * fromlen)290 nw_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
291 {
292 int r;
293 int frombufsize = *fromlen;
294
295 r = recvfrom(s, buf, len, flags, from, fromlen);
296 //Not sure if the is required - chksgp
297 if (r && frombufsize == *fromlen)
298 (void)nw_getpeername(s, from, fromlen);
299 return r;
300 }
301
302 int
nw_select(int nfds,fd_set * rd,fd_set * wr,fd_set * ex,const struct timeval * timeout)303 nw_select(int nfds, fd_set* rd, fd_set* wr, fd_set* ex, const struct timeval* timeout)
304 {
305 return(select(nfds, rd, wr, ex, (struct timeval*)timeout));
306 }
307
308 int
nw_shutdown(SOCKET s,int how)309 nw_shutdown(SOCKET s, int how)
310 {
311 return (shutdown(s, how));
312 }
313
314