This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
NetWare port from Guruprasad S <SGURUPRASAD@novell.com>.
[perl5.git] / NetWare / nw5sck.c
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
229 nw_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
230 {
231         return getsockname(s, addr, addrlen);
232 }
233
234 int
235 nw_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
236 {
237         return ((int)getsockopt(s, level, optname, optval, optlen));
238 }
239
240 unsigned long
241 nw_inet_addr(const char *cp)
242 {
243     return inet_addr((char*)cp);
244 }
245
246 static struct servent*
247 nw_savecopyservent(struct servent*d, struct servent*s, const char *proto)
248 {
249     d->s_name = s->s_name;
250     d->s_aliases = s->s_aliases;
251     d->s_port = s->s_port;
252 #ifndef __BORLANDC__    /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
253     if (/*!IsWin95() && */s->s_proto && strlen(s->s_proto))
254         d->s_proto = s->s_proto;
255     else
256 #endif
257     if (proto && strlen(proto))
258         d->s_proto = (char *)proto;
259     else
260         d->s_proto = "tcp";
261    
262     return d;
263 }
264
265 SOCKET
266 nw_socket(int af, int type, int protocol)
267 {
268     SOCKET s;
269
270 #ifndef USE_SOCKETS_AS_HANDLES
271     s = socket(af, type, protocol);
272 #else
273     //StartSockets();
274     if((s = socket(af, type, protocol)) == INVALID_SOCKET)
275         //errno = WSAGetLastError();
276     else
277         s = s;
278 #endif  /* USE_SOCKETS_AS_HANDLES */
279
280     return s;
281 }
282
283 int
284 nw_listen(SOCKET s, int backlog)
285 {
286     return(listen(s, backlog));
287 }
288
289 int
290 nw_send(SOCKET s, const char *buf, int len, int flags)
291 {
292         return(send(s,(char*)buf,len,flags));
293 }
294
295 int
296 nw_recv(SOCKET s, char *buf, int len, int flags)
297 {
298         return (recv(s, buf, len, flags));
299 }
300
301 int
302 nw_sendto(SOCKET s, const char *buf, int len, int flags,
303              const struct sockaddr *to, int tolen)
304 {
305     return(sendto(s, (char*)buf, len, flags, (struct sockaddr *)to, tolen));
306 }
307
308 int
309 nw_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
310 {
311     int r;
312     int frombufsize = *fromlen;
313
314     r = recvfrom(s, buf, len, flags, from, fromlen);
315         //Not sure if the is required - chksgp
316     if (r && frombufsize == *fromlen)
317         (void)nw_getpeername(s, from, fromlen);
318     return r;
319 }
320
321 int
322 nw_select(int nfds, fd_set* rd, fd_set* wr, fd_set* ex, const struct timeval* timeout)
323 {
324         return(select(nfds, rd, wr, ex, (struct timeval*)timeout));
325 }
326
327 int
328 nw_shutdown(SOCKET s, int how)
329 {
330     return (shutdown(s, how));
331 }
332