1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 2008-2012 Patrice <GomoR> Auffret
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 SV *
route_c2sv(RouteEntry * entry)32 route_c2sv(RouteEntry *entry)
33 {
34    HV *out     = newHV();
35    SV *out_ref = newRV_noinc((SV *)out);
36    char *dst, *gw;
37    if (entry != NULL) {
38       dst = addr_ntoa(&(entry->route_dst));
39       dst == NULL ? hv_store(out, "route_dst", 9, &PL_sv_undef, 0)
40                   : hv_store(out, "route_dst", 9, newSVpv(dst, 0), 0);
41       gw = addr_ntoa(&(entry->route_gw));
42       gw == NULL ? hv_store(out, "route_gw", 8, &PL_sv_undef, 0)
43                  : hv_store(out, "route_gw", 8, newSVpv(gw, 0), 0);
44    }
45    return out_ref;
46 }
47 
48 static RouteEntry *
route_sv2c(SV * h,RouteEntry * ref)49 route_sv2c(SV *h, RouteEntry *ref)
50 {
51    if (ref && h && SvROK(h)) {
52       HV *hv = (HV *)SvRV(h);
53       memset(ref, 0, sizeof(RouteEntry));
54       if (hv_exists(hv, "route_dst", 9)) {
55          SV **r = hv_fetch(hv, "route_dst", 9, 0);
56          if (SvOK(*r)) {
57             struct addr a;
58             if (addr_aton(SvPV(*r, PL_na), &a) == 0) {
59                memcpy(&(ref->route_dst), &a, sizeof(struct addr));
60             }
61          }
62       }
63       if (hv_exists(hv, "route_gw", 8)) {
64          SV **r = hv_fetch(hv, "route_gw", 8, 0);
65          if (SvOK(*r)) {
66             struct addr a;
67             if (addr_aton(SvPV(*r, PL_na), &a) == 0) {
68                memcpy(&(ref->route_gw), &a, sizeof(struct addr));
69             }
70          }
71       }
72    }
73    else {
74       ref = NULL;
75    }
76    return ref;
77 }
78