source: mainline/uspace/srv/net/dhcp/main.c@ 89e5c0c7

Last change on this file since 89e5c0c7 was 1bbc6dc, checked in by Jiri Svoboda <jiri@…>, 11 months ago

Network configuration persistence.

nconfsrv is folded into inetsrv
DHCP is disabled when a static address is configured on a link

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup dhcp
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <async.h>
37#include <errno.h>
38#include <str_error.h>
39#include <io/log.h>
40#include <ipc/dhcp.h>
41#include <ipc/services.h>
42#include <loc.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <task.h>
46
47#include "dhcp.h"
48
49#define NAME "dhcp"
50
51static void dhcp_client_conn(ipc_call_t *, void *);
52
53static errno_t dhcp_init(void)
54{
55 loc_srv_t *srv;
56 errno_t rc;
57
58 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_init()");
59
60 dhcpsrv_links_init();
61
62 async_set_fallback_port_handler(dhcp_client_conn, NULL);
63
64 rc = loc_server_register(NAME, &srv);
65 if (rc != EOK) {
66 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
67 return EEXIST;
68 }
69
70 service_id_t sid;
71 rc = loc_service_register(srv, SERVICE_NAME_DHCP, &sid);
72 if (rc != EOK) {
73 loc_server_unregister(srv);
74 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
75 return EEXIST;
76 }
77
78 return EOK;
79}
80
81static void dhcp_link_add_srv(ipc_call_t *call)
82{
83 sysarg_t link_id;
84 errno_t rc;
85
86 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_add_srv()");
87
88 link_id = ipc_get_arg1(call);
89
90 rc = dhcpsrv_link_add(link_id);
91 async_answer_0(call, rc);
92}
93
94static void dhcp_link_remove_srv(ipc_call_t *call)
95{
96 sysarg_t link_id;
97 errno_t rc;
98
99 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_remove_srv()");
100
101 link_id = ipc_get_arg1(call);
102
103 rc = dhcpsrv_link_remove(link_id);
104 async_answer_0(call, rc);
105}
106
107static void dhcp_discover_srv(ipc_call_t *call)
108{
109 sysarg_t link_id;
110 errno_t rc;
111
112 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_discover_srv()");
113
114 link_id = ipc_get_arg1(call);
115
116 rc = dhcpsrv_discover(link_id);
117 async_answer_0(call, rc);
118}
119
120static void dhcp_client_conn(ipc_call_t *icall, void *arg)
121{
122 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
123
124 /* Accept the connection */
125 async_accept_0(icall);
126
127 while (true) {
128 ipc_call_t call;
129 async_get_call(&call);
130 sysarg_t method = ipc_get_imethod(&call);
131
132 if (!method) {
133 /* The other side has hung up */
134 async_answer_0(&call, EOK);
135 return;
136 }
137
138 switch (method) {
139 case DHCP_LINK_ADD:
140 dhcp_link_add_srv(&call);
141 break;
142 case DHCP_LINK_REMOVE:
143 dhcp_link_remove_srv(&call);
144 break;
145 case DHCP_DISCOVER:
146 dhcp_discover_srv(&call);
147 break;
148 default:
149 async_answer_0(&call, EINVAL);
150 }
151 }
152}
153
154int main(int argc, char *argv[])
155{
156 errno_t rc;
157
158 printf("%s: DHCP Service\n", NAME);
159
160 if (log_init(NAME) != EOK) {
161 printf(NAME ": Failed to initialize logging.\n");
162 return 1;
163 }
164
165 rc = dhcp_init();
166 if (rc != EOK)
167 return 1;
168
169 printf(NAME ": Accepting connections.\n");
170 task_retval(0);
171 async_manager();
172
173 return 0;
174}
175
176/** @}
177 */
Note: See TracBrowser for help on using the repository browser.