source: mainline/uspace/lib/c/generic/inet.c@ a2e3ee6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a2e3ee6 was a2e3ee6, checked in by Martin Decky <martin@…>, 12 years ago

use new network address infrastructure (towards IPv6 support)

  • Property mode set to 100644
File size: 5.0 KB
Line 
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
37static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
38
39static async_sess_t *inet_sess = NULL;
40static inet_ev_ops_t *inet_ev_ops = NULL;
41static uint8_t inet_protocol = 0;
42
43static int inet_callback_create(void)
44{
45 async_exch_t *exch = async_exchange_begin(inet_sess);
46
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);
51
52 if (rc != EOK)
53 return rc;
54
55 sysarg_t retval;
56 async_wait_for(req, &retval);
57
58 return retval;
59}
60
61static int inet_set_proto(uint8_t protocol)
62{
63 async_exch_t *exch = async_exchange_begin(inet_sess);
64 int rc = async_req_1_0(exch, INET_SET_PROTO, protocol);
65 async_exchange_end(exch);
66
67 return rc;
68}
69
70int inet_init(uint8_t protocol, inet_ev_ops_t *ev_ops)
71{
72 service_id_t inet_svc;
73 int rc;
74
75 assert(inet_sess == NULL);
76 assert(inet_ev_ops == NULL);
77 assert(inet_protocol == 0);
78
79 rc = loc_service_get_id(SERVICE_NAME_INET, &inet_svc,
80 IPC_FLAG_BLOCKING);
81 if (rc != EOK)
82 return ENOENT;
83
84 inet_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
85 IPC_FLAG_BLOCKING);
86 if (inet_sess == NULL)
87 return ENOENT;
88
89 if (inet_set_proto(protocol) != EOK) {
90 async_hangup(inet_sess);
91 inet_sess = NULL;
92 return EIO;
93 }
94
95 if (inet_callback_create() != EOK) {
96 async_hangup(inet_sess);
97 inet_sess = NULL;
98 return EIO;
99 }
100
101 inet_protocol = protocol;
102 inet_ev_ops = ev_ops;
103
104 return EOK;
105}
106
107int inet_send(inet_dgram_t *dgram, uint8_t ttl, inet_df_t df)
108{
109 uint32_t src;
110 int rc = inet_addr_pack(&dgram->src, &src);
111 if (rc != EOK)
112 return rc;
113
114 uint32_t dest;
115 rc = inet_addr_pack(&dgram->dest, &dest);
116 if (rc != EOK)
117 return EOK;
118
119 async_exch_t *exch = async_exchange_begin(inet_sess);
120
121 ipc_call_t answer;
122 aid_t req = async_send_5(exch, INET_SEND, (sysarg_t) src,
123 (sysarg_t) dest, dgram->tos, ttl, df, &answer);
124 rc = async_data_write_start(exch, dgram->data, dgram->size);
125
126 async_exchange_end(exch);
127
128 if (rc != EOK) {
129 async_forget(req);
130 return rc;
131 }
132
133 sysarg_t retval;
134 async_wait_for(req, &retval);
135 if (retval != EOK)
136 return retval;
137
138 return EOK;
139}
140
141int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
142{
143 uint32_t remote_addr;
144 int rc = inet_addr_pack(remote, &remote_addr);
145 if (rc != EOK)
146 return rc;
147
148 async_exch_t *exch = async_exchange_begin(inet_sess);
149
150 sysarg_t local_addr;
151 rc = async_req_2_1(exch, INET_GET_SRCADDR, (sysarg_t) remote_addr,
152 tos, &local_addr);
153
154 async_exchange_end(exch);
155
156 if (rc != EOK)
157 return rc;
158
159 inet_addr_unpack(local_addr, local);
160 return EOK;
161}
162
163static void inet_ev_recv(ipc_callid_t callid, ipc_call_t *call)
164{
165 int rc;
166 inet_dgram_t dgram;
167
168 inet_addr_unpack(IPC_GET_ARG1(*call), &dgram.src);
169 inet_addr_unpack(IPC_GET_ARG2(*call), &dgram.dest);
170 dgram.tos = IPC_GET_ARG3(*call);
171
172 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
173 if (rc != EOK) {
174 async_answer_0(callid, rc);
175 return;
176 }
177
178 rc = inet_ev_ops->recv(&dgram);
179 async_answer_0(callid, rc);
180}
181
182static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
183{
184 while (true) {
185 ipc_call_t call;
186 ipc_callid_t callid = async_get_call(&call);
187
188 if (!IPC_GET_IMETHOD(call)) {
189 /* TODO: Handle hangup */
190 return;
191 }
192
193 switch (IPC_GET_IMETHOD(call)) {
194 case INET_EV_RECV:
195 inet_ev_recv(callid, &call);
196 break;
197 default:
198 async_answer_0(callid, ENOTSUP);
199 }
200 }
201}
202
203/** @}
204 */
Note: See TracBrowser for help on using the repository browser.