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