[34] | 1 | /* |
---|
| 2 | * Copyright (C) 2005 Mike Wray <mike.wray@hp.com> |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by the |
---|
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
---|
| 7 | * option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, but |
---|
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
| 11 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 12 | * for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License along |
---|
| 15 | * with this program; if not, write to the Free software Foundation, Inc., |
---|
| 16 | * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | */ |
---|
| 19 | #include "sys_net.h" |
---|
| 20 | #include "if_varp.h" |
---|
| 21 | #include "varp_util.h" |
---|
| 22 | #include "sxpr_util.h" |
---|
| 23 | |
---|
| 24 | int stringof(Sxpr exp, char **s){ |
---|
| 25 | int err = 0; |
---|
| 26 | if(ATOMP(exp)){ |
---|
| 27 | *s = atom_name(exp); |
---|
| 28 | } else if(STRINGP(exp)){ |
---|
| 29 | *s = string_string(exp); |
---|
| 30 | } else { |
---|
| 31 | err = -EINVAL; |
---|
| 32 | *s = NULL; |
---|
| 33 | } |
---|
| 34 | return err; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | int child_string(Sxpr exp, Sxpr key, char **s){ |
---|
| 38 | int err = 0; |
---|
| 39 | Sxpr val = sxpr_child_value(exp, key, ONONE); |
---|
| 40 | err = stringof(val, s); |
---|
| 41 | return err; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | int intof(Sxpr exp, int *v){ |
---|
| 45 | int err = 0; |
---|
| 46 | char *s; |
---|
| 47 | unsigned long l; |
---|
| 48 | if(INTP(exp)){ |
---|
| 49 | *v = OBJ_INT(exp); |
---|
| 50 | } else { |
---|
| 51 | err = stringof(exp, &s); |
---|
| 52 | if(err) goto exit; |
---|
| 53 | err = convert_atoul(s, &l); |
---|
| 54 | *v = (int)l; |
---|
| 55 | } |
---|
| 56 | exit: |
---|
| 57 | return err; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | int child_int(Sxpr exp, Sxpr key, int *v){ |
---|
| 61 | int err = 0; |
---|
| 62 | Sxpr val = sxpr_child_value(exp, key, ONONE); |
---|
| 63 | err = intof(val, v); |
---|
| 64 | return err; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | int vnetof(Sxpr exp, VnetId *v){ |
---|
| 68 | int err = 0; |
---|
| 69 | char *s; |
---|
| 70 | err = stringof(exp, &s); |
---|
| 71 | if(err) goto exit; |
---|
| 72 | err = VnetId_aton(s, v); |
---|
| 73 | exit: |
---|
| 74 | return err; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | int child_vnet(Sxpr exp, Sxpr key, VnetId *v){ |
---|
| 78 | int err = 0; |
---|
| 79 | Sxpr val = sxpr_child_value(exp, key, ONONE); |
---|
| 80 | err = vnetof(val, v); |
---|
| 81 | return err; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int macof(Sxpr exp, unsigned char *v){ |
---|
| 85 | int err = 0; |
---|
| 86 | char *s; |
---|
| 87 | err = stringof(exp, &s); |
---|
| 88 | if(err) goto exit; |
---|
| 89 | err = mac_aton(s, v); |
---|
| 90 | exit: |
---|
| 91 | return err; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | int child_mac(Sxpr exp, Sxpr key, unsigned char *v){ |
---|
| 95 | int err = 0; |
---|
| 96 | Sxpr val = sxpr_child_value(exp, key, ONONE); |
---|
| 97 | err = macof(val, v); |
---|
| 98 | return err; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | int addrof(Sxpr exp, uint32_t *v){ |
---|
| 102 | int err = 0; |
---|
| 103 | char *s; |
---|
| 104 | unsigned long w; |
---|
| 105 | err = stringof(exp, &s); |
---|
| 106 | if(err) goto exit; |
---|
| 107 | err = get_inet_addr(s, &w); |
---|
| 108 | if(err) goto exit; |
---|
| 109 | *v = (uint32_t)w; |
---|
| 110 | exit: |
---|
| 111 | return err; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | int child_addr(Sxpr exp, Sxpr key, uint32_t *v){ |
---|
| 115 | int err = 0; |
---|
| 116 | Sxpr val = sxpr_child_value(exp, key, ONONE); |
---|
| 117 | err = addrof(val, v); |
---|
| 118 | return err; |
---|
| 119 | } |
---|