[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>
|
---|
| 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 |
|
---|
[b417559] | 59 | dhcpsrv_links_init();
|
---|
| 60 |
|
---|
[bd88bee] | 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 |
|
---|
[053fc2b] | 111 | static void dhcp_discover_srv(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 112 | {
|
---|
| 113 | sysarg_t link_id;
|
---|
| 114 | int rc;
|
---|
| 115 |
|
---|
| 116 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_discover_srv()");
|
---|
| 117 |
|
---|
| 118 | link_id = IPC_GET_ARG1(*call);
|
---|
| 119 |
|
---|
| 120 | rc = dhcpsrv_discover(link_id);
|
---|
| 121 | async_answer_0(callid, rc);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[bd88bee] | 124 | static void dhcp_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 125 | {
|
---|
| 126 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_client_conn()");
|
---|
| 127 |
|
---|
| 128 | /* Accept the connection */
|
---|
| 129 | async_answer_0(iid, EOK);
|
---|
| 130 |
|
---|
| 131 | while (true) {
|
---|
| 132 | ipc_call_t call;
|
---|
| 133 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 134 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 135 |
|
---|
| 136 | if (!method) {
|
---|
| 137 | /* The other side has hung up */
|
---|
| 138 | async_answer_0(callid, EOK);
|
---|
| 139 | return;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | switch (method) {
|
---|
| 143 | case DHCP_LINK_ADD:
|
---|
| 144 | dhcp_link_add_srv(callid, &call);
|
---|
| 145 | break;
|
---|
| 146 | case DHCP_LINK_REMOVE:
|
---|
| 147 | dhcp_link_remove_srv(callid, &call);
|
---|
| 148 | break;
|
---|
[053fc2b] | 149 | case DHCP_DISCOVER:
|
---|
| 150 | dhcp_discover_srv(callid, &call);
|
---|
| 151 | break;
|
---|
[bd88bee] | 152 | default:
|
---|
| 153 | async_answer_0(callid, EINVAL);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | int main(int argc, char *argv[])
|
---|
| 159 | {
|
---|
| 160 | int rc;
|
---|
| 161 |
|
---|
| 162 | printf("%s: DHCP Service\n", NAME);
|
---|
| 163 |
|
---|
| 164 | if (log_init(NAME) != EOK) {
|
---|
| 165 | printf(NAME ": Failed to initialize logging.\n");
|
---|
| 166 | return 1;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | rc = dhcp_init();
|
---|
| 170 | if (rc != EOK)
|
---|
| 171 | return 1;
|
---|
| 172 |
|
---|
| 173 | printf(NAME ": Accepting connections.\n");
|
---|
| 174 | task_retval(0);
|
---|
| 175 | async_manager();
|
---|
| 176 |
|
---|
| 177 | return 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /** @}
|
---|
| 181 | */
|
---|