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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4c6fd56 was 4c6fd56, checked in by Jiri Svoboda <jiri@…>, 22 months ago

loc_server_register() should be callable more than once (API only)

Now loc_server_register() returns a pointer to a loc_srv_t object,
that is then passed to loc_service_register() and
loc_service_add_to_cat().

Added loc_server_unregister() that unregisters the server
and frees the loc_srv_t object.

Updated all callers. The implementation, however, is a stub.
It is not actually possible to call loc_server_register() more
than once, yet.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2023 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 <inet/inetcfg.h>
41#include <ipc/dhcp.h>
42#include <ipc/services.h>
43#include <loc.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <task.h>
47
48#include "dhcp.h"
49
50#define NAME "dhcp"
51
52static void dhcp_client_conn(ipc_call_t *, void *);
53
54static errno_t dhcp_init(void)
55{
56 loc_srv_t *srv;
57 errno_t rc;
58
59 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_init()");
60
61 dhcpsrv_links_init();
62
63 rc = inetcfg_init();
64 if (rc != EOK) {
65 log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting inet configuration service.\n");
66 return EIO;
67 }
68
69 async_set_fallback_port_handler(dhcp_client_conn, NULL);
70
71 rc = loc_server_register(NAME, &srv);
72 if (rc != EOK) {
73 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
74 return EEXIST;
75 }
76
77 service_id_t sid;
78 rc = loc_service_register(srv, SERVICE_NAME_DHCP, &sid);
79 if (rc != EOK) {
80 loc_server_unregister(srv);
81 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
82 return EEXIST;
83 }
84
85 return EOK;
86}
87
88static void dhcp_link_add_srv(ipc_call_t *call)
89{
90 sysarg_t link_id;
91 errno_t rc;
92
93 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_add_srv()");
94
95 link_id = ipc_get_arg1(call);
96
97 rc = dhcpsrv_link_add(link_id);
98 async_answer_0(call, rc);
99}
100
101static void dhcp_link_remove_srv(ipc_call_t *call)
102{
103 sysarg_t link_id;
104 errno_t rc;
105
106 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_remove_srv()");
107
108 link_id = ipc_get_arg1(call);
109
110 rc = dhcpsrv_link_remove(link_id);
111 async_answer_0(call, rc);
112}
113
114static void dhcp_discover_srv(ipc_call_t *call)
115{
116 sysarg_t link_id;
117 errno_t rc;
118
119 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_discover_srv()");
120
121 link_id = ipc_get_arg1(call);
122
123 rc = dhcpsrv_discover(link_id);
124 async_answer_0(call, rc);
125}
126
127static void dhcp_client_conn(ipc_call_t *icall, void *arg)
128{
129 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
130
131 /* Accept the connection */
132 async_accept_0(icall);
133
134 while (true) {
135 ipc_call_t call;
136 async_get_call(&call);
137 sysarg_t method = ipc_get_imethod(&call);
138
139 if (!method) {
140 /* The other side has hung up */
141 async_answer_0(&call, EOK);
142 return;
143 }
144
145 switch (method) {
146 case DHCP_LINK_ADD:
147 dhcp_link_add_srv(&call);
148 break;
149 case DHCP_LINK_REMOVE:
150 dhcp_link_remove_srv(&call);
151 break;
152 case DHCP_DISCOVER:
153 dhcp_discover_srv(&call);
154 break;
155 default:
156 async_answer_0(&call, EINVAL);
157 }
158 }
159}
160
161int main(int argc, char *argv[])
162{
163 errno_t rc;
164
165 printf("%s: DHCP Service\n", NAME);
166
167 if (log_init(NAME) != EOK) {
168 printf(NAME ": Failed to initialize logging.\n");
169 return 1;
170 }
171
172 rc = dhcp_init();
173 if (rc != EOK)
174 return 1;
175
176 printf(NAME ": Accepting connections.\n");
177 task_retval(0);
178 async_manager();
179
180 return 0;
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.