chiark / gitweb /
hostnamectl: H requires an argument, P is no longer used
[elogind.git] / src / udev / net / link-config-parse.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4  This file is part of systemd.
5
6  Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <netinet/ether.h>
23 #include <net/if.h>
24
25 #include "link-config.h"
26
27 #include "utf8.h"
28 #include "util.h"
29 #include "conf-parser.h"
30
31 static const char* const mac_policy_table[] = {
32         [MACPOLICY_PERSISTENT] = "persistent",
33         [MACPOLICY_RANDOM] = "random"
34 };
35
36 DEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
37 DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy, "Failed to parse MAC address policy");
38
39 static const char* const name_policy_table[] = {
40         [NAMEPOLICY_ONBOARD] = "onboard",
41         [NAMEPOLICY_SLOT] = "slot",
42         [NAMEPOLICY_PATH] = "path",
43         [NAMEPOLICY_MAC] = "mac"
44 };
45
46 DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
47 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy");
48
49 int config_parse_ifname(const char *unit,
50                         const char *filename,
51                         unsigned line,
52                         const char *section,
53                         const char *lvalue,
54                         int ltype,
55                         const char *rvalue,
56                         void *data,
57                         void *userdata) {
58
59         char **s = data;
60         char *n;
61
62         assert(filename);
63         assert(lvalue);
64         assert(rvalue);
65         assert(data);
66
67         n = strdup(rvalue);
68         if (!n)
69                 return log_oom();
70
71         if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
72                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
73                            "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
74                 free(n);
75                 return 0;
76         }
77
78         free(*s);
79         if (*n)
80                 *s = n;
81         else {
82                 free(n);
83                 *s = NULL;
84         }
85
86         return 0;
87 }
88
89 int config_parse_hwaddr(const char *unit,
90                         const char *filename,
91                         unsigned line,
92                         const char *section,
93                         const char *lvalue,
94                         int ltype,
95                         const char *rvalue,
96                         void *data,
97                         void *userdata) {
98         struct ether_addr **hwaddr = data;
99         struct ether_addr *n;
100         int r;
101
102         assert(filename);
103         assert(lvalue);
104         assert(rvalue);
105         assert(data);
106
107         n = new0(struct ether_addr, 1);
108         if (!n)
109                 return log_oom();
110
111         r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
112                    &n->ether_addr_octet[0],
113                    &n->ether_addr_octet[1],
114                    &n->ether_addr_octet[2],
115                    &n->ether_addr_octet[3],
116                    &n->ether_addr_octet[4],
117                    &n->ether_addr_octet[5]);
118         if (r != 6) {
119                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
120                            "Not a valid MAC address, ignoring assignment: %s", rvalue);
121                 free(n);
122                 return 0;
123         }
124
125         free(*hwaddr);
126         *hwaddr = n;
127
128         return 0;
129 }