source: mainline/uspace/srv/net/slip/slip.c@ ac307b2

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

Convert char_dev_iface users to chardev.

  • Property mode set to 100644
File size: 10.1 KB
Line 
1/*
2 * Copyright (c) 2013 Jakub Jermar
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 slip
30 * @{
31 */
32/**
33 * @file
34 * @brief IP over serial line IP link provider.
35 */
36
37#include <stdio.h>
38#include <stdint.h>
39#include <loc.h>
40#include <inet/addr.h>
41#include <inet/iplink_srv.h>
42#include <io/chardev.h>
43#include <io/log.h>
44#include <errno.h>
45#include <task.h>
46
47#define NAME "slip"
48#define CAT_IPLINK "iplink"
49
50#define SLIP_MTU 1006 /* as per RFC 1055 */
51
52#define SLIP_END 0300
53#define SLIP_ESC 0333
54#define SLIP_ESC_END 0334
55#define SLIP_ESC_ESC 0335
56
57static int slip_open(iplink_srv_t *);
58static int slip_close(iplink_srv_t *);
59static int slip_send(iplink_srv_t *, iplink_sdu_t *);
60static int slip_send6(iplink_srv_t *, iplink_sdu6_t *);
61static int slip_get_mtu(iplink_srv_t *, size_t *);
62static int slip_get_mac48(iplink_srv_t *, addr48_t *);
63static int slip_addr_add(iplink_srv_t *, inet_addr_t *);
64static int slip_addr_remove(iplink_srv_t *, inet_addr_t *);
65
66static iplink_srv_t slip_iplink;
67
68static iplink_ops_t slip_iplink_ops = {
69 .open = slip_open,
70 .close = slip_close,
71 .send = slip_send,
72 .send6 = slip_send6,
73 .get_mtu = slip_get_mtu,
74 .get_mac48 = slip_get_mac48,
75 .addr_add = slip_addr_add,
76 .addr_remove = slip_addr_remove
77};
78
79static uint8_t slip_send_buf[SLIP_MTU + 2];
80static size_t slip_send_pending;
81
82static uint8_t slip_recv_buf[SLIP_MTU + 2];
83static size_t slip_recv_pending;
84static size_t slip_recv_read;
85
86int slip_open(iplink_srv_t *srv)
87{
88 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_open()");
89 return EOK;
90}
91
92int slip_close(iplink_srv_t *srv)
93{
94 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_close()");
95 return EOK;
96}
97
98static void write_flush(chardev_t *chardev)
99{
100 size_t written = 0;
101
102 while (slip_send_pending > 0) {
103 int rc;
104 size_t nwr;
105
106 rc = chardev_write(chardev, &slip_send_buf[written],
107 slip_send_pending, &nwr);
108 if (rc != EOK) {
109 log_msg(LOG_DEFAULT, LVL_ERROR,
110 "chardev_write() returned %d", rc);
111 slip_send_pending = 0;
112 break;
113 }
114 written += nwr;
115 slip_send_pending -= nwr;
116 }
117}
118
119static void write_buffered(chardev_t *chardev, uint8_t ch)
120{
121 if (slip_send_pending == sizeof(slip_send_buf))
122 write_flush(chardev);
123 slip_send_buf[slip_send_pending++] = ch;
124}
125
126int slip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
127{
128 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send()");
129
130 chardev_t *chardev = (chardev_t *) srv->arg;
131 uint8_t *data = sdu->data;
132
133 /*
134 * Strictly speaking, this is not prescribed by the RFC, but the RFC
135 * suggests to start with sending a SLIP_END byte as a synchronization
136 * measure for dealing with previous possible noise on the line.
137 */
138 write_buffered(chardev, SLIP_END);
139
140 for (size_t i = 0; i < sdu->size; i++) {
141 switch (data[i]) {
142 case SLIP_END:
143 write_buffered(chardev, SLIP_ESC);
144 write_buffered(chardev, SLIP_ESC_END);
145 break;
146 case SLIP_ESC:
147 write_buffered(chardev, SLIP_ESC);
148 write_buffered(chardev, SLIP_ESC_ESC);
149 break;
150 default:
151 write_buffered(chardev, data[i]);
152 break;
153 }
154 }
155
156 write_buffered(chardev, SLIP_END);
157 write_flush(chardev);
158
159 return EOK;
160}
161
162int slip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
163{
164 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send6()");
165
166 return ENOTSUP;
167}
168
169int slip_get_mtu(iplink_srv_t *srv, size_t *mtu)
170{
171 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_get_mtu()");
172 *mtu = SLIP_MTU;
173 return EOK;
174}
175
176int slip_get_mac48(iplink_srv_t *src, addr48_t *mac)
177{
178 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_get_mac48()");
179 return ENOTSUP;
180}
181
182int slip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
183{
184 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_add()");
185 return EOK;
186}
187
188int slip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
189{
190 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_remove()");
191 return EOK;
192}
193
194static void usage(void)
195{
196 printf("Usage: " NAME " <service-name> <link-name>\n");
197}
198
199static void slip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
200{
201 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_client_conn()");
202 iplink_conn(iid, icall, &slip_iplink);
203}
204
205static uint8_t read_buffered(chardev_t *chardev)
206{
207 while (slip_recv_pending == 0) {
208 int rc;
209 size_t nread;
210
211 rc = chardev_read(chardev, slip_recv_buf,
212 sizeof(slip_recv_buf), &nread);
213 if (rc != EOK) {
214 log_msg(LOG_DEFAULT, LVL_ERROR,
215 "char_dev_read() returned %d", rc);
216 }
217
218 if (nread == 0)
219 return SLIP_END;
220
221 slip_recv_pending = nread;
222 slip_recv_read = 0;
223 }
224 slip_recv_pending--;
225 return slip_recv_buf[slip_recv_read++];
226}
227
228static int slip_recv_fibril(void *arg)
229{
230 chardev_t *chardev = (chardev_t *) arg;
231 static uint8_t recv_final[SLIP_MTU];
232 iplink_recv_sdu_t sdu;
233 uint8_t ch;
234 int rc;
235
236 sdu.data = recv_final;
237
238 while (true) {
239 for (sdu.size = 0; sdu.size < sizeof(recv_final); /**/) {
240 ch = read_buffered(chardev);
241 switch (ch) {
242 case SLIP_END:
243 if (sdu.size == 0) {
244 /*
245 * Discard the empty SLIP datagram.
246 */
247 break;
248 }
249 goto pass;
250
251 case SLIP_ESC:
252 ch = read_buffered(chardev);
253 if (ch == SLIP_ESC_END) {
254 recv_final[sdu.size++] = SLIP_END;
255 break;
256 } else if (ch == SLIP_ESC_ESC) {
257 recv_final[sdu.size++] = SLIP_ESC;
258 break;
259 }
260
261 /*
262 * The RFC suggests to simply insert the wrongly
263 * escaped character into the packet so we fall
264 * through.
265 */
266 /* Fallthrough */
267
268 default:
269 recv_final[sdu.size++] = ch;
270 break;
271 }
272
273 }
274
275 /*
276 * We have reached the limit of our MTU. Regardless of whether
277 * the datagram is properly ended with SLIP_END, pass it along.
278 * If the next character is really SLIP_END, nothing
279 * catastrophic happens. The algorithm will just see an
280 * artificially empty SLIP datagram and life will go on.
281 */
282
283pass:
284 rc = iplink_ev_recv(&slip_iplink, &sdu, ip_v4);
285 if (rc != EOK) {
286 log_msg(LOG_DEFAULT, LVL_ERROR,
287 "iplink_ev_recv() returned %d", rc);
288 }
289 }
290
291 return 0;
292}
293
294static int slip_init(const char *svcstr, const char *linkstr)
295{
296 service_id_t svcid;
297 service_id_t linksid;
298 category_id_t iplinkcid;
299 async_sess_t *sess_in = NULL;
300 async_sess_t *sess_out = NULL;
301 chardev_t *chardev_in = NULL;
302 chardev_t *chardev_out = NULL;
303 fid_t fid;
304 int rc;
305
306 iplink_srv_init(&slip_iplink);
307 slip_iplink.ops = &slip_iplink_ops;
308
309 async_set_fallback_port_handler(slip_client_conn, NULL);
310
311 rc = loc_server_register(NAME);
312 if (rc != EOK) {
313 log_msg(LOG_DEFAULT, LVL_ERROR,
314 "Failed registering server.");
315 return rc;
316 }
317
318 rc = loc_service_get_id(svcstr, &svcid, 0);
319 if (rc != EOK) {
320 log_msg(LOG_DEFAULT, LVL_ERROR,
321 "Failed getting ID for service %s", svcstr);
322 return rc;
323 }
324
325 rc = loc_category_get_id(CAT_IPLINK, &iplinkcid, 0);
326 if (rc != EOK) {
327 log_msg(LOG_DEFAULT, LVL_ERROR,
328 "Failed to get category ID for %s",
329 CAT_IPLINK);
330 return rc;
331 }
332
333 /*
334 * Create two sessions to allow to both read and write from the
335 * char_dev at the same time.
336 */
337 sess_out = loc_service_connect(svcid, INTERFACE_DDF, 0);
338 if (!sess_out) {
339 log_msg(LOG_DEFAULT, LVL_ERROR,
340 "Failed to connect to service %s (ID=%d)",
341 svcstr, (int) svcid);
342 return ENOENT;
343 }
344
345 rc = chardev_open(sess_out, &chardev_out);
346 if (rc != EOK) {
347 log_msg(LOG_DEFAULT, LVL_ERROR,
348 "Failed opening character device.");
349 return ENOENT;
350 }
351
352 slip_iplink.arg = chardev_out;
353
354 sess_in = loc_service_connect(svcid, INTERFACE_DDF, 0);
355 if (!sess_in) {
356 log_msg(LOG_DEFAULT, LVL_ERROR,
357 "Failed to connect to service %s (ID=%d)",
358 svcstr, (int) svcid);
359 rc = ENOENT;
360 goto fail;
361 }
362
363 rc = chardev_open(sess_in, &chardev_in);
364 if (rc != EOK) {
365 log_msg(LOG_DEFAULT, LVL_ERROR,
366 "Failed opening character device.");
367 return ENOENT;
368 }
369
370 rc = loc_service_register(linkstr, &linksid);
371 if (rc != EOK) {
372 log_msg(LOG_DEFAULT, LVL_ERROR,
373 "Failed to register service %s",
374 linkstr);
375 goto fail;
376 }
377
378 rc = loc_service_add_to_cat(linksid, iplinkcid);
379 if (rc != EOK) {
380 log_msg(LOG_DEFAULT, LVL_ERROR,
381 "Failed to add service %d (%s) to category %d (%s).",
382 (int) linksid, linkstr, (int) iplinkcid, CAT_IPLINK);
383 goto fail;
384 }
385
386 fid = fibril_create(slip_recv_fibril, chardev_in);
387 if (!fid) {
388 log_msg(LOG_DEFAULT, LVL_ERROR,
389 "Failed to create receive fibril.");
390 rc = ENOENT;
391 goto fail;
392 }
393 fibril_add_ready(fid);
394
395 return EOK;
396
397fail:
398 chardev_close(chardev_out);
399 if (sess_out)
400 async_hangup(sess_out);
401 chardev_close(chardev_in);
402 if (sess_in)
403 async_hangup(sess_in);
404
405 /*
406 * We assume that our registration at the location service will be
407 * cleaned up automatically as the service (i.e. this task) terminates.
408 */
409
410 return rc;
411}
412
413int main(int argc, char *argv[])
414{
415 int rc;
416
417 printf(NAME ": IP over serial line service\n");
418
419 if (argc != 3) {
420 usage();
421 return EINVAL;
422 }
423
424 rc = log_init(NAME);
425 if (rc != EOK) {
426 printf(NAME ": failed to initialize log\n");
427 return rc;
428 }
429
430 rc = slip_init(argv[1], argv[2]);
431 if (rc != EOK)
432 return 1;
433
434 printf(NAME ": Accepting connections.\n");
435 task_retval(0);
436 async_manager();
437
438 /* not reached */
439 return 0;
440}
441
442/** @}
443 */
Note: See TracBrowser for help on using the repository browser.