[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 | {
|
---|
[d8b47eca] | 110 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
[a2e3ee6] | 111 |
|
---|
[ecff3d9] | 112 | ipc_call_t answer;
|
---|
[d8b47eca] | 113 | aid_t req = async_send_3(exch, INET_SEND, dgram->tos, ttl, df,
|
---|
| 114 | &answer);
|
---|
| 115 |
|
---|
| 116 | int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
|
---|
| 117 | if (rc != EOK) {
|
---|
| 118 | async_exchange_end(exch);
|
---|
| 119 | async_forget(req);
|
---|
| 120 | return rc;
|
---|
| 121 | }
|
---|
[a2e3ee6] | 122 |
|
---|
[d8b47eca] | 123 | rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
|
---|
| 124 | if (rc != EOK) {
|
---|
[02a09ed] | 125 | async_exchange_end(exch);
|
---|
[d8b47eca] | 126 | async_forget(req);
|
---|
| 127 | return rc;
|
---|
[02a09ed] | 128 | }
|
---|
[a2e3ee6] | 129 |
|
---|
[d8b47eca] | 130 | rc = async_data_write_start(exch, dgram->data, dgram->size);
|
---|
| 131 |
|
---|
| 132 | async_exchange_end(exch);
|
---|
| 133 |
|
---|
[ecff3d9] | 134 | if (rc != EOK) {
|
---|
[50b581d] | 135 | async_forget(req);
|
---|
[ecff3d9] | 136 | return rc;
|
---|
| 137 | }
|
---|
[a2e3ee6] | 138 |
|
---|
[ecff3d9] | 139 | sysarg_t retval;
|
---|
| 140 | async_wait_for(req, &retval);
|
---|
[a2e3ee6] | 141 |
|
---|
[02a09ed] | 142 | return (int) retval;
|
---|
[c76e926] | 143 | }
|
---|
| 144 |
|
---|
| 145 | int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
|
---|
[19a4f73] | 146 | {
|
---|
[d8b47eca] | 147 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
[19a4f73] | 148 |
|
---|
[d8b47eca] | 149 | ipc_call_t answer;
|
---|
| 150 | aid_t req = async_send_1(exch, INET_GET_SRCADDR, tos, &answer);
|
---|
[19a4f73] | 151 |
|
---|
[d8b47eca] | 152 | int rc = async_data_write_start(exch, remote, sizeof(inet_addr_t));
|
---|
| 153 | if (rc != EOK) {
|
---|
[02a09ed] | 154 | async_exchange_end(exch);
|
---|
[d8b47eca] | 155 | async_forget(req);
|
---|
| 156 | return rc;
|
---|
[02a09ed] | 157 | }
|
---|
[d8b47eca] | 158 |
|
---|
| 159 | rc = async_data_read_start(exch, local, sizeof(inet_addr_t));
|
---|
| 160 |
|
---|
| 161 | async_exchange_end(exch);
|
---|
| 162 |
|
---|
| 163 | if (rc != EOK) {
|
---|
| 164 | async_forget(req);
|
---|
| 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | sysarg_t retval;
|
---|
| 169 | async_wait_for(req, &retval);
|
---|
| 170 |
|
---|
| 171 | return (int) retval;
|
---|
[19a4f73] | 172 | }
|
---|
| 173 |
|
---|
[02a09ed] | 174 | static void inet_ev_recv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[59157eb] | 175 | {
|
---|
| 176 | inet_dgram_t dgram;
|
---|
[a2e3ee6] | 177 |
|
---|
[02a09ed] | 178 | dgram.tos = IPC_GET_ARG1(*icall);
|
---|
[b5cf742a] | 179 |
|
---|
[02a09ed] | 180 | ipc_callid_t callid;
|
---|
| 181 | size_t size;
|
---|
| 182 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 183 | async_answer_0(callid, EINVAL);
|
---|
| 184 | async_answer_0(iid, EINVAL);
|
---|
| 185 | return;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (size != sizeof(inet_addr_t)) {
|
---|
| 189 | async_answer_0(callid, EINVAL);
|
---|
| 190 | async_answer_0(iid, EINVAL);
|
---|
| 191 | return;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | int rc = async_data_write_finalize(callid, &dgram.src, size);
|
---|
| 195 | if (rc != EOK) {
|
---|
| 196 | async_answer_0(callid, rc);
|
---|
| 197 | async_answer_0(iid, rc);
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 202 | async_answer_0(callid, EINVAL);
|
---|
| 203 | async_answer_0(iid, EINVAL);
|
---|
| 204 | return;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | if (size != sizeof(inet_addr_t)) {
|
---|
| 208 | async_answer_0(callid, EINVAL);
|
---|
| 209 | async_answer_0(iid, EINVAL);
|
---|
| 210 | return;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | rc = async_data_write_finalize(callid, &dgram.dest, size);
|
---|
[59157eb] | 214 | if (rc != EOK) {
|
---|
| 215 | async_answer_0(callid, rc);
|
---|
[02a09ed] | 216 | async_answer_0(iid, rc);
|
---|
| 217 | return;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
|
---|
| 221 | if (rc != EOK) {
|
---|
| 222 | async_answer_0(iid, rc);
|
---|
[59157eb] | 223 | return;
|
---|
| 224 | }
|
---|
[b5cf742a] | 225 |
|
---|
[59157eb] | 226 | rc = inet_ev_ops->recv(&dgram);
|
---|
[02a09ed] | 227 | async_answer_0(iid, rc);
|
---|
[59157eb] | 228 | }
|
---|
| 229 |
|
---|
[c76e926] | 230 | static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 231 | {
|
---|
| 232 | while (true) {
|
---|
| 233 | ipc_call_t call;
|
---|
| 234 | ipc_callid_t callid = async_get_call(&call);
|
---|
[b5cf742a] | 235 |
|
---|
[c76e926] | 236 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 237 | /* TODO: Handle hangup */
|
---|
| 238 | return;
|
---|
| 239 | }
|
---|
[b5cf742a] | 240 |
|
---|
[c76e926] | 241 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 242 | case INET_EV_RECV:
|
---|
[59157eb] | 243 | inet_ev_recv(callid, &call);
|
---|
[c76e926] | 244 | break;
|
---|
| 245 | default:
|
---|
| 246 | async_answer_0(callid, ENOTSUP);
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /** @}
|
---|
| 252 | */
|
---|