| 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 |
|
|---|
| 52 | static void dhcp_client_conn(ipc_call_t *, void *);
|
|---|
| 53 |
|
|---|
| 54 | static 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 |
|
|---|
| 86 | static 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 |
|
|---|
| 99 | static 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 |
|
|---|
| 112 | static 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 |
|
|---|
| 125 | static 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 |
|
|---|
| 159 | int 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 | */
|
|---|