1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "config.h"
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <netinet/in.h>
#endif
#include <sys/socket.h>
#ifdef PerlIO
typedef PerlIO * InputStream;
#else
#define PERLIO_IS_STDIO 1
typedef FILE * InputStream;
#define PerlIO_fileno(f) fileno(f)
#endif
static int
not_here(char *s)
{
croak("%s not implemented on this architecture", s);
return -1;
}
/* Recent versions of Win32 platforms are confused about these constants due to
problems in the order of socket header file importation
#ifdef WIN32
#if (PERL_REVISION >=5) && (PERL_VERSION >= 8) && (PERL_SUBVERSION >= 6)
#undef IP_OPTIONS
#undef IP_HDRINCL
#undef IP_TOS
#undef IP_TTL
#undef IP_MULTICAST_IF
#undef IP_MULTICAST_TTL
#undef IP_MULTICAST_LOOP
#undef IP_ADD_MEMBERSHIP
#undef IP_DROP_MEMBERSHIP
#undef IP_DONTFRAGMENT
#define IP_OPTIONS 1
#define IP_HDRINCL 2
#define IP_TOS 3
#define IP_TTL 4
#define IP_MULTICAST_IF 9
#define IP_MULTICAST_TTL 10
#define IP_MULTICAST_LOOP 11
#define IP_ADD_MEMBERSHIP 12
#define IP_DROP_MEMBERSHIP 13
#define IP_DONTFRAGMENT 14
#endif
#endif
*/
#ifndef HAS_INET_ATON
static int
my_inet_aton(register const char *cp, struct in_addr *addr)
{
dTHX;
register U32 val;
register int base;
register char c;
int nparts;
const char *s;
unsigned int parts[4];
register unsigned int *pp = parts;
if (!cp || !*cp)
return 0;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, other=decimal.
*/
val = 0; base = 10;
if (*cp == '0') {
if (*++cp == 'x' || *cp == 'X')
base = 16, cp++;
else
base = 8;
}
while ((c = *cp) != '\0') {
if (isDIGIT(c)) {
val = (val * base) + (c - '0');
cp++;
continue;
}
if (base == 16 && (s=strchr(PL_hexdigit,c))) {
val = (val << 4) +
((s - PL_hexdigit) & 15);
cp++;
continue;
}
break;
}
if (*cp == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16-bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3 || val > 0xff)
return 0;
*pp++ = val, cp++;
} else
break;
}
/*
* Check for trailing characters.
*/
if (*cp && !isSPACE(*cp))
return 0;
/*
* Concoct the address according to
* the number of parts specified.
*/
nparts = pp - parts + 1; /* force to an int for switch() */
switch (nparts) {
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return 0;
val |= parts[0] << 24;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return 0;
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return 0;
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
addr->s_addr = htonl(val);
return 1;
}
#undef inet_aton
#define inet_aton my_inet_aton
#endif
MODULE = IO::Socket::Multicast PACKAGE = IO::Socket::Multicast
void
_mcast_add(sock,mcast_group,interface_addr="")
InputStream sock
char* mcast_group
char* interface_addr
PROTOTYPE: $$;$
PREINIT:
int fd;
struct ip_mreq mreq;
PPCODE:
{
fd = PerlIO_fileno(sock);
if (!inet_aton(mcast_group,&mreq.imr_multiaddr))
croak("Invalid address used for mcast group");
if ((strlen(interface_addr) > 0)) {
if (!inet_aton(interface_addr,&mreq.imr_interface))
croak("Invalid address used for local interface");
} else {
mreq.imr_interface.s_addr = INADDR_ANY;
}
if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(void*) &mreq,sizeof(mreq)) < 0)
XSRETURN_EMPTY;
else
XSRETURN_YES;
}
void
_mcast_drop(sock,mcast_group,interface_addr="")
InputStream sock
char* mcast_group
char* interface_addr
PROTOTYPE: $$;$
PREINIT:
int fd;
struct ip_mreq mreq;
PPCODE:
{
fd = PerlIO_fileno(sock);
if (!inet_aton(mcast_group,&mreq.imr_multiaddr))
croak("Invalid address used for mcast group");
if ((strlen(interface_addr) > 0)) {
if (!inet_aton(interface_addr,&mreq.imr_interface))
croak("Invalid address used for local interface");
} else {
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
}
if (setsockopt(fd,IPPROTO_IP,IP_DROP_MEMBERSHIP,(void*)&mreq,sizeof(mreq)) < 0)
XSRETURN_EMPTY;
else
XSRETURN_YES;
}
int
mcast_loopback(sock,...)
InputStream sock
PROTOTYPE: $;$
PREINIT:
int fd;
int len;
char previous,loopback;
CODE:
{
fd = PerlIO_fileno(sock);
/* get previous value of flag */
len = sizeof(previous);
if (getsockopt(fd,IPPROTO_IP,IP_MULTICAST_LOOP,(void*)&previous,&len) < 0)
XSRETURN_UNDEF;
if (items > 1) { /* set value */
loopback = SvIV(ST(1));
if (setsockopt(fd,IPPROTO_IP,IP_MULTICAST_LOOP,(void*)&loopback,sizeof(loopback)) < 0)
XSRETURN_UNDEF;
}
RETVAL = previous;
}
OUTPUT:
RETVAL
int
mcast_ttl(sock,...)
InputStream sock
PROTOTYPE: $;$
PREINIT:
int fd;
int len;
char previous,ttl;
CODE:
{
fd = PerlIO_fileno(sock);
/* get previous value of flag */
len = sizeof(previous);
if (getsockopt(fd,IPPROTO_IP,IP_MULTICAST_TTL,(void*)&previous,&len) < 0)
XSRETURN_UNDEF;
if (items > 1) { /* set value */
ttl = SvIV(ST(1));
if (setsockopt(fd,IPPROTO_IP,IP_MULTICAST_TTL,(void*)&ttl,sizeof(ttl)) < 0)
XSRETURN_UNDEF;
}
RETVAL = previous;
}
OUTPUT:
RETVAL
void
_mcast_if(sock,...)
InputStream sock
PROTOTYPE: $;$
PREINIT:
int fd,len;
STRLEN slen;
char* addr;
struct in_addr ifaddr;
struct ip_mreq mreq;
PPCODE:
{
fd = PerlIO_fileno(sock);
if (items > 1) { /* setting interface */
addr = SvPV(ST(1),slen);
if (inet_aton(addr,&ifaddr) == 0 )
XSRETURN_EMPTY;
if (setsockopt(fd,IPPROTO_IP,IP_MULTICAST_IF,(void*)&ifaddr,sizeof(ifaddr)) == 0)
XSRETURN_YES;
else
XSRETURN_NO;
} else { /* getting interface address */
/* freakin' bug in Linux -- IP_MULTICAST_IF returns a struct mreqn rather than
an in_addr (contrary to Stevens and the setsockopt()!
We work around this by looking at size of returned thing and doing a
ugly cast */
len = sizeof(mreq);
if (getsockopt(fd,IPPROTO_IP,IP_MULTICAST_IF,(void*) &mreq,&len) != 0)
XSRETURN_EMPTY;
if (len == sizeof(mreq)) {
XPUSHs(sv_2mortal(newSVpv(inet_ntoa(mreq.imr_interface),0)));
} else if (len == sizeof (struct in_addr)) {
XPUSHs(sv_2mortal(newSVpv(inet_ntoa(*(struct in_addr*)&mreq),0)));
} else {
croak("getsockopt() returned a data type I don't understand");
}
}
}
|