| 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 <io/log.h>
|
|---|
| 39 | #include <inet/inetcfg.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 |
|
|---|
| 51 | static void dhcp_client_conn(ipc_callid_t, ipc_call_t *, void *);
|
|---|
| 52 |
|
|---|
| 53 | static int dhcp_init(void)
|
|---|
| 54 | {
|
|---|
| 55 | int rc;
|
|---|
| 56 |
|
|---|
| 57 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_init()");
|
|---|
| 58 |
|
|---|
| 59 | dhcpsrv_links_init();
|
|---|
| 60 |
|
|---|
| 61 | rc = inetcfg_init();
|
|---|
| 62 | if (rc != EOK) {
|
|---|
| 63 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting inet configuration service.\n");
|
|---|
| 64 | return EIO;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | async_set_client_connection(dhcp_client_conn);
|
|---|
| 68 |
|
|---|
| 69 | rc = loc_server_register(NAME);
|
|---|
| 70 | if (rc != EOK) {
|
|---|
| 71 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
|
|---|
| 72 | return EEXIST;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | service_id_t sid;
|
|---|
| 76 | rc = loc_service_register(SERVICE_NAME_DHCP, &sid);
|
|---|
| 77 | if (rc != EOK) {
|
|---|
| 78 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
|
|---|
| 79 | return EEXIST;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | return EOK;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | static void dhcp_link_add_srv(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 86 | {
|
|---|
| 87 | sysarg_t link_id;
|
|---|
| 88 | int rc;
|
|---|
| 89 |
|
|---|
| 90 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_add_srv()");
|
|---|
| 91 |
|
|---|
| 92 | link_id = IPC_GET_ARG1(*call);
|
|---|
| 93 |
|
|---|
| 94 | rc = dhcpsrv_link_add(link_id);
|
|---|
| 95 | async_answer_0(callid, rc);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static void dhcp_link_remove_srv(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 99 | {
|
|---|
| 100 | sysarg_t link_id;
|
|---|
| 101 | int rc;
|
|---|
| 102 |
|
|---|
| 103 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_link_remove_srv()");
|
|---|
| 104 |
|
|---|
| 105 | link_id = IPC_GET_ARG1(*call);
|
|---|
| 106 |
|
|---|
| 107 | rc = dhcpsrv_link_remove(link_id);
|
|---|
| 108 | async_answer_0(callid, rc);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static void dhcp_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 112 | {
|
|---|
| 113 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
|
|---|
| 114 |
|
|---|
| 115 | /* Accept the connection */
|
|---|
| 116 | async_answer_0(iid, EOK);
|
|---|
| 117 |
|
|---|
| 118 | while (true) {
|
|---|
| 119 | ipc_call_t call;
|
|---|
| 120 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 121 | sysarg_t method = IPC_GET_IMETHOD(call);
|
|---|
| 122 |
|
|---|
| 123 | if (!method) {
|
|---|
| 124 | /* The other side has hung up */
|
|---|
| 125 | async_answer_0(callid, EOK);
|
|---|
| 126 | return;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | switch (method) {
|
|---|
| 130 | case DHCP_LINK_ADD:
|
|---|
| 131 | dhcp_link_add_srv(callid, &call);
|
|---|
| 132 | break;
|
|---|
| 133 | case DHCP_LINK_REMOVE:
|
|---|
| 134 | dhcp_link_remove_srv(callid, &call);
|
|---|
| 135 | break;
|
|---|
| 136 | default:
|
|---|
| 137 | async_answer_0(callid, EINVAL);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | int main(int argc, char *argv[])
|
|---|
| 143 | {
|
|---|
| 144 | int rc;
|
|---|
| 145 |
|
|---|
| 146 | printf("%s: DHCP Service\n", NAME);
|
|---|
| 147 |
|
|---|
| 148 | if (log_init(NAME) != EOK) {
|
|---|
| 149 | printf(NAME ": Failed to initialize logging.\n");
|
|---|
| 150 | return 1;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | rc = dhcp_init();
|
|---|
| 154 | if (rc != EOK)
|
|---|
| 155 | return 1;
|
|---|
| 156 |
|
|---|
| 157 | printf(NAME ": Accepting connections.\n");
|
|---|
| 158 | task_retval(0);
|
|---|
| 159 | async_manager();
|
|---|
| 160 |
|
|---|
| 161 | return 0;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /** @}
|
|---|
| 165 | */
|
|---|