source: mainline/uspace/lib/c/generic/iplink.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: 4.6 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/** @addtogroup libc
30 * @{
31 */
32/**
33 * @file
34 * @brief IP link client stub
35 */
36
37#include <async.h>
38#include <assert.h>
39#include <errno.h>
40#include <inet/iplink.h>
41#include <inet/addr.h>
42#include <ipc/iplink.h>
43#include <ipc/services.h>
44#include <loc.h>
45#include <stdlib.h>
46
47static void iplink_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
48
49int iplink_open(async_sess_t *sess, iplink_ev_ops_t *ev_ops,
50 iplink_t **riplink)
51{
52 iplink_t *iplink = calloc(1, sizeof(iplink_t));
53 if (iplink == NULL)
54 return ENOMEM;
55
56 iplink->sess = sess;
57 iplink->ev_ops = ev_ops;
58
59 async_exch_t *exch = async_exchange_begin(sess);
60
61 int rc = async_connect_to_me(exch, 0, 0, 0, iplink_cb_conn, iplink);
62 async_exchange_end(exch);
63
64 if (rc != EOK)
65 goto error;
66
67 *riplink = iplink;
68 return EOK;
69
70error:
71 if (iplink != NULL)
72 free(iplink);
73
74 return rc;
75}
76
77void iplink_close(iplink_t *iplink)
78{
79 /* XXX Synchronize with iplink_cb_conn */
80 free(iplink);
81}
82
83int iplink_send(iplink_t *iplink, iplink_sdu_t *sdu)
84{
85 async_exch_t *exch = async_exchange_begin(iplink->sess);
86
87 ipc_call_t answer;
88 aid_t req = async_send_2(exch, IPLINK_SEND, (sysarg_t) sdu->lsrc,
89 (sysarg_t) sdu->ldest, &answer);
90 int rc = async_data_write_start(exch, sdu->data, sdu->size);
91
92 async_exchange_end(exch);
93
94 if (rc != EOK) {
95 async_forget(req);
96 return rc;
97 }
98
99 sysarg_t retval;
100 async_wait_for(req, &retval);
101 if (retval != EOK)
102 return retval;
103
104 return EOK;
105}
106
107int iplink_get_mtu(iplink_t *iplink, size_t *rmtu)
108{
109 sysarg_t mtu;
110 async_exch_t *exch = async_exchange_begin(iplink->sess);
111
112 int rc = async_req_0_1(exch, IPLINK_GET_MTU, &mtu);
113 async_exchange_end(exch);
114
115 if (rc != EOK)
116 return rc;
117
118 *rmtu = mtu;
119 return EOK;
120}
121
122int iplink_addr_add(iplink_t *iplink, inet_addr_t *addr)
123{
124 uint32_t addr_addr;
125 int rc = inet_addr_pack(addr, &addr_addr);
126 if (rc != EOK)
127 return rc;
128
129 async_exch_t *exch = async_exchange_begin(iplink->sess);
130 rc = async_req_1_0(exch, IPLINK_ADDR_ADD, (sysarg_t) addr_addr);
131 async_exchange_end(exch);
132
133 return rc;
134}
135
136int iplink_addr_remove(iplink_t *iplink, inet_addr_t *addr)
137{
138 uint32_t addr_addr;
139 int rc = inet_addr_pack(addr, &addr_addr);
140 if (rc != EOK)
141 return rc;
142
143 async_exch_t *exch = async_exchange_begin(iplink->sess);
144 rc = async_req_1_0(exch, IPLINK_ADDR_REMOVE, (sysarg_t) addr_addr);
145 async_exchange_end(exch);
146
147 return rc;
148}
149
150static void iplink_ev_recv(iplink_t *iplink, ipc_callid_t callid,
151 ipc_call_t *call)
152{
153 iplink_sdu_t sdu;
154
155 sdu.lsrc = IPC_GET_ARG1(*call);
156 sdu.ldest = IPC_GET_ARG2(*call);
157
158 int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
159 if (rc != EOK) {
160 async_answer_0(callid, rc);
161 return;
162 }
163
164 rc = iplink->ev_ops->recv(iplink, &sdu);
165 free(sdu.data);
166 async_answer_0(callid, rc);
167}
168
169static void iplink_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
170{
171 iplink_t *iplink = (iplink_t *)arg;
172
173 while (true) {
174 ipc_call_t call;
175 ipc_callid_t callid = async_get_call(&call);
176
177 if (!IPC_GET_IMETHOD(call)) {
178 /* TODO: Handle hangup */
179 return;
180 }
181
182 switch (IPC_GET_IMETHOD(call)) {
183 case IPLINK_EV_RECV:
184 iplink_ev_recv(iplink, callid, &call);
185 break;
186 default:
187 async_answer_0(callid, ENOTSUP);
188 }
189 }
190}
191
192/** @}
193 */
Note: See TracBrowser for help on using the repository browser.