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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e948fde was 695b6ff, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Crude DHCP client prototype.

  • Property mode set to 100644
File size: 5.9 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 <net/socket_codes.h>
33#include <inet/inet.h>
34#include <ipc/inet.h>
35#include <ipc/services.h>
36#include <loc.h>
37
38static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
39
40static async_sess_t *inet_sess = NULL;
41static inet_ev_ops_t *inet_ev_ops = NULL;
42static uint8_t inet_protocol = 0;
43
44static int inet_callback_create(void)
45{
46 async_exch_t *exch = async_exchange_begin(inet_sess);
47
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);
52
53 if (rc != EOK)
54 return rc;
55
56 sysarg_t retval;
57 async_wait_for(req, &retval);
58
59 return retval;
60}
61
62static int inet_set_proto(uint8_t protocol)
63{
64 async_exch_t *exch = async_exchange_begin(inet_sess);
65 int rc = async_req_1_0(exch, INET_SET_PROTO, protocol);
66 async_exchange_end(exch);
67
68 return rc;
69}
70
71int inet_init(uint8_t protocol, inet_ev_ops_t *ev_ops)
72{
73 service_id_t inet_svc;
74 int rc;
75
76 assert(inet_sess == NULL);
77 assert(inet_ev_ops == NULL);
78 assert(inet_protocol == 0);
79
80 rc = loc_service_get_id(SERVICE_NAME_INET, &inet_svc,
81 IPC_FLAG_BLOCKING);
82 if (rc != EOK)
83 return ENOENT;
84
85 inet_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
86 IPC_FLAG_BLOCKING);
87 if (inet_sess == NULL)
88 return ENOENT;
89
90 if (inet_set_proto(protocol) != EOK) {
91 async_hangup(inet_sess);
92 inet_sess = NULL;
93 return EIO;
94 }
95
96 if (inet_callback_create() != EOK) {
97 async_hangup(inet_sess);
98 inet_sess = NULL;
99 return EIO;
100 }
101
102 inet_protocol = protocol;
103 inet_ev_ops = ev_ops;
104
105 return EOK;
106}
107
108int inet_send(inet_dgram_t *dgram, uint8_t ttl, inet_df_t df)
109{
110 async_exch_t *exch = async_exchange_begin(inet_sess);
111
112 ipc_call_t answer;
113 aid_t req = async_send_4(exch, INET_SEND, dgram->iplink, dgram->tos,
114 ttl, df, &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 }
122
123 rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
124 if (rc != EOK) {
125 async_exchange_end(exch);
126 async_forget(req);
127 return rc;
128 }
129
130 rc = async_data_write_start(exch, dgram->data, dgram->size);
131
132 async_exchange_end(exch);
133
134 if (rc != EOK) {
135 async_forget(req);
136 return rc;
137 }
138
139 sysarg_t retval;
140 async_wait_for(req, &retval);
141
142 return (int) retval;
143}
144
145int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
146{
147 async_exch_t *exch = async_exchange_begin(inet_sess);
148
149 ipc_call_t answer;
150 aid_t req = async_send_1(exch, INET_GET_SRCADDR, tos, &answer);
151
152 int rc = async_data_write_start(exch, remote, sizeof(inet_addr_t));
153 if (rc != EOK) {
154 async_exchange_end(exch);
155 async_forget(req);
156 return rc;
157 }
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;
172}
173
174static void inet_ev_recv(ipc_callid_t iid, ipc_call_t *icall)
175{
176 inet_dgram_t dgram;
177
178 dgram.tos = IPC_GET_ARG1(*icall);
179
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);
214 if (rc != EOK) {
215 async_answer_0(callid, rc);
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);
223 return;
224 }
225
226 rc = inet_ev_ops->recv(&dgram);
227 async_answer_0(iid, rc);
228}
229
230static 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);
235
236 if (!IPC_GET_IMETHOD(call)) {
237 /* TODO: Handle hangup */
238 return;
239 }
240
241 switch (IPC_GET_IMETHOD(call)) {
242 case INET_EV_RECV:
243 inet_ev_recv(callid, &call);
244 break;
245 default:
246 async_answer_0(callid, ENOTSUP);
247 }
248 }
249}
250
251/** @}
252 */
Note: See TracBrowser for help on using the repository browser.