]> git.ipfire.org Git - people/ms/network.git/blame - src/networkd/port.c
networkd: Perform port setup from configuration
[people/ms/network.git] / src / networkd / port.c
CommitLineData
abeab069
MT
1/*#############################################################################
2# #
3# IPFire.org - A linux based firewall #
4# Copyright (C) 2023 IPFire Network Development Team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
827435c8 21#include <limits.h>
abeab069
MT
22#include <net/if.h>
23#include <stdlib.h>
24
25#include "config.h"
827435c8 26#include "logging.h"
abeab069
MT
27#include "string.h"
28#include "port.h"
29
30struct nw_port {
31 int nrefs;
32
33 char name[IF_NAMESIZE];
827435c8 34 nw_port_type_t type;
abeab069
MT
35
36 // Configuration
37 struct nw_config *config;
38};
39
827435c8
MT
40static const struct nw_port_type_map {
41 nw_port_type_t type;
42 const char* name;
43} nw_port_type_map[] = {
44 { NW_PORT_DUMMY, "dummy" },
45 { NW_PORT_UNKNOWN, NULL },
46};
47
48static nw_port_type_t nw_port_type_from_string(const char* s) {
49 const struct nw_port_type_map* map = NULL;
50
51 for (map = nw_port_type_map; *map->name; map++) {
52 if (strcmp(map->name, s) == 0)
53 return map->type;
54 }
55
56 return NW_PORT_UNKNOWN;
57}
58
abeab069
MT
59static void nw_port_free(struct nw_port* port) {
60 if (port->config)
61 nw_config_unref(port->config);
62
63 free(port);
64}
65
827435c8
MT
66static int nw_port_setup_common(struct nw_port* port) {
67 return 0; // XXX TODO
68}
69
70static nw_port_type_t nw_port_setup_type(struct nw_port* port) {
71 const char* type = nw_config_get(port->config, "TYPE");
72 if (!type)
73 return NW_PORT_UNKNOWN;
74
75 return nw_port_type_from_string(type);
76}
77
78static int nw_port_setup(struct nw_port* port) {
79 char path[PATH_MAX];
80 int r;
81
82 // Compose the path to the main configuration file
83 r = nw_path_join(path, PORT_CONFIG_DIR, port->name);
84 if (r)
85 return r;
86
87 // Initialize the configuration
88 r = nw_config_create(&port->config, path);
89 if (r)
90 return r;
91
92 // Determine type
93 port->type = nw_port_setup_type(port);
94 if (!port->type) {
95 ERROR("Could not determine type of port %s\n", port->name);
96 return 0;
97 }
98
99 // Perform some common initialization
100 r = nw_port_setup_common(port);
101 if (r)
102 return r;
103
104 // Call any custom initialization
105 switch (port->type) {
106 // These do not need any special initialization
107 case NW_PORT_DUMMY:
108 case NW_PORT_UNKNOWN:
109 break;
110 }
111
112 return 0;
113}
114
abeab069
MT
115int nw_port_create(struct nw_port** port, const char* name) {
116 int r;
117
118 // Allocate a new object
119 struct nw_port* p = calloc(1, sizeof(*p));
120 if (!p)
121 return 1;
122
123 // Initialize reference counter
124 p->nrefs = 1;
125
126 // Store the name
127 r = nw_string_set(p->name, name);
128 if (r)
129 goto ERROR;
130
827435c8
MT
131 // Setup the port
132 r = nw_port_setup(p);
133 if (r)
134 goto ERROR;
135
abeab069
MT
136 *port = p;
137 return 0;
138
139ERROR:
140 nw_port_free(p);
141 return r;
142}
143
144struct nw_port* nw_port_ref(struct nw_port* port) {
145 port->nrefs++;
146
147 return port;
148}
149
150struct nw_port* nw_port_unref(struct nw_port* port) {
151 if (--port->nrefs > 0)
152 return port;
153
154 nw_port_free(port);
155 return NULL;
156}
157
158const char* nw_port_name(struct nw_port* port) {
159 return port->name;
160}