chiark / gitweb /
net-util: don't use libudev
[elogind.git] / src / shared / net-util.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 "net-util.h"
26 #include "log.h"
27 #include "utf8.h"
28 #include "util.h"
29 #include "conf-parser.h"
30
31 bool net_match_config(const struct ether_addr *match_mac,
32                       const char *match_path,
33                       const char *match_driver,
34                       const char *match_type,
35                       const char *match_name,
36                       const char *dev_mac,
37                       const char *dev_path,
38                       const char *dev_driver,
39                       const char *dev_type,
40                       const char *dev_name) {
41
42         if (match_mac) {
43                 if (!dev_mac || memcmp(match_mac, ether_aton(dev_mac), ETH_ALEN)) {
44                         log_debug("Interface MAC address (%s) did not match MACAddress=%s",
45                                   dev_mac, ether_ntoa(match_mac));
46                         return 0;
47                 }
48         }
49
50         if (match_path) {
51                 if (!streq_ptr(match_path, dev_path)) {
52                         log_debug("Interface persistent path (%s) did not match Path=%s",
53                                   dev_path, match_path);
54                         return 0;
55                 }
56         }
57
58         if (match_driver) {
59                 if (!streq_ptr(match_driver, dev_driver)) {
60                         log_debug("Interface device driver (%s) did not match Driver=%s",
61                                   dev_driver, match_driver);
62                         return 0;
63                 }
64         }
65
66         if (match_type) {
67                 if (!streq_ptr(match_type, dev_type)) {
68                         log_debug("Interface type (%s) did not match Type=%s",
69                                   dev_type, match_type);
70                         return 0;
71                 }
72         }
73
74         if (match_name) {
75                 if (!streq_ptr(match_name, dev_name)) {
76                         log_debug("Interface name (%s) did not match Name=%s",
77                                   dev_name, match_name);
78                         return 0;
79                 }
80         }
81
82         return 1;
83 }
84
85 int config_parse_ifname(const char *unit,
86                         const char *filename,
87                         unsigned line,
88                         const char *section,
89                         const char *lvalue,
90                         int ltype,
91                         const char *rvalue,
92                         void *data,
93                         void *userdata) {
94
95         char **s = data;
96         char *n;
97
98         assert(filename);
99         assert(lvalue);
100         assert(rvalue);
101         assert(data);
102
103         n = strdup(rvalue);
104         if (!n)
105                 return log_oom();
106
107         if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
108                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
109                            "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
110                 free(n);
111                 return 0;
112         }
113
114         free(*s);
115         if (*n)
116                 *s = n;
117         else {
118                 free(n);
119                 *s = NULL;
120         }
121
122         return 0;
123 }
124
125 int config_parse_hwaddr(const char *unit,
126                         const char *filename,
127                         unsigned line,
128                         const char *section,
129                         const char *lvalue,
130                         int ltype,
131                         const char *rvalue,
132                         void *data,
133                         void *userdata) {
134         struct ether_addr **hwaddr = data;
135         struct ether_addr *n;
136         int r;
137
138         assert(filename);
139         assert(lvalue);
140         assert(rvalue);
141         assert(data);
142
143         n = new0(struct ether_addr, 1);
144         if (!n)
145                 return log_oom();
146
147         r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
148                    &n->ether_addr_octet[0],
149                    &n->ether_addr_octet[1],
150                    &n->ether_addr_octet[2],
151                    &n->ether_addr_octet[3],
152                    &n->ether_addr_octet[4],
153                    &n->ether_addr_octet[5]);
154         if (r != 6) {
155                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
156                            "Not a valid MAC address, ignoring assignment: %s", rvalue);
157                 free(n);
158                 return 0;
159         }
160
161         free(*hwaddr);
162         *hwaddr = n;
163
164         return 0;
165 }