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

Last change on this file since ca48672 was ca48672, checked in by Jiri Svoboda <jiri@…>, 8 days ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2025 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, fallback_port_id,
72 &sid);
73 if (rc != EOK) {
74 loc_server_unregister(srv);
75 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
76 return EEXIST;
77 }
78
79 return EOK;
80}
81
82static void dhcp_link_add_srv(ipc_call_t *call)
83{
84 sysarg_t link_id;
85 errno_t rc;
86
87 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_add_srv()");
88
89 link_id = ipc_get_arg1(call);
90
91 rc = dhcpsrv_link_add(link_id);
92 async_answer_0(call, rc);
93}
94
95static void dhcp_link_remove_srv(ipc_call_t *call)
96{
97 sysarg_t link_id;
98 errno_t rc;
99
100 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_remove_srv()");
101
102 link_id = ipc_get_arg1(call);
103
104 rc = dhcpsrv_link_remove(link_id);
105 async_answer_0(call, rc);
106}
107
108static void dhcp_discover_srv(ipc_call_t *call)
109{
110 sysarg_t link_id;
111 errno_t rc;
112
113 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_discover_srv()");
114
115 link_id = ipc_get_arg1(call);
116
117 rc = dhcpsrv_discover(link_id);
118 async_answer_0(call, rc);
119}
120
121static void dhcp_client_conn(ipc_call_t *icall, void *arg)
122{
123 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
124
125 /* Accept the connection */
126 async_accept_0(icall);
127
128 while (true) {
129 ipc_call_t call;
130 async_get_call(&call);
131 sysarg_t method = ipc_get_imethod(&call);
132
133 if (!method) {
134 /* The other side has hung up */
135 async_answer_0(&call, EOK);
136 return;
137 }
138
139 switch (method) {
140 case DHCP_LINK_ADD:
141 dhcp_link_add_srv(&call);
142 break;
143 case DHCP_LINK_REMOVE:
144 dhcp_link_remove_srv(&call);
145 break;
146 case DHCP_DISCOVER:
147 dhcp_discover_srv(&call);
148 break;
149 default:
150 async_answer_0(&call, EINVAL);
151 }
152 }
153}
154
155int main(int argc, char *argv[])
156{
157 errno_t rc;
158
159 printf("%s: DHCP Service\n", NAME);
160
161 if (log_init(NAME) != EOK) {
162 printf(NAME ": Failed to initialize logging.\n");
163 return 1;
164 }
165
166 rc = dhcp_init();
167 if (rc != EOK)
168 return 1;
169
170 printf(NAME ": Accepting connections.\n");
171 task_retval(0);
172 async_manager();
173
174 return 0;
175}
176
177/** @}
178 */
Note: See TracBrowser for help on using the repository browser.