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