This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document the bad assumptions currently breaking
[perl5.git] / ext / Socket / Socket.xs
CommitLineData
c5be433b 1#define PERL_NO_GET_CONTEXT
a0d0e21e
LW
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
8e07c86e
AD
6#ifndef VMS
7# ifdef I_SYS_TYPES
8# include <sys/types.h>
9# endif
9cc6feab 10# include <sys/socket.h>
29209bc5 11# if defined(USE_SOCKS) && defined(I_SOCKS)
86959918 12# include <socks.h>
a062834f 13# endif
9cc6feab
JH
14# ifdef MPE
15# define PF_INET AF_INET
16# define PF_UNIX AF_UNIX
17# define SOCK_RAW 3
18# endif
19# ifdef I_SYS_UN
20# include <sys/un.h>
21# endif
bf9b2f8f
JH
22/* XXX Configure test for <netinet/in_systm.h needed XXX */
23# if defined(NeXT) || defined(__NeXT__)
24# include <netinet/in_systm.h>
25# endif
8e07c86e
AD
26# ifdef I_NETINET_IN
27# include <netinet/in.h>
28# endif
86959918
JH
29# ifdef I_NETDB
30# include <netdb.h>
31# endif
9cc6feab
JH
32# ifdef I_ARPA_INET
33# include <arpa/inet.h>
34# endif
35# ifdef I_NETINET_TCP
36# include <netinet/tcp.h>
37# endif
8e07c86e 38#else
9cc6feab 39# include "sockadapt.h"
8e07c86e 40#endif
a0d0e21e 41
2986a63f
JH
42#ifdef NETWARE
43NETDB_DEFINE_CONTEXT
44NETINET_DEFINE_CONTEXT
45#endif
46
6b1016b5
JH
47#ifdef I_SYSUIO
48# include <sys/uio.h>
49#endif
50
a0d0e21e 51#ifndef AF_NBS
9cc6feab 52# undef PF_NBS
a0d0e21e
LW
53#endif
54
55#ifndef AF_X25
9cc6feab 56# undef PF_X25
a0d0e21e
LW
57#endif
58
8e07c86e 59#ifndef INADDR_NONE
9cc6feab 60# define INADDR_NONE 0xffffffff
8e07c86e 61#endif /* INADDR_NONE */
7e1af8bc 62#ifndef INADDR_BROADCAST
9cc6feab 63# define INADDR_BROADCAST 0xffffffff
7e1af8bc 64#endif /* INADDR_BROADCAST */
8e07c86e 65#ifndef INADDR_LOOPBACK
9cc6feab 66# define INADDR_LOOPBACK 0x7F000001
8e07c86e
AD
67#endif /* INADDR_LOOPBACK */
68
7e1af8bc
PP
69#ifndef HAS_INET_ATON
70
a062834f 71/*
7e1af8bc
PP
72 * Check whether "cp" is a valid ascii representation
73 * of an Internet address and convert to a binary address.
74 * Returns 1 if the address is valid, 0 if not.
75 * This replaces inet_addr, the return value from which
76 * cannot distinguish between failure and a local broadcast address.
77 */
78static int
f0f333f4 79my_inet_aton(register const char *cp, struct in_addr *addr)
7e1af8bc 80{
c5be433b 81 dTHX;
0caed002 82 register U32 val;
7e1af8bc
PP
83 register int base;
84 register char c;
85 int nparts;
86 const char *s;
87 unsigned int parts[4];
88 register unsigned int *pp = parts;
89
0caed002
CS
90 if (!cp)
91 return 0;
7e1af8bc
PP
92 for (;;) {
93 /*
94 * Collect number up to ``.''.
95 * Values are specified as for C:
96 * 0x=hex, 0=octal, other=decimal.
97 */
98 val = 0; base = 10;
99 if (*cp == '0') {
100 if (*++cp == 'x' || *cp == 'X')
101 base = 16, cp++;
102 else
103 base = 8;
104 }
105 while ((c = *cp) != '\0') {
106 if (isDIGIT(c)) {
107 val = (val * base) + (c - '0');
108 cp++;
109 continue;
110 }
3280af22 111 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
a062834f 112 val = (val << 4) +
3280af22 113 ((s - PL_hexdigit) & 15);
7e1af8bc
PP
114 cp++;
115 continue;
116 }
117 break;
118 }
119 if (*cp == '.') {
120 /*
121 * Internet format:
122 * a.b.c.d
123 * a.b.c (with c treated as 16-bits)
124 * a.b (with b treated as 24 bits)
125 */
126 if (pp >= parts + 3 || val > 0xff)
127 return 0;
128 *pp++ = val, cp++;
129 } else
130 break;
131 }
132 /*
133 * Check for trailing characters.
134 */
135 if (*cp && !isSPACE(*cp))
136 return 0;
137 /*
138 * Concoct the address according to
139 * the number of parts specified.
140 */
141 nparts = pp - parts + 1; /* force to an int for switch() */
142 switch (nparts) {
143
144 case 1: /* a -- 32 bits */
145 break;
146
147 case 2: /* a.b -- 8.24 bits */
148 if (val > 0xffffff)
149 return 0;
150 val |= parts[0] << 24;
151 break;
152
153 case 3: /* a.b.c -- 8.8.16 bits */
154 if (val > 0xffff)
155 return 0;
156 val |= (parts[0] << 24) | (parts[1] << 16);
157 break;
158
159 case 4: /* a.b.c.d -- 8.8.8.8 bits */
160 if (val > 0xff)
161 return 0;
162 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
163 break;
164 }
165 addr->s_addr = htonl(val);
166 return 1;
167}
168
169#undef inet_aton
170#define inet_aton my_inet_aton
171
172#endif /* ! HAS_INET_ATON */
173
8e07c86e 174
a0d0e21e 175static int
f0f333f4 176not_here(char *s)
a0d0e21e
LW
177{
178 croak("Socket::%s not implemented on this architecture", s);
179 return -1;
180}
181
ee96af8f 182#include "constants.c"
8e07c86e 183
a0d0e21e
LW
184MODULE = Socket PACKAGE = Socket
185
ee96af8f 186INCLUDE: constants.xs
8e07c86e
AD
187
188void
189inet_aton(host)
190 char * host
191 CODE:
192 {
193 struct in_addr ip_address;
194 struct hostent * phe;
ac9fe1c2 195 int ok = inet_aton(host, &ip_address);
8e07c86e 196
ac9fe1c2 197 if (!ok && (phe = gethostbyname(host))) {
8e07c86e 198 Copy( phe->h_addr, &ip_address, phe->h_length, char );
7e1af8bc 199 ok = 1;
8e07c86e
AD
200 }
201
202 ST(0) = sv_newmortal();
7e1af8bc 203 if (ok) {
8e07c86e
AD
204 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
205 }
206 }
207
208void
209inet_ntoa(ip_address_sv)
210 SV * ip_address_sv
211 CODE:
212 {
213 STRLEN addrlen;
214 struct in_addr addr;
215 char * addr_str;
1d68b939
JH
216 char * ip_address;
217 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
b2d93eb8 218 croak("Wide character in Socket::inet_ntoa");
1d68b939 219 ip_address = SvPV(ip_address_sv,addrlen);
219c81c4
JH
220 /* Bad assumptions here.
221 *
222 * Bad Assumption 1: struct in_addr has no other fields than
223 * the s_addr (which is the field we really care about here).
224 *
225 * Bad Assumption 2: the s_addr field is the first field
226 * in struct in_addr (the Copy() assumes that).
227 *
228 * Bad Assumption 3: the s_addr field is a simple type
229 * (such as an int). It can be a bit field, in which
230 * case using & (address-of) on it or taking sizeof()
231 * wouldn't go over too well. (Those are not attempted
232 * now but in case someone thinks to fix the below uses
233 * of addr (both in the length check and the Copy())
234 * by using addr.s_addr.
235 *
236 * These bad assumptions currently break UNICOS which has
237 * struct in_addr struct { u_long st_addr:32; } s_da;
238 * #define s_addr s_da.st_addr
239 *
240 * and u_long is 64 bits.
241 *
242 * The bold soul attempting to fix this should also
243 * fix pack_sockaddr_in() to agree.
244 *
245 * --jhi
246 */
8e07c86e
AD
247 if (addrlen != sizeof(addr)) {
248 croak("Bad arg length for %s, length is %d, should be %d",
249 "Socket::inet_ntoa",
250 addrlen, sizeof(addr));
251 }
8e07c86e 252 Copy( ip_address, &addr, sizeof addr, char );
df8513eb 253#if defined(__hpux) && defined(__GNUC__) && defined(USE_64_BIT_INT)
1d68b939
JH
254 /* GCC on HP_UX breaks the call to inet_ntoa --sky */
255 New(1138, addr_str, 4 * 3 + 3 + 1, char);
256 sprintf(addr_str, "%d.%d.%d.%d",
257 ((addr.s_addr >> 24) & 0xFF),
258 ((addr.s_addr >> 16) & 0xFF),
259 ((addr.s_addr >> 8) & 0xFF),
260 ( addr.s_addr & 0xFF));
261 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
262 Safefree(addr_str);
df8513eb 263#else
8e07c86e 264 addr_str = inet_ntoa(addr);
79cb57f6 265 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
1d68b939 266#endif
8e07c86e
AD
267 }
268
269void
4633a7c4
LW
270pack_sockaddr_un(pathname)
271 char * pathname
272 CODE:
273 {
25f94b33 274#ifdef I_SYS_UN
4633a7c4 275 struct sockaddr_un sun_ad; /* fear using sun */
fcdb74fc 276 STRLEN len;
202975e6 277
4633a7c4
LW
278 Zero( &sun_ad, sizeof sun_ad, char );
279 sun_ad.sun_family = AF_UNIX;
20408e3c
GS
280 len = strlen(pathname);
281 if (len > sizeof(sun_ad.sun_path))
282 len = sizeof(sun_ad.sun_path);
202975e6
IZ
283# ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
284 {
285 int off;
286 char *s, *e;
287
288 if (pathname[0] != '/' && pathname[0] != '\\')
289 croak("Relative UNIX domain socket name '%s' unsupported", pathname);
a062834f 290 else if (len < 8
202975e6
IZ
291 || pathname[7] != '/' && pathname[7] != '\\'
292 || !strnicmp(pathname + 1, "socket", 6))
293 off = 7;
294 else
295 off = 0; /* Preserve names starting with \socket\ */
296 Copy( "\\socket", sun_ad.sun_path, off, char);
297 Copy( pathname, sun_ad.sun_path + off, len, char );
298
299 s = sun_ad.sun_path + off - 1;
300 e = s + len + 1;
301 while (++s < e)
302 if (*s = '/')
303 *s = '\\';
304 }
a062834f 305# else /* !( defined OS2 ) */
20408e3c 306 Copy( pathname, sun_ad.sun_path, len, char );
202975e6 307# endif
a062834f 308 if (0) not_here("dummy");
79cb57f6 309 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
25f94b33
AD
310#else
311 ST(0) = (SV *) not_here("pack_sockaddr_un");
312#endif
313
4633a7c4
LW
314 }
315
316void
317unpack_sockaddr_un(sun_sv)
318 SV * sun_sv
f13e1b2f 319 CODE:
4633a7c4 320 {
25f94b33 321#ifdef I_SYS_UN
4633a7c4 322 struct sockaddr_un addr;
fcdb74fc
TB
323 STRLEN sockaddrlen;
324 char * sun_ad = SvPV(sun_sv,sockaddrlen);
325 char * e;
36902e12
RG
326# ifndef __linux__
327 /* On Linux sockaddrlen on sockets returned by accept, recvfrom,
328 getpeername and getsockname is not equal to sizeof(addr). */
4633a7c4
LW
329 if (sockaddrlen != sizeof(addr)) {
330 croak("Bad arg length for %s, length is %d, should be %d",
331 "Socket::unpack_sockaddr_un",
332 sockaddrlen, sizeof(addr));
333 }
36902e12 334# endif
4633a7c4
LW
335
336 Copy( sun_ad, &addr, sizeof addr, char );
337
338 if ( addr.sun_family != AF_UNIX ) {
339 croak("Bad address family for %s, got %d, should be %d",
340 "Socket::unpack_sockaddr_un",
341 addr.sun_family,
342 AF_UNIX);
fcdb74fc
TB
343 }
344 e = addr.sun_path;
345 while (*e && e < addr.sun_path + sizeof addr.sun_path)
346 ++e;
79cb57f6 347 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
25f94b33
AD
348#else
349 ST(0) = (SV *) not_here("unpack_sockaddr_un");
350#endif
4633a7c4
LW
351 }
352
353void
354pack_sockaddr_in(port,ip_address)
2c129a17 355 unsigned short port
8e07c86e
AD
356 char * ip_address
357 CODE:
358 {
359 struct sockaddr_in sin;
360
361 Zero( &sin, sizeof sin, char );
4633a7c4 362 sin.sin_family = AF_INET;
8e07c86e
AD
363 sin.sin_port = htons(port);
364 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
365
79cb57f6 366 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
8e07c86e
AD
367 }
368
369void
370unpack_sockaddr_in(sin_sv)
371 SV * sin_sv
372 PPCODE:
373 {
374 STRLEN sockaddrlen;
375 struct sockaddr_in addr;
2c129a17 376 unsigned short port;
8e07c86e
AD
377 struct in_addr ip_address;
378 char * sin = SvPV(sin_sv,sockaddrlen);
379 if (sockaddrlen != sizeof(addr)) {
380 croak("Bad arg length for %s, length is %d, should be %d",
381 "Socket::unpack_sockaddr_in",
382 sockaddrlen, sizeof(addr));
383 }
8e07c86e 384 Copy( sin, &addr,sizeof addr, char );
4633a7c4
LW
385 if ( addr.sin_family != AF_INET ) {
386 croak("Bad address family for %s, got %d, should be %d",
387 "Socket::unpack_sockaddr_in",
388 addr.sin_family,
389 AF_INET);
a062834f 390 }
8e07c86e
AD
391 port = ntohs(addr.sin_port);
392 ip_address = addr.sin_addr;
393
924508f0 394 EXTEND(SP, 2);
2c129a17 395 PUSHs(sv_2mortal(newSViv((IV) port)));
79cb57f6 396 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address)));
8e07c86e 397 }