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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9ab4026 was 984a9ba, checked in by Martin Decky <martin@…>, 7 years ago

do not expose the call capability handler from the async framework

Keep the call capability handler encapsulated within the async framework
and do not expose it explicitly via its API. Use the pointer to
ipc_call_t as the sole object identifying an IPC call in the code that
uses the async framework.

This plugs a major leak in the abstraction and also simplifies both the
async framework (slightly) and all IPC servers.

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