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 | |
---|
20 | static int hex16(char *s, uint16_t *val) |
---|
21 | { |
---|
22 | int err = -EINVAL; |
---|
23 | uint16_t v = 0; |
---|
24 | |
---|
25 | for( ; *s; s++){ |
---|
26 | v <<= 4; |
---|
27 | if('0' <= *s && *s <= '9'){ |
---|
28 | v |= *s - '0'; |
---|
29 | } else if('A' <= *s && *s <= 'F'){ |
---|
30 | v |= *s - 'A' + 10; |
---|
31 | } else if('a' <= *s && *s <= 'f'){ |
---|
32 | v |= *s - 'a' + 10; |
---|
33 | } else { |
---|
34 | goto exit; |
---|
35 | } |
---|
36 | } |
---|
37 | err = 0; |
---|
38 | exit: |
---|
39 | *val = (err ? 0 : v); |
---|
40 | return err; |
---|
41 | } |
---|
42 | |
---|
43 | int VnetId_aton(const char *s, VnetId *vnet){ |
---|
44 | int err = -EINVAL; |
---|
45 | const char *p, *q; |
---|
46 | uint16_t v; |
---|
47 | char buf[5]; |
---|
48 | int buf_n = sizeof(buf) - 1; |
---|
49 | int i, n; |
---|
50 | const int elts_n = VNETID_SIZE16; |
---|
51 | |
---|
52 | q = s; |
---|
53 | p = strchr(q, ':'); |
---|
54 | i = (p ? 0 : elts_n - 1); |
---|
55 | do { |
---|
56 | if(!p){ |
---|
57 | if(i < elts_n - 1) goto exit; |
---|
58 | p = s + strlen(s); |
---|
59 | } |
---|
60 | n = p - q; |
---|
61 | if(n > buf_n) goto exit; |
---|
62 | memcpy(buf, q, n); |
---|
63 | buf[n] = '\0'; |
---|
64 | err = hex16(buf, &v); |
---|
65 | if(err) goto exit; |
---|
66 | vnet->u.vnet16[i] = htons(v); |
---|
67 | q = p+1; |
---|
68 | p = strchr(q, ':'); |
---|
69 | i++; |
---|
70 | } while(i < elts_n); |
---|
71 | err = 0; |
---|
72 | exit: |
---|
73 | if(err){ |
---|
74 | *vnet = (VnetId){}; |
---|
75 | } |
---|
76 | return err; |
---|
77 | } |
---|