| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 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 inet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Internet Protocol service
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <adt/list.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 | #include <io/log.h>
|
|---|
| 42 | #include <ipc/inet.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <loc.h>
|
|---|
| 45 | #include <stdio.h>
|
|---|
| 46 | #include <stdlib.h>
|
|---|
| 47 | #include <sys/types.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "addrobj.h"
|
|---|
| 50 | #include "inet.h"
|
|---|
| 51 | #include "inet_link.h"
|
|---|
| 52 |
|
|---|
| 53 | #define NAME "inet"
|
|---|
| 54 |
|
|---|
| 55 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
|---|
| 56 |
|
|---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
|---|
| 58 | static LIST_INITIALIZE(client_list);
|
|---|
| 59 |
|
|---|
| 60 | static int inet_init(void)
|
|---|
| 61 | {
|
|---|
| 62 | service_id_t sid;
|
|---|
| 63 | int rc;
|
|---|
| 64 |
|
|---|
| 65 | log_msg(LVL_DEBUG, "inet_init()");
|
|---|
| 66 |
|
|---|
| 67 | async_set_client_connection(inet_client_conn);
|
|---|
| 68 |
|
|---|
| 69 | rc = loc_server_register(NAME);
|
|---|
| 70 | if (rc != EOK) {
|
|---|
| 71 | log_msg(LVL_ERROR, "Failed registering server (%d).", rc);
|
|---|
| 72 | return EEXIST;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | rc = loc_service_register(SERVICE_NAME_INET, &sid);
|
|---|
| 76 | if (rc != EOK) {
|
|---|
| 77 | log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
|
|---|
| 78 | return EEXIST;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | rc = inet_link_discovery_start();
|
|---|
| 82 | if (rc != EOK)
|
|---|
| 83 | return EEXIST;
|
|---|
| 84 |
|
|---|
| 85 | return EOK;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
|
|---|
| 89 | ipc_call_t *call)
|
|---|
| 90 | {
|
|---|
| 91 | log_msg(LVL_DEBUG, "inet_callback_create_srv()");
|
|---|
| 92 |
|
|---|
| 93 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
|---|
| 94 | if (sess == NULL) {
|
|---|
| 95 | async_answer_0(callid, ENOMEM);
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | client->sess = sess;
|
|---|
| 100 | async_answer_0(callid, EOK);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
|
|---|
| 104 | uint8_t ttl, int df)
|
|---|
| 105 | {
|
|---|
| 106 | inet_addrobj_t *addr;
|
|---|
| 107 |
|
|---|
| 108 | addr = inet_addrobj_find(&dgram->dest);
|
|---|
| 109 | if (addr != NULL) {
|
|---|
| 110 | /* Destination is directly accessible */
|
|---|
| 111 | return inet_addrobj_send_dgram(addr, dgram, ttl, df);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /* TODO: Gateways */
|
|---|
| 115 | log_msg(LVL_DEBUG, "inet_send: No route to destination.");
|
|---|
| 116 | return ENOENT;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t callid,
|
|---|
| 120 | ipc_call_t *call)
|
|---|
| 121 | {
|
|---|
| 122 | log_msg(LVL_DEBUG, "inet_get_srcaddr_srv()");
|
|---|
| 123 |
|
|---|
| 124 | async_answer_0(callid, ENOTSUP);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
|
|---|
| 128 | ipc_call_t *call)
|
|---|
| 129 | {
|
|---|
| 130 | inet_dgram_t dgram;
|
|---|
| 131 | uint8_t ttl;
|
|---|
| 132 | int df;
|
|---|
| 133 | int rc;
|
|---|
| 134 |
|
|---|
| 135 | log_msg(LVL_DEBUG, "inet_send_srv()");
|
|---|
| 136 |
|
|---|
| 137 | dgram.src.ipv4 = IPC_GET_ARG1(*call);
|
|---|
| 138 | dgram.dest.ipv4 = IPC_GET_ARG2(*call);
|
|---|
| 139 | dgram.tos = IPC_GET_ARG3(*call);
|
|---|
| 140 | ttl = IPC_GET_ARG4(*call);
|
|---|
| 141 | df = IPC_GET_ARG5(*call);
|
|---|
| 142 |
|
|---|
| 143 | rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
|
|---|
| 144 | if (rc != EOK) {
|
|---|
| 145 | async_answer_0(callid, rc);
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | rc = inet_send(client, &dgram, ttl, df);
|
|---|
| 150 |
|
|---|
| 151 | free(dgram.data);
|
|---|
| 152 | async_answer_0(callid, rc);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
|
|---|
| 156 | ipc_call_t *call)
|
|---|
| 157 | {
|
|---|
| 158 | sysarg_t proto;
|
|---|
| 159 |
|
|---|
| 160 | proto = IPC_GET_ARG1(*call);
|
|---|
| 161 | log_msg(LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
|
|---|
| 162 |
|
|---|
| 163 | if (proto > UINT8_MAX) {
|
|---|
| 164 | async_answer_0(callid, EINVAL);
|
|---|
| 165 | return;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | client->protocol = proto;
|
|---|
| 169 | async_answer_0(callid, EOK);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | static void inet_client_init(inet_client_t *client)
|
|---|
| 173 | {
|
|---|
| 174 | client->sess = NULL;
|
|---|
| 175 |
|
|---|
| 176 | fibril_mutex_lock(&client_list_lock);
|
|---|
| 177 | list_append(&client->client_list, &client_list);
|
|---|
| 178 | fibril_mutex_unlock(&client_list_lock);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static void inet_client_fini(inet_client_t *client)
|
|---|
| 182 | {
|
|---|
| 183 | async_hangup(client->sess);
|
|---|
| 184 | client->sess = NULL;
|
|---|
| 185 |
|
|---|
| 186 | fibril_mutex_lock(&client_list_lock);
|
|---|
| 187 | list_remove(&client->client_list);
|
|---|
| 188 | fibril_mutex_unlock(&client_list_lock);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 192 | {
|
|---|
| 193 | inet_client_t client;
|
|---|
| 194 |
|
|---|
| 195 | log_msg(LVL_DEBUG, "inet_client_conn()");
|
|---|
| 196 |
|
|---|
| 197 | /* Accept the connection */
|
|---|
| 198 | async_answer_0(iid, EOK);
|
|---|
| 199 |
|
|---|
| 200 | inet_client_init(&client);
|
|---|
| 201 |
|
|---|
| 202 | while (true) {
|
|---|
| 203 | ipc_call_t call;
|
|---|
| 204 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 205 | sysarg_t method = IPC_GET_IMETHOD(call);
|
|---|
| 206 |
|
|---|
| 207 | if (!method) {
|
|---|
| 208 | /* The other side has hung up */
|
|---|
| 209 | async_answer_0(callid, EOK);
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | switch (method) {
|
|---|
| 214 | case INET_CALLBACK_CREATE:
|
|---|
| 215 | inet_callback_create_srv(&client, callid, &call);
|
|---|
| 216 | break;
|
|---|
| 217 | case INET_GET_SRCADDR:
|
|---|
| 218 | inet_get_srcaddr_srv(&client, callid, &call);
|
|---|
| 219 | break;
|
|---|
| 220 | case INET_SEND:
|
|---|
| 221 | inet_send_srv(&client, callid, &call);
|
|---|
| 222 | break;
|
|---|
| 223 | case INET_SET_PROTO:
|
|---|
| 224 | inet_set_proto_srv(&client, callid, &call);
|
|---|
| 225 | break;
|
|---|
| 226 | default:
|
|---|
| 227 | async_answer_0(callid, EINVAL);
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | inet_client_fini(&client);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
|
|---|
| 235 | {
|
|---|
| 236 | async_exch_t *exch = async_exchange_begin(client->sess);
|
|---|
| 237 |
|
|---|
| 238 | ipc_call_t answer;
|
|---|
| 239 | aid_t req = async_send_3(exch, INET_EV_RECV, dgram->src.ipv4,
|
|---|
| 240 | dgram->dest.ipv4, dgram->tos, &answer);
|
|---|
| 241 | int rc = async_data_write_start(exch, dgram->data, dgram->size);
|
|---|
| 242 | async_exchange_end(exch);
|
|---|
| 243 |
|
|---|
| 244 | if (rc != EOK) {
|
|---|
| 245 | async_wait_for(req, NULL);
|
|---|
| 246 | return rc;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | sysarg_t retval;
|
|---|
| 250 | async_wait_for(req, &retval);
|
|---|
| 251 | if (retval != EOK)
|
|---|
| 252 | return retval;
|
|---|
| 253 |
|
|---|
| 254 | return EOK;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | int main(int argc, char *argv[])
|
|---|
| 258 | {
|
|---|
| 259 | int rc;
|
|---|
| 260 |
|
|---|
| 261 | printf(NAME ": HelenOS Internet Protocol service\n");
|
|---|
| 262 |
|
|---|
| 263 | if (log_init(NAME, LVL_DEBUG) != EOK) {
|
|---|
| 264 | printf(NAME ": Failed to initialize logging.\n");
|
|---|
| 265 | return 1;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | rc = inet_init();
|
|---|
| 269 | if (rc != EOK)
|
|---|
| 270 | return 1;
|
|---|
| 271 |
|
|---|
| 272 | printf(NAME ": Accepting connections.\n");
|
|---|
| 273 | task_retval(0);
|
|---|
| 274 | async_manager();
|
|---|
| 275 |
|
|---|
| 276 | /* Not reached */
|
|---|
| 277 | return 0;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /** @}
|
|---|
| 281 | */
|
|---|