Imported Upstream version 3.2.0
[debian/amanda] / perl / amglue / directtcp.swg
1 /*
2  * Copyright (c) 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 /*
22  * SWIG typemaps for DirectTCP-related stuff.
23  */
24
25 %typemap(out) DirectTCPAddr * {
26     /* we assume this is an *array* of addresses, and return an arrayref or, if
27      * the result is NULL, undef. */
28     DirectTCPAddr *iter = $1;
29     AV *av;
30     int i;
31
32     i = 0;
33     av = newAV();
34     while (iter && iter->ipv4) {
35         struct in_addr in;
36         char *addr;
37         AV *tuple;
38
39         in.s_addr = htonl(iter->ipv4);
40         addr = inet_ntoa(in);
41
42         tuple = newAV();
43         g_assert(NULL != av_store(tuple, 0,
44                 newSVpv(addr, 0)));
45         g_assert(NULL != av_store(tuple, 1, newSViv(iter->port)));
46         g_assert(NULL != av_store(av, i++, newRV_noinc((SV *)tuple)));
47         iter++;
48     }
49
50     $result = newRV_noinc((SV *)av);
51     argvi++;
52 }
53
54 %typemap(in,numinputs=0) DirectTCPAddr **addrs
55     (DirectTCPAddr *addrs) {
56     addrs = NULL;
57     $1 = &addrs;
58 }
59 %typemap(argout) DirectTCPAddr **addrs {
60     if ($1 && *$1) {
61         DirectTCPAddr *iter = *$1;
62         AV *av = newAV();
63         int i = 0;
64
65         while (iter && iter->ipv4) {
66             struct in_addr in;
67             char *addr;
68             AV *tuple = newAV();
69
70             in.s_addr = htonl(iter->ipv4);
71             addr = inet_ntoa(in);
72             g_assert(NULL != av_store(tuple, 0,
73                     newSVpv(addr, 0)));
74             g_assert(NULL != av_store(tuple, 1, newSViv(iter->port)));
75             g_assert(NULL != av_store(av, i++, newRV_noinc((SV *)tuple)));
76             iter++;
77         }
78
79         $result = newRV_noinc((SV *)av);
80         argvi++;
81     }
82 }
83
84 %typemap(in) DirectTCPAddr *addrs {
85     AV *addrs_av;
86     int num_addrs, i;
87
88     if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
89         SWIG_exception_fail(SWIG_TypeError, "must provide an arrayref of DirectTCPAddrs");
90     }
91     addrs_av = (AV *)SvRV($input);
92     num_addrs = av_len(addrs_av)+1;
93
94     $1 = g_new0(DirectTCPAddr, num_addrs+1);
95
96     for (i = 0; i < num_addrs; i++) {
97         SV **svp = av_fetch(addrs_av, i, 0);
98         AV *addr_av;
99         struct in_addr addr;
100         IV port;
101
102         if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV
103                  || av_len((AV *)SvRV(*svp))+1 != 2) {
104             SWIG_exception_fail(SWIG_TypeError, "each DirectTCPAddr must be a 2-element arrayref");
105         }
106
107         addr_av = (AV *)SvRV(*svp);
108
109         /* get address */
110         svp = av_fetch(addr_av, 0, 0);
111         if (!svp || !SvPOK(*svp) || !inet_aton(SvPV_nolen(*svp), &addr)) {
112             SWIG_exception_fail(SWIG_TypeError, "invalid IPv4 addr in address");
113         }
114         $1[i].ipv4 = ntohl(addr.s_addr);
115
116         /* get port */
117         svp = av_fetch(addr_av, 1, 0);
118         if (!svp || !SvIOK(*svp) || (port = SvIV(*svp)) <= 0 || port >= 65536) {
119             SWIG_exception_fail(SWIG_TypeError, "invalid port in address");
120         }
121         $1[i].port = (guint16)port;
122     }
123 }