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