]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/ipx_pton.c
Tree wide: Drop sockaddr_nl arg
[mirror_iproute2.git] / lib / ipx_pton.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <errno.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7
8 #include "utils.h"
9
10 static int ipx_getnet(u_int32_t *net, const char *str)
11 {
12 int i;
13 u_int32_t tmp;
14
15 for(i = 0; *str && (i < 8); i++) {
16
17 if ((tmp = get_hex(*str)) == -1) {
18 if (*str == '.')
19 return 0;
20 else
21 return -1;
22 }
23
24 str++;
25 (*net) <<= 4;
26 (*net) |= tmp;
27 }
28
29 if (*str == 0)
30 return 0;
31
32 return -1;
33 }
34
35 static int ipx_getnode(u_int8_t *node, const char *str)
36 {
37 int i;
38 u_int32_t tmp;
39
40 for(i = 0; i < 6; i++) {
41 if ((tmp = get_hex(*str++)) == -1)
42 return -1;
43 node[i] = (u_int8_t)tmp;
44 node[i] <<= 4;
45 if ((tmp = get_hex(*str++)) == -1)
46 return -1;
47 node[i] |= (u_int8_t)tmp;
48 if (*str == ':')
49 str++;
50 }
51
52 return 0;
53 }
54
55 static int ipx_pton1(const char *src, struct ipx_addr *addr)
56 {
57 char *sep = (char *)src;
58 int no_node = 0;
59
60 memset(addr, 0, sizeof(struct ipx_addr));
61
62 while(*sep && (*sep != '.'))
63 sep++;
64
65 if (*sep != '.')
66 no_node = 1;
67
68 if (ipx_getnet(&addr->ipx_net, src))
69 return 0;
70
71 addr->ipx_net = htonl(addr->ipx_net);
72
73 if (no_node)
74 return 1;
75
76 if (ipx_getnode(addr->ipx_node, sep + 1))
77 return 0;
78
79 return 1;
80 }
81
82 int ipx_pton(int af, const char *src, void *addr)
83 {
84 int err;
85
86 switch (af) {
87 case AF_IPX:
88 errno = 0;
89 err = ipx_pton1(src, (struct ipx_addr *)addr);
90 break;
91 default:
92 errno = EAFNOSUPPORT;
93 err = -1;
94 }
95
96 return err;
97 }