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