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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a53ed3a was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

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