Commit | Line | Data |
c5be433b |
1 | #define PERL_NO_GET_CONTEXT |
a0d0e21e |
2 | #include "EXTERN.h" |
3 | #include "perl.h" |
4 | #include "XSUB.h" |
5 | |
8e07c86e |
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 |
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 |
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 |
26 | # ifdef I_NETINET_IN |
27 | # include <netinet/in.h> |
28 | # endif |
86959918 |
29 | # ifdef I_NETDB |
30 | # include <netdb.h> |
31 | # endif |
9cc6feab |
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 |
42 | #ifdef NETWARE |
43 | NETDB_DEFINE_CONTEXT |
44 | NETINET_DEFINE_CONTEXT |
45 | #endif |
46 | |
6b1016b5 |
47 | #ifdef I_SYSUIO |
48 | # include <sys/uio.h> |
49 | #endif |
50 | |
a0d0e21e |
51 | #ifndef AF_NBS |
9cc6feab |
52 | # undef PF_NBS |
a0d0e21e |
53 | #endif |
54 | |
55 | #ifndef AF_X25 |
9cc6feab |
56 | # undef PF_X25 |
a0d0e21e |
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 |
67 | #endif /* INADDR_LOOPBACK */ |
68 | |
7e1af8bc |
69 | #ifndef HAS_INET_ATON |
70 | |
a062834f |
71 | /* |
7e1af8bc |
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 | */ |
78 | static int |
f0f333f4 |
79 | my_inet_aton(register const char *cp, struct in_addr *addr) |
7e1af8bc |
80 | { |
c5be433b |
81 | dTHX; |
0caed002 |
82 | register U32 val; |
7e1af8bc |
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 |
90 | if (!cp) |
91 | return 0; |
7e1af8bc |
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 |
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 |
175 | static int |
f0f333f4 |
176 | not_here(char *s) |
a0d0e21e |
177 | { |
178 | croak("Socket::%s not implemented on this architecture", s); |
179 | return -1; |
180 | } |
181 | |
2528d3bc |
182 | #define PERL_IN_ADDR_S_ADDR_SIZE 4 |
183 | |
184 | /* |
185 | * Bad assumptions possible here. |
186 | * |
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. |
193 | * |
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.) |
200 | * |
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 |
203 | * an in_addr. |
204 | * |
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. |
209 | * |
210 | * --jhi */ |
211 | |
ee96af8f |
212 | #include "constants.c" |
8e07c86e |
213 | |
a0d0e21e |
214 | MODULE = Socket PACKAGE = Socket |
215 | |
ee96af8f |
216 | INCLUDE: constants.xs |
8e07c86e |
217 | |
218 | void |
219 | inet_aton(host) |
220 | char * host |
221 | CODE: |
222 | { |
fbf77aee |
223 | struct in_addr ip_address; |
8e07c86e |
224 | struct hostent * phe; |
fbf77aee |
225 | int ok = inet_aton(host, &ip_address); |
8e07c86e |
226 | |
fbf77aee |
227 | if (!ok && (phe = gethostbyname(host))) { |
228 | Copy( phe->h_addr, &ip_address, phe->h_length, char ); |
7e1af8bc |
229 | ok = 1; |
8e07c86e |
230 | } |
231 | |
232 | ST(0) = sv_newmortal(); |
2528d3bc |
233 | if (ok) |
fbf77aee |
234 | sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address ); |
8e07c86e |
235 | } |
8e07c86e |
236 | |
237 | void |
238 | inet_ntoa(ip_address_sv) |
239 | SV * ip_address_sv |
240 | CODE: |
241 | { |
242 | STRLEN addrlen; |
243 | struct in_addr addr; |
244 | char * addr_str; |
1d68b939 |
245 | char * ip_address; |
246 | if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) |
b2d93eb8 |
247 | croak("Wide character in Socket::inet_ntoa"); |
2528d3bc |
248 | ip_address = SvPV(ip_address_sv, addrlen); |
249 | if (addrlen == sizeof(addr) || addrlen == 4) |
250 | addr.s_addr = |
251 | (ip_address[0] & 0xFF) << 24 | |
252 | (ip_address[1] & 0xFF) << 16 | |
253 | (ip_address[2] & 0xFF) << 8 | |
254 | (ip_address[3] & 0xFF); |
255 | else |
256 | croak("Bad arg length for %s, length is %d, should be %d", |
257 | "Socket::inet_ntoa", |
258 | addrlen, sizeof(addr)); |
259 | /* We could use inet_ntoa() but that is broken |
260 | * in HP-UX + GCC + 64bitint (returns "0.0.0.0"), |
261 | * so let's use this sprintf() workaround everywhere. */ |
1d68b939 |
262 | New(1138, addr_str, 4 * 3 + 3 + 1, char); |
263 | sprintf(addr_str, "%d.%d.%d.%d", |
264 | ((addr.s_addr >> 24) & 0xFF), |
265 | ((addr.s_addr >> 16) & 0xFF), |
266 | ((addr.s_addr >> 8) & 0xFF), |
267 | ( addr.s_addr & 0xFF)); |
268 | ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str))); |
269 | Safefree(addr_str); |
8e07c86e |
270 | } |
271 | |
272 | void |
4633a7c4 |
273 | pack_sockaddr_un(pathname) |
274 | char * pathname |
275 | CODE: |
276 | { |
25f94b33 |
277 | #ifdef I_SYS_UN |
4633a7c4 |
278 | struct sockaddr_un sun_ad; /* fear using sun */ |
fcdb74fc |
279 | STRLEN len; |
202975e6 |
280 | |
4633a7c4 |
281 | Zero( &sun_ad, sizeof sun_ad, char ); |
282 | sun_ad.sun_family = AF_UNIX; |
20408e3c |
283 | len = strlen(pathname); |
284 | if (len > sizeof(sun_ad.sun_path)) |
285 | len = sizeof(sun_ad.sun_path); |
202975e6 |
286 | # ifdef OS2 /* Name should start with \socket\ and contain backslashes! */ |
287 | { |
288 | int off; |
289 | char *s, *e; |
290 | |
291 | if (pathname[0] != '/' && pathname[0] != '\\') |
292 | croak("Relative UNIX domain socket name '%s' unsupported", pathname); |
a062834f |
293 | else if (len < 8 |
202975e6 |
294 | || pathname[7] != '/' && pathname[7] != '\\' |
295 | || !strnicmp(pathname + 1, "socket", 6)) |
296 | off = 7; |
297 | else |
298 | off = 0; /* Preserve names starting with \socket\ */ |
299 | Copy( "\\socket", sun_ad.sun_path, off, char); |
300 | Copy( pathname, sun_ad.sun_path + off, len, char ); |
301 | |
302 | s = sun_ad.sun_path + off - 1; |
303 | e = s + len + 1; |
304 | while (++s < e) |
305 | if (*s = '/') |
306 | *s = '\\'; |
307 | } |
a062834f |
308 | # else /* !( defined OS2 ) */ |
20408e3c |
309 | Copy( pathname, sun_ad.sun_path, len, char ); |
202975e6 |
310 | # endif |
a062834f |
311 | if (0) not_here("dummy"); |
79cb57f6 |
312 | ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad)); |
25f94b33 |
313 | #else |
314 | ST(0) = (SV *) not_here("pack_sockaddr_un"); |
315 | #endif |
316 | |
4633a7c4 |
317 | } |
318 | |
319 | void |
320 | unpack_sockaddr_un(sun_sv) |
321 | SV * sun_sv |
f13e1b2f |
322 | CODE: |
4633a7c4 |
323 | { |
25f94b33 |
324 | #ifdef I_SYS_UN |
4633a7c4 |
325 | struct sockaddr_un addr; |
fcdb74fc |
326 | STRLEN sockaddrlen; |
327 | char * sun_ad = SvPV(sun_sv,sockaddrlen); |
328 | char * e; |
36902e12 |
329 | # ifndef __linux__ |
330 | /* On Linux sockaddrlen on sockets returned by accept, recvfrom, |
331 | getpeername and getsockname is not equal to sizeof(addr). */ |
4633a7c4 |
332 | if (sockaddrlen != sizeof(addr)) { |
333 | croak("Bad arg length for %s, length is %d, should be %d", |
334 | "Socket::unpack_sockaddr_un", |
335 | sockaddrlen, sizeof(addr)); |
336 | } |
36902e12 |
337 | # endif |
4633a7c4 |
338 | |
339 | Copy( sun_ad, &addr, sizeof addr, char ); |
340 | |
341 | if ( addr.sun_family != AF_UNIX ) { |
342 | croak("Bad address family for %s, got %d, should be %d", |
343 | "Socket::unpack_sockaddr_un", |
344 | addr.sun_family, |
345 | AF_UNIX); |
fcdb74fc |
346 | } |
347 | e = addr.sun_path; |
348 | while (*e && e < addr.sun_path + sizeof addr.sun_path) |
349 | ++e; |
79cb57f6 |
350 | ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path)); |
25f94b33 |
351 | #else |
352 | ST(0) = (SV *) not_here("unpack_sockaddr_un"); |
353 | #endif |
4633a7c4 |
354 | } |
355 | |
356 | void |
2528d3bc |
357 | pack_sockaddr_in(port, ip_address_sv) |
2c129a17 |
358 | unsigned short port |
2528d3bc |
359 | SV * ip_address_sv |
8e07c86e |
360 | CODE: |
361 | { |
362 | struct sockaddr_in sin; |
2528d3bc |
363 | struct in_addr addr; |
364 | STRLEN addrlen; |
365 | char * ip_address; |
366 | if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) |
367 | croak("Wide character in Socket::pack_sockaddr_in"); |
368 | ip_address = SvPV(ip_address_sv, addrlen); |
369 | if (addrlen == sizeof(addr) || addrlen == 4) |
370 | addr.s_addr = |
371 | (ip_address[0] & 0xFF) << 24 | |
372 | (ip_address[1] & 0xFF) << 16 | |
373 | (ip_address[2] & 0xFF) << 8 | |
374 | (ip_address[3] & 0xFF); |
375 | else |
376 | croak("Bad arg length for %s, length is %d, should be %d", |
377 | "Socket::pack_sockaddr_in", |
378 | addrlen, sizeof(addr)); |
8e07c86e |
379 | Zero( &sin, sizeof sin, char ); |
4633a7c4 |
380 | sin.sin_family = AF_INET; |
8e07c86e |
381 | sin.sin_port = htons(port); |
2528d3bc |
382 | sin.sin_addr.s_addr = htonl(addr.s_addr); |
79cb57f6 |
383 | ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin)); |
8e07c86e |
384 | } |
385 | |
386 | void |
387 | unpack_sockaddr_in(sin_sv) |
388 | SV * sin_sv |
389 | PPCODE: |
390 | { |
391 | STRLEN sockaddrlen; |
392 | struct sockaddr_in addr; |
2c129a17 |
393 | unsigned short port; |
2528d3bc |
394 | struct in_addr ip_address; |
8e07c86e |
395 | char * sin = SvPV(sin_sv,sockaddrlen); |
396 | if (sockaddrlen != sizeof(addr)) { |
397 | croak("Bad arg length for %s, length is %d, should be %d", |
398 | "Socket::unpack_sockaddr_in", |
399 | sockaddrlen, sizeof(addr)); |
400 | } |
8e07c86e |
401 | Copy( sin, &addr,sizeof addr, char ); |
4633a7c4 |
402 | if ( addr.sin_family != AF_INET ) { |
403 | croak("Bad address family for %s, got %d, should be %d", |
404 | "Socket::unpack_sockaddr_in", |
405 | addr.sin_family, |
406 | AF_INET); |
a062834f |
407 | } |
8e07c86e |
408 | port = ntohs(addr.sin_port); |
409 | ip_address = addr.sin_addr; |
410 | |
924508f0 |
411 | EXTEND(SP, 2); |
2c129a17 |
412 | PUSHs(sv_2mortal(newSViv((IV) port))); |
2528d3bc |
413 | PUSHs(sv_2mortal(newSVpvn((char *)&ip_address, sizeof ip_address))); |
8e07c86e |
414 | } |