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