]>
git.ipfire.org Git - people/ms/network.git/blob - src/networkd/port.c
aff79a532a46bd35cdfdf16200298797539343ae
1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
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. #
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. #
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/>. #
19 #############################################################################*/
35 char name
[IF_NAMESIZE
];
39 struct nw_config
*config
;
45 static const struct nw_port_type_map
{
48 } nw_port_type_map
[] = {
49 { NW_PORT_DUMMY
, "dummy" },
50 { NW_PORT_UNKNOWN
, NULL
},
53 static nw_port_type_t
nw_port_type_from_string(const char* s
) {
54 const struct nw_port_type_map
* map
= NULL
;
56 for (map
= nw_port_type_map
; *map
->name
; map
++) {
57 if (strcmp(map
->name
, s
) == 0)
61 return NW_PORT_UNKNOWN
;
64 static void nw_port_free(struct nw_port
* port
) {
66 nw_config_unref(port
->config
);
71 static int nw_port_setup_address(struct nw_port
* port
) {
74 // Read ADDRESS from configuration
75 const char* s
= nw_config_get(port
->config
, "ADDRESS");
77 ERROR("Port %s: Address isn't set\n", port
->name
);
82 r
= nw_address_from_string(&port
->address
, s
);
84 ERROR("Port %s: Could not parse address: %m\n", port
->name
);
88 // XXX Do we need to check for multicast here?
93 static int nw_port_setup_common(struct nw_port
* port
) {
97 r
= nw_port_setup_address(port
);
104 static nw_port_type_t
nw_port_setup_type(struct nw_port
* port
) {
105 const char* type
= nw_config_get(port
->config
, "TYPE");
107 return NW_PORT_UNKNOWN
;
109 return nw_port_type_from_string(type
);
112 static int nw_port_setup(struct nw_port
* port
) {
116 // Compose the path to the main configuration file
117 r
= nw_path_join(path
, PORT_CONFIG_DIR
, port
->name
);
121 // Initialize the configuration
122 r
= nw_config_create(&port
->config
, path
);
127 port
->type
= nw_port_setup_type(port
);
129 ERROR("Could not determine type of port %s\n", port
->name
);
133 // Perform some common initialization
134 r
= nw_port_setup_common(port
);
138 // Call any custom initialization
139 switch (port
->type
) {
140 // These do not need any special initialization
142 case NW_PORT_UNKNOWN
:
149 int nw_port_create(struct nw_port
** port
, const char* name
) {
152 // Allocate a new object
153 struct nw_port
* p
= calloc(1, sizeof(*p
));
157 // Initialize reference counter
161 r
= nw_string_set(p
->name
, name
);
166 r
= nw_port_setup(p
);
178 struct nw_port
* nw_port_ref(struct nw_port
* port
) {
184 struct nw_port
* nw_port_unref(struct nw_port
* port
) {
185 if (--port
->nrefs
> 0)
192 const char* nw_port_name(struct nw_port
* port
) {