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