dlz_perl_callback_clientinfo.xs revision 3b9ba6bd74e2cbbc0f821e5483ebf46fa8c4d8de
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Copyright (C) 2012 John Eaglesham
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Permission to use, copy, modify, and distribute this software for any
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * purpose with or without fee is hereby granted, provided that the above
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * copyright notice and this permission notice appear in all copies.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND JOHN EAGLESHAM
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * JOHN EAGLESHAM BE LIABLE FOR ANY SPECIAL, DIRECT,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#define ADDR_BUF_LEN INET6_ADDRSTRLEN
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include "EXTERN.h"
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include "perl.h"
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include "XSUB.h"
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include "dlz_perl_driver.h"
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dlz_minimal.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/* And some XS code. */
44a966dff66061ac3f266c6b451a70733eb78e82Mark AndrewsMODULE = DLZ_Perl::clientinfo PACKAGE = DLZ_Perl::clientinfo
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark AndrewsPROTOTYPES: DISABLE
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsvoid
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewssourceip(opaque)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews SV *opaque
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews PREINIT:
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const char *ret;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews char addr_buf[ADDR_BUF_LEN];
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews int port;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_sockaddr_t *src;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews dlz_perl_clientinfo_opaque *ci;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews I32 wantarray = GIMME_V;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews PPCODE:
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (!SvTRUE(opaque) || !SvIOK(opaque)) XSRETURN_EMPTY;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews /*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Safe, because Perl guarantees that an IV (the type we
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * pass into DLZ functions who pass it here) is able to
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * hold a pointer.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews ci = (dlz_perl_clientinfo_opaque *) SvIV(opaque);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (wantarray == G_VOID || ci->methods == NULL ||
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews ci->methods->version - ci->methods->age <
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews DNS_CLIENTINFOMETHODS_VERSION)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews XSRETURN_EMPTY;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews ci->methods->sourceip(ci->clientinfo, &src);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews switch (src->type.sa.sa_family) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews case AF_INET:
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews port = ntohs(src->type.sin.sin_port);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews ret = inet_ntop(AF_INET,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews &src->type.sin.sin_addr,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews addr_buf, ADDR_BUF_LEN);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews break;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews case AF_INET6:
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews port = ntohs(src->type.sin6.sin6_port);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews ret = inet_ntop(AF_INET6,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews &src->type.sin6.sin6_addr,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews addr_buf, ADDR_BUF_LEN);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews break;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews default:
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews ret = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (ret == NULL) XSRETURN_EMPTY;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews XPUSHs(sv_2mortal(newSVpv(addr_buf, strlen(addr_buf))));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (wantarray == G_ARRAY) XPUSHs(sv_2mortal(newSViv(port)));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews