]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/dnet_pton.c
Import patch iproute2.119
[mirror_iproute2.git] / lib / dnet_pton.c
CommitLineData
aba5acdf
SH
1#include <errno.h>
2#include <sys/types.h>
3#include <netinet/in.h>
4
5#include "utils.h"
6
7static __inline__ u_int16_t dn_htons(u_int16_t addr)
8{
9 union {
10 u_int8_t byte[2];
11 u_int16_t word;
12 } u;
13
14 u.word = addr;
15 return ((u_int16_t)u.byte[0]) | (((u_int16_t)u.byte[1]) << 8);
16}
17
18
19static int dnet_num(const char *src, u_int16_t * dst)
20{
21 int rv = 0;
22 int tmp;
23 *dst = 0;
24
25 while ((tmp = *src++) != 0) {
26 tmp -= '0';
27 if ((tmp < 0) || (tmp > 9))
28 return rv;
29
30 rv++;
31 (*dst) *= 10;
32 (*dst) += tmp;
33 }
34
35 return rv;
36}
37
38static int dnet_pton1(const char *src, struct dn_naddr *dna)
39{
40 u_int16_t area = 0;
41 u_int16_t node = 0;
42 int pos;
43
44 pos = dnet_num(src, &area);
45 if ((pos == 0) || (area > 63) || (*(src + pos) != '.'))
46 return 0;
47 pos = dnet_num(src + pos + 1, &node);
48 if ((pos == 0) || (node > 1023))
49 return 0;
50 dna->a_len = 2;
51 *(u_int16_t *)dna->a_addr = dn_htons((area << 10) | node);
52
53 return 1;
54}
55
56int dnet_pton(int af, const char *src, void *addr)
57{
58 int err;
59
60 switch (af) {
61 case AF_DECnet:
62 errno = 0;
63 err = dnet_pton1(src, (struct dn_naddr *)addr);
64 break;
65 default:
66 errno = EAFNOSUPPORT;
67 err = -1;
68 }
69
70 return err;
71}