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

Last change on this file 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
RevLine 
[bd88bee]1/*
[ca48672]2 * Copyright (c) 2025 Jiri Svoboda
[bd88bee]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>
[c1694b6b]38#include <str_error.h>
[bd88bee]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
[984a9ba]51static void dhcp_client_conn(ipc_call_t *, void *);
[bd88bee]52
[b7fd2a0]53static errno_t dhcp_init(void)
[bd88bee]54{
[4c6fd56]55 loc_srv_t *srv;
[b7fd2a0]56 errno_t rc;
[bd88bee]57
58 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_init()");
59
[b417559]60 dhcpsrv_links_init();
61
[b688fd8]62 async_set_fallback_port_handler(dhcp_client_conn, NULL);
[bd88bee]63
[4c6fd56]64 rc = loc_server_register(NAME, &srv);
[bd88bee]65 if (rc != EOK) {
[c1694b6b]66 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
[bd88bee]67 return EEXIST;
68 }
69
70 service_id_t sid;
[ca48672]71 rc = loc_service_register(srv, SERVICE_NAME_DHCP, fallback_port_id,
72 &sid);
[bd88bee]73 if (rc != EOK) {
[4c6fd56]74 loc_server_unregister(srv);
[c1694b6b]75 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
[bd88bee]76 return EEXIST;
77 }
78
79 return EOK;
80}
81
[984a9ba]82static void dhcp_link_add_srv(ipc_call_t *call)
[bd88bee]83{
84 sysarg_t link_id;
[b7fd2a0]85 errno_t rc;
[bd88bee]86
87 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_add_srv()");
88
[fafb8e5]89 link_id = ipc_get_arg1(call);
[bd88bee]90
91 rc = dhcpsrv_link_add(link_id);
[984a9ba]92 async_answer_0(call, rc);
[bd88bee]93}
94
[984a9ba]95static void dhcp_link_remove_srv(ipc_call_t *call)
[bd88bee]96{
97 sysarg_t link_id;
[b7fd2a0]98 errno_t rc;
[bd88bee]99
100 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_remove_srv()");
101
[fafb8e5]102 link_id = ipc_get_arg1(call);
[bd88bee]103
104 rc = dhcpsrv_link_remove(link_id);
[984a9ba]105 async_answer_0(call, rc);
[bd88bee]106}
107
[984a9ba]108static void dhcp_discover_srv(ipc_call_t *call)
[053fc2b]109{
110 sysarg_t link_id;
[b7fd2a0]111 errno_t rc;
[053fc2b]112
113 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_discover_srv()");
114
[fafb8e5]115 link_id = ipc_get_arg1(call);
[053fc2b]116
117 rc = dhcpsrv_discover(link_id);
[984a9ba]118 async_answer_0(call, rc);
[053fc2b]119}
120
[984a9ba]121static void dhcp_client_conn(ipc_call_t *icall, void *arg)
[bd88bee]122{
123 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
124
125 /* Accept the connection */
[beb83c1]126 async_accept_0(icall);
[bd88bee]127
128 while (true) {
129 ipc_call_t call;
[984a9ba]130 async_get_call(&call);
[fafb8e5]131 sysarg_t method = ipc_get_imethod(&call);
[bd88bee]132
133 if (!method) {
134 /* The other side has hung up */
[984a9ba]135 async_answer_0(&call, EOK);
[bd88bee]136 return;
137 }
138
139 switch (method) {
140 case DHCP_LINK_ADD:
[984a9ba]141 dhcp_link_add_srv(&call);
[bd88bee]142 break;
143 case DHCP_LINK_REMOVE:
[984a9ba]144 dhcp_link_remove_srv(&call);
[bd88bee]145 break;
[053fc2b]146 case DHCP_DISCOVER:
[984a9ba]147 dhcp_discover_srv(&call);
[053fc2b]148 break;
[bd88bee]149 default:
[984a9ba]150 async_answer_0(&call, EINVAL);
[bd88bee]151 }
152 }
153}
154
155int main(int argc, char *argv[])
156{
[b7fd2a0]157 errno_t rc;
[bd88bee]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.