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

Last change on this file since 59ff52d was 59ff52d, checked in by Jakub Jermar <jakub@…>, 7 years ago

Add async_accept_0() for accepting connections

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[bd88bee]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>
[c1694b6b]38#include <str_error.h>
[bd88bee]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
[984a9ba]52static void dhcp_client_conn(ipc_call_t *, void *);
[bd88bee]53
[b7fd2a0]54static errno_t dhcp_init(void)
[bd88bee]55{
[b7fd2a0]56 errno_t rc;
[bd88bee]57
58 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_init()");
59
[b417559]60 dhcpsrv_links_init();
61
[bd88bee]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
[b688fd8]68 async_set_fallback_port_handler(dhcp_client_conn, NULL);
[bd88bee]69
70 rc = loc_server_register(NAME);
71 if (rc != EOK) {
[c1694b6b]72 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
[bd88bee]73 return EEXIST;
74 }
75
76 service_id_t sid;
77 rc = loc_service_register(SERVICE_NAME_DHCP, &sid);
78 if (rc != EOK) {
[c1694b6b]79 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
[bd88bee]80 return EEXIST;
81 }
82
83 return EOK;
84}
85
[984a9ba]86static void dhcp_link_add_srv(ipc_call_t *call)
[bd88bee]87{
88 sysarg_t link_id;
[b7fd2a0]89 errno_t rc;
[bd88bee]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);
[984a9ba]96 async_answer_0(call, rc);
[bd88bee]97}
98
[984a9ba]99static void dhcp_link_remove_srv(ipc_call_t *call)
[bd88bee]100{
101 sysarg_t link_id;
[b7fd2a0]102 errno_t rc;
[bd88bee]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);
[984a9ba]109 async_answer_0(call, rc);
[bd88bee]110}
111
[984a9ba]112static void dhcp_discover_srv(ipc_call_t *call)
[053fc2b]113{
114 sysarg_t link_id;
[b7fd2a0]115 errno_t rc;
[053fc2b]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);
[984a9ba]122 async_answer_0(call, rc);
[053fc2b]123}
124
[984a9ba]125static void dhcp_client_conn(ipc_call_t *icall, void *arg)
[bd88bee]126{
127 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
128
129 /* Accept the connection */
[59ff52d]130 async_accept_0(icall);
[bd88bee]131
132 while (true) {
133 ipc_call_t call;
[984a9ba]134 async_get_call(&call);
[bd88bee]135 sysarg_t method = IPC_GET_IMETHOD(call);
136
137 if (!method) {
138 /* The other side has hung up */
[984a9ba]139 async_answer_0(&call, EOK);
[bd88bee]140 return;
141 }
142
143 switch (method) {
144 case DHCP_LINK_ADD:
[984a9ba]145 dhcp_link_add_srv(&call);
[bd88bee]146 break;
147 case DHCP_LINK_REMOVE:
[984a9ba]148 dhcp_link_remove_srv(&call);
[bd88bee]149 break;
[053fc2b]150 case DHCP_DISCOVER:
[984a9ba]151 dhcp_discover_srv(&call);
[053fc2b]152 break;
[bd88bee]153 default:
[984a9ba]154 async_answer_0(&call, EINVAL);
[bd88bee]155 }
156 }
157}
158
159int main(int argc, char *argv[])
160{
[b7fd2a0]161 errno_t rc;
[bd88bee]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.