1 #define PERL_NO_GET_CONTEXT
8 # include <sys/types.h>
10 # include <sys/socket.h>
11 # if defined(USE_SOCKS) && defined(I_SOCKS)
15 # define PF_INET AF_INET
16 # define PF_UNIX AF_UNIX
22 /* XXX Configure test for <netinet/in_systm.h needed XXX */
23 # if defined(NeXT) || defined(__NeXT__)
24 # include <netinet/in_systm.h>
27 # include <netinet/in.h>
33 # include <arpa/inet.h>
36 # include <netinet/tcp.h>
39 # include "sockadapt.h"
44 NETINET_DEFINE_CONTEXT
60 # define INADDR_NONE 0xffffffff
61 #endif /* INADDR_NONE */
62 #ifndef INADDR_BROADCAST
63 # define INADDR_BROADCAST 0xffffffff
64 #endif /* INADDR_BROADCAST */
65 #ifndef INADDR_LOOPBACK
66 # define INADDR_LOOPBACK 0x7F000001
67 #endif /* INADDR_LOOPBACK */
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.
79 my_inet_aton(register const char *cp, struct in_addr *addr)
87 unsigned int parts[4];
88 register unsigned int *pp = parts;
94 * Collect number up to ``.''.
95 * Values are specified as for C:
96 * 0x=hex, 0=octal, other=decimal.
100 if (*++cp == 'x' || *cp == 'X')
105 while ((c = *cp) != '\0') {
107 val = (val * base) + (c - '0');
111 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
113 ((s - PL_hexdigit) & 15);
123 * a.b.c (with c treated as 16-bits)
124 * a.b (with b treated as 24 bits)
126 if (pp >= parts + 3 || val > 0xff)
133 * Check for trailing characters.
135 if (*cp && !isSPACE(*cp))
138 * Concoct the address according to
139 * the number of parts specified.
141 nparts = pp - parts + 1; /* force to an int for switch() */
144 case 1: /* a -- 32 bits */
147 case 2: /* a.b -- 8.24 bits */
150 val |= parts[0] << 24;
153 case 3: /* a.b.c -- 8.8.16 bits */
156 val |= (parts[0] << 24) | (parts[1] << 16);
159 case 4: /* a.b.c.d -- 8.8.8.8 bits */
162 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
165 addr->s_addr = htonl(val);
170 #define inet_aton my_inet_aton
172 #endif /* ! HAS_INET_ATON */
178 croak("Socket::%s not implemented on this architecture", s);
182 #define PERL_IN_ADDR_S_ADDR_SIZE 4
185 * Bad assumptions possible here.
187 * Bad Assumption 1: struct in_addr has no other fields
188 * than the s_addr (which is the field we care about
189 * in here, really). However, we can be fed either 4-byte
190 * addresses (from pack("N", ...), or va.b.c.d, or ...),
191 * or full struct in_addrs (from e.g. pack_sockaddr_in()),
192 * which may or may not be 4 bytes in size.
194 * Bad Assumption 2: the s_addr field is a simple type
195 * (such as an int, u_int32_t). It can be a bit field,
196 * in which case using & (address-of) on it or taking sizeof()
197 * wouldn't go over too well. (Those are not attempted
198 * now but in case someone thinks to change the below code
199 * to use addr.s_addr instead of addr, you have been warned.)
201 * Bad Assumption 3: the s_addr is the first field in
202 * an in_addr, or that its bytes are the first bytes in
205 * These bad assumptions are wrong in UNICOS which has
206 * struct in_addr { struct { u_long st_addr:32; } s_da };
207 * #define s_addr s_da.st_addr
208 * and u_long is 64 bits.
212 #include "constants.c"
214 MODULE = Socket PACKAGE = Socket
216 INCLUDE: constants.xs
223 struct in_addr ip_address;
224 struct hostent * phe;
228 inet_aton(host, &ip_address);
230 if (!ok && (phe = gethostbyname(host))) {
231 Copy( phe->h_addr, &ip_address, phe->h_length, char );
235 ST(0) = sv_newmortal();
237 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
241 inet_ntoa(ip_address_sv)
249 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
250 croak("Wide character in Socket::inet_ntoa");
251 ip_address = SvPV(ip_address_sv, addrlen);
252 if (addrlen == sizeof(addr) || addrlen == 4)
254 (ip_address[0] & 0xFF) << 24 |
255 (ip_address[1] & 0xFF) << 16 |
256 (ip_address[2] & 0xFF) << 8 |
257 (ip_address[3] & 0xFF);
259 croak("Bad arg length for %s, length is %d, should be %d",
261 addrlen, sizeof(addr));
262 /* We could use inet_ntoa() but that is broken
263 * in HP-UX + GCC + 64bitint (returns "0.0.0.0"),
264 * so let's use this sprintf() workaround everywhere. */
265 New(1138, addr_str, 4 * 3 + 3 + 1, char);
266 sprintf(addr_str, "%d.%d.%d.%d",
267 ((addr.s_addr >> 24) & 0xFF),
268 ((addr.s_addr >> 16) & 0xFF),
269 ((addr.s_addr >> 8) & 0xFF),
270 ( addr.s_addr & 0xFF));
271 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
276 pack_sockaddr_un(pathname)
281 struct sockaddr_un sun_ad; /* fear using sun */
284 Zero( &sun_ad, sizeof sun_ad, char );
285 sun_ad.sun_family = AF_UNIX;
286 len = strlen(pathname);
287 if (len > sizeof(sun_ad.sun_path))
288 len = sizeof(sun_ad.sun_path);
289 # ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
294 if (pathname[0] != '/' && pathname[0] != '\\')
295 croak("Relative UNIX domain socket name '%s' unsupported", pathname);
297 || pathname[7] != '/' && pathname[7] != '\\'
298 || !strnicmp(pathname + 1, "socket", 6))
301 off = 0; /* Preserve names starting with \socket\ */
302 Copy( "\\socket", sun_ad.sun_path, off, char);
303 Copy( pathname, sun_ad.sun_path + off, len, char );
305 s = sun_ad.sun_path + off - 1;
311 # else /* !( defined OS2 ) */
312 Copy( pathname, sun_ad.sun_path, len, char );
314 if (0) not_here("dummy");
315 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
317 ST(0) = (SV *) not_here("pack_sockaddr_un");
323 unpack_sockaddr_un(sun_sv)
328 struct sockaddr_un addr;
330 char * sun_ad = SvPV(sun_sv,sockaddrlen);
333 /* On Linux sockaddrlen on sockets returned by accept, recvfrom,
334 getpeername and getsockname is not equal to sizeof(addr). */
335 if (sockaddrlen != sizeof(addr)) {
336 croak("Bad arg length for %s, length is %d, should be %d",
337 "Socket::unpack_sockaddr_un",
338 sockaddrlen, sizeof(addr));
342 Copy( sun_ad, &addr, sizeof addr, char );
344 if ( addr.sun_family != AF_UNIX ) {
345 croak("Bad address family for %s, got %d, should be %d",
346 "Socket::unpack_sockaddr_un",
351 while (*e && e < addr.sun_path + sizeof addr.sun_path)
353 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
355 ST(0) = (SV *) not_here("unpack_sockaddr_un");
360 pack_sockaddr_in(port, ip_address_sv)
365 struct sockaddr_in sin;
369 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
370 croak("Wide character in Socket::pack_sockaddr_in");
371 ip_address = SvPV(ip_address_sv, addrlen);
372 if (addrlen == sizeof(addr) || addrlen == 4)
374 (ip_address[0] & 0xFF) << 24 |
375 (ip_address[1] & 0xFF) << 16 |
376 (ip_address[2] & 0xFF) << 8 |
377 (ip_address[3] & 0xFF);
379 croak("Bad arg length for %s, length is %d, should be %d",
380 "Socket::pack_sockaddr_in",
381 addrlen, sizeof(addr));
382 Zero( &sin, sizeof sin, char );
383 sin.sin_family = AF_INET;
384 sin.sin_port = htons(port);
385 sin.sin_addr.s_addr = htonl(addr.s_addr);
386 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
390 unpack_sockaddr_in(sin_sv)
395 struct sockaddr_in addr;
397 struct in_addr ip_address;
398 char * sin = SvPV(sin_sv,sockaddrlen);
399 if (sockaddrlen != sizeof(addr)) {
400 croak("Bad arg length for %s, length is %d, should be %d",
401 "Socket::unpack_sockaddr_in",
402 sockaddrlen, sizeof(addr));
404 Copy( sin, &addr,sizeof addr, char );
405 if ( addr.sin_family != AF_INET ) {
406 croak("Bad address family for %s, got %d, should be %d",
407 "Socket::unpack_sockaddr_in",
411 port = ntohs(addr.sin_port);
412 ip_address = addr.sin_addr;
415 PUSHs(sv_2mortal(newSViv((IV) port)));
416 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address, sizeof ip_address)));