[c76e926] | 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 | #include <async.h>
|
---|
| 30 | #include <assert.h>
|
---|
| 31 | #include <errno.h>
|
---|
[02a09ed] | 32 | #include <net/socket_codes.h>
|
---|
[c76e926] | 33 | #include <inet/inet.h>
|
---|
| 34 | #include <ipc/inet.h>
|
---|
| 35 | #include <ipc/services.h>
|
---|
| 36 | #include <loc.h>
|
---|
| 37 |
|
---|
| 38 | static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
| 39 |
|
---|
| 40 | static async_sess_t *inet_sess = NULL;
|
---|
| 41 | static inet_ev_ops_t *inet_ev_ops = NULL;
|
---|
| 42 | static uint8_t inet_protocol = 0;
|
---|
| 43 |
|
---|
| 44 | static int inet_callback_create(void)
|
---|
| 45 | {
|
---|
| 46 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
[77ad86c] | 47 |
|
---|
[c76e926] | 48 | ipc_call_t answer;
|
---|
| 49 | aid_t req = async_send_0(exch, INET_CALLBACK_CREATE, &answer);
|
---|
| 50 | int rc = async_connect_to_me(exch, 0, 0, 0, inet_cb_conn, NULL);
|
---|
| 51 | async_exchange_end(exch);
|
---|
[77ad86c] | 52 |
|
---|
[c76e926] | 53 | if (rc != EOK)
|
---|
| 54 | return rc;
|
---|
[77ad86c] | 55 |
|
---|
[c76e926] | 56 | sysarg_t retval;
|
---|
| 57 | async_wait_for(req, &retval);
|
---|
[77ad86c] | 58 |
|
---|
| 59 | return retval;
|
---|
[c76e926] | 60 | }
|
---|
| 61 |
|
---|
| 62 | static int inet_set_proto(uint8_t protocol)
|
---|
| 63 | {
|
---|
| 64 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
[77ad86c] | 65 | int rc = async_req_1_0(exch, INET_SET_PROTO, protocol);
|
---|
[c76e926] | 66 | async_exchange_end(exch);
|
---|
[77ad86c] | 67 |
|
---|
[c76e926] | 68 | return rc;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | int inet_init(uint8_t protocol, inet_ev_ops_t *ev_ops)
|
---|
| 72 | {
|
---|
| 73 | service_id_t inet_svc;
|
---|
[ecff3d9] | 74 | int rc;
|
---|
[c76e926] | 75 |
|
---|
| 76 | assert(inet_sess == NULL);
|
---|
| 77 | assert(inet_ev_ops == NULL);
|
---|
| 78 | assert(inet_protocol == 0);
|
---|
[77ad86c] | 79 |
|
---|
[ecff3d9] | 80 | rc = loc_service_get_id(SERVICE_NAME_INET, &inet_svc,
|
---|
[c76e926] | 81 | IPC_FLAG_BLOCKING);
|
---|
[ecff3d9] | 82 | if (rc != EOK)
|
---|
| 83 | return ENOENT;
|
---|
[77ad86c] | 84 |
|
---|
[c76e926] | 85 | inet_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
|
---|
| 86 | IPC_FLAG_BLOCKING);
|
---|
| 87 | if (inet_sess == NULL)
|
---|
| 88 | return ENOENT;
|
---|
[77ad86c] | 89 |
|
---|
[c76e926] | 90 | if (inet_set_proto(protocol) != EOK) {
|
---|
| 91 | async_hangup(inet_sess);
|
---|
| 92 | inet_sess = NULL;
|
---|
| 93 | return EIO;
|
---|
| 94 | }
|
---|
[77ad86c] | 95 |
|
---|
[c76e926] | 96 | if (inet_callback_create() != EOK) {
|
---|
| 97 | async_hangup(inet_sess);
|
---|
| 98 | inet_sess = NULL;
|
---|
| 99 | return EIO;
|
---|
| 100 | }
|
---|
[77ad86c] | 101 |
|
---|
[c76e926] | 102 | inet_protocol = protocol;
|
---|
| 103 | inet_ev_ops = ev_ops;
|
---|
| 104 |
|
---|
| 105 | return EOK;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | int inet_send(inet_dgram_t *dgram, uint8_t ttl, inet_df_t df)
|
---|
| 109 | {
|
---|
[02a09ed] | 110 | addr32_t src_v4;
|
---|
| 111 | addr128_t src_v6;
|
---|
| 112 | uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, &src_v6);
|
---|
[a2e3ee6] | 113 |
|
---|
[02a09ed] | 114 | addr32_t dest_v4;
|
---|
| 115 | addr128_t dest_v6;
|
---|
| 116 | uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, &dest_v6);
|
---|
[a2e3ee6] | 117 |
|
---|
[02a09ed] | 118 | if (src_af != dest_af)
|
---|
| 119 | return EINVAL;
|
---|
[a2e3ee6] | 120 |
|
---|
[02a09ed] | 121 | async_exch_t *exch;
|
---|
[ecff3d9] | 122 | ipc_call_t answer;
|
---|
[02a09ed] | 123 | aid_t req;
|
---|
| 124 | int rc;
|
---|
[a2e3ee6] | 125 |
|
---|
[02a09ed] | 126 | switch (src_af) {
|
---|
| 127 | case AF_INET:
|
---|
| 128 | exch = async_exchange_begin(inet_sess);
|
---|
| 129 |
|
---|
| 130 | req = async_send_5(exch, INET_SEND, (sysarg_t) src_v4,
|
---|
| 131 | (sysarg_t) dest_v4, dgram->tos, ttl, df, &answer);
|
---|
| 132 | rc = async_data_write_start(exch, dgram->data, dgram->size);
|
---|
| 133 |
|
---|
| 134 | async_exchange_end(exch);
|
---|
| 135 | break;
|
---|
| 136 | case AF_INET6:
|
---|
| 137 | // FIXME TODO
|
---|
| 138 | return ENOTSUP;
|
---|
| 139 | default:
|
---|
| 140 | return EINVAL;
|
---|
| 141 | }
|
---|
[a2e3ee6] | 142 |
|
---|
[ecff3d9] | 143 | if (rc != EOK) {
|
---|
[50b581d] | 144 | async_forget(req);
|
---|
[ecff3d9] | 145 | return rc;
|
---|
| 146 | }
|
---|
[a2e3ee6] | 147 |
|
---|
[ecff3d9] | 148 | sysarg_t retval;
|
---|
| 149 | async_wait_for(req, &retval);
|
---|
[a2e3ee6] | 150 |
|
---|
[02a09ed] | 151 | return (int) retval;
|
---|
[c76e926] | 152 | }
|
---|
| 153 |
|
---|
| 154 | int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
|
---|
[19a4f73] | 155 | {
|
---|
[02a09ed] | 156 | addr32_t remote_v4;
|
---|
| 157 | addr128_t remote_v6;
|
---|
| 158 | uint16_t remote_af = inet_addr_get(remote, &remote_v4, &remote_v6);
|
---|
[19a4f73] | 159 |
|
---|
[02a09ed] | 160 | async_exch_t *exch;
|
---|
| 161 | int rc;
|
---|
[19a4f73] | 162 |
|
---|
[02a09ed] | 163 | switch (remote_af) {
|
---|
| 164 | case AF_INET:
|
---|
| 165 | exch = async_exchange_begin(inet_sess);
|
---|
| 166 |
|
---|
| 167 | sysarg_t local_v4;
|
---|
| 168 | rc = async_req_2_1(exch, INET_GET_SRCADDR, (sysarg_t) remote_v4,
|
---|
| 169 | tos, &local_v4);
|
---|
| 170 |
|
---|
| 171 | async_exchange_end(exch);
|
---|
| 172 |
|
---|
| 173 | if (rc != EOK)
|
---|
| 174 | return rc;
|
---|
| 175 |
|
---|
| 176 | inet_addr_set(local_v4, local);
|
---|
| 177 | return EOK;
|
---|
| 178 | case AF_INET6:
|
---|
| 179 | // FIXME TODO
|
---|
| 180 | return ENOTSUP;
|
---|
| 181 | default:
|
---|
| 182 | return EINVAL;
|
---|
| 183 | }
|
---|
[19a4f73] | 184 | }
|
---|
| 185 |
|
---|
[02a09ed] | 186 | static void inet_ev_recv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[59157eb] | 187 | {
|
---|
| 188 | inet_dgram_t dgram;
|
---|
[a2e3ee6] | 189 |
|
---|
[02a09ed] | 190 | dgram.tos = IPC_GET_ARG1(*icall);
|
---|
[b5cf742a] | 191 |
|
---|
[02a09ed] | 192 | ipc_callid_t callid;
|
---|
| 193 | size_t size;
|
---|
| 194 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 195 | async_answer_0(callid, EINVAL);
|
---|
| 196 | async_answer_0(iid, EINVAL);
|
---|
| 197 | return;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | if (size != sizeof(inet_addr_t)) {
|
---|
| 201 | async_answer_0(callid, EINVAL);
|
---|
| 202 | async_answer_0(iid, EINVAL);
|
---|
| 203 | return;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | int rc = async_data_write_finalize(callid, &dgram.src, size);
|
---|
| 207 | if (rc != EOK) {
|
---|
| 208 | async_answer_0(callid, rc);
|
---|
| 209 | async_answer_0(iid, rc);
|
---|
| 210 | return;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 214 | async_answer_0(callid, EINVAL);
|
---|
| 215 | async_answer_0(iid, EINVAL);
|
---|
| 216 | return;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | if (size != sizeof(inet_addr_t)) {
|
---|
| 220 | async_answer_0(callid, EINVAL);
|
---|
| 221 | async_answer_0(iid, EINVAL);
|
---|
| 222 | return;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | rc = async_data_write_finalize(callid, &dgram.dest, size);
|
---|
[59157eb] | 226 | if (rc != EOK) {
|
---|
| 227 | async_answer_0(callid, rc);
|
---|
[02a09ed] | 228 | async_answer_0(iid, rc);
|
---|
| 229 | return;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
|
---|
| 233 | if (rc != EOK) {
|
---|
| 234 | async_answer_0(iid, rc);
|
---|
[59157eb] | 235 | return;
|
---|
| 236 | }
|
---|
[b5cf742a] | 237 |
|
---|
[59157eb] | 238 | rc = inet_ev_ops->recv(&dgram);
|
---|
[02a09ed] | 239 | async_answer_0(iid, rc);
|
---|
[59157eb] | 240 | }
|
---|
| 241 |
|
---|
[c76e926] | 242 | static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 243 | {
|
---|
| 244 | while (true) {
|
---|
| 245 | ipc_call_t call;
|
---|
| 246 | ipc_callid_t callid = async_get_call(&call);
|
---|
[b5cf742a] | 247 |
|
---|
[c76e926] | 248 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 249 | /* TODO: Handle hangup */
|
---|
| 250 | return;
|
---|
| 251 | }
|
---|
[b5cf742a] | 252 |
|
---|
[c76e926] | 253 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 254 | case INET_EV_RECV:
|
---|
[59157eb] | 255 | inet_ev_recv(callid, &call);
|
---|
[c76e926] | 256 | break;
|
---|
| 257 | default:
|
---|
| 258 | async_answer_0(callid, ENOTSUP);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /** @}
|
---|
| 264 | */
|
---|