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
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>
[2093fbe]39#include <loc.h>
[02a09ed]40#include <inet/addr.h>
[2093fbe]41#include <inet/iplink_srv.h>
[74017ce]42#include <io/chardev.h>
[2093fbe]43#include <io/log.h>
44#include <errno.h>
[1c635d6]45#include <task.h>
[2093fbe]46
47#define NAME "slip"
48#define CAT_IPLINK "iplink"
49
50#define SLIP_MTU 1006 /* as per RFC 1055 */
51
[7b64cf0]52#define SLIP_END 0300
53#define SLIP_ESC 0333
[02a09ed]54#define SLIP_ESC_END 0334
[7b64cf0]55#define SLIP_ESC_ESC 0335
56
[2093fbe]57static int slip_open(iplink_srv_t *);
58static int slip_close(iplink_srv_t *);
[02a09ed]59static int slip_send(iplink_srv_t *, iplink_sdu_t *);
[a17356fd]60static int slip_send6(iplink_srv_t *, iplink_sdu6_t *);
[2093fbe]61static int slip_get_mtu(iplink_srv_t *, size_t *);
[a17356fd]62static int slip_get_mac48(iplink_srv_t *, addr48_t *);
[02a09ed]63static int slip_addr_add(iplink_srv_t *, inet_addr_t *);
64static int slip_addr_remove(iplink_srv_t *, inet_addr_t *);
[2093fbe]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,
[a17356fd]72 .send6 = slip_send6,
[2093fbe]73 .get_mtu = slip_get_mtu,
[a17356fd]74 .get_mac48 = slip_get_mac48,
[2093fbe]75 .addr_add = slip_addr_add,
76 .addr_remove = slip_addr_remove
77};
78
[7b64cf0]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
[2093fbe]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
[74017ce]98static void write_flush(chardev_t *chardev)
[7b64cf0]99{
100 size_t written = 0;
101
102 while (slip_send_pending > 0) {
[74017ce]103 int rc;
104 size_t nwr;
[7b64cf0]105
[74017ce]106 rc = chardev_write(chardev, &slip_send_buf[written],
107 slip_send_pending, &nwr);
108 if (rc != EOK) {
[7b64cf0]109 log_msg(LOG_DEFAULT, LVL_ERROR,
[74017ce]110 "chardev_write() returned %d", rc);
[7b64cf0]111 slip_send_pending = 0;
112 break;
113 }
[74017ce]114 written += nwr;
115 slip_send_pending -= nwr;
[7b64cf0]116 }
117}
118
[74017ce]119static void write_buffered(chardev_t *chardev, uint8_t ch)
[7b64cf0]120{
121 if (slip_send_pending == sizeof(slip_send_buf))
[74017ce]122 write_flush(chardev);
[7b64cf0]123 slip_send_buf[slip_send_pending++] = ch;
124}
125
[02a09ed]126int slip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
[2093fbe]127{
[a17356fd]128 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send()");
129
[74017ce]130 chardev_t *chardev = (chardev_t *) srv->arg;
[7b64cf0]131 uint8_t *data = sdu->data;
[a17356fd]132
[7b64cf0]133 /*
[a17356fd]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 */
[74017ce]138 write_buffered(chardev, SLIP_END);
[a17356fd]139
140 for (size_t i = 0; i < sdu->size; i++) {
[7b64cf0]141 switch (data[i]) {
142 case SLIP_END:
[74017ce]143 write_buffered(chardev, SLIP_ESC);
144 write_buffered(chardev, SLIP_ESC_END);
[7b64cf0]145 break;
146 case SLIP_ESC:
[74017ce]147 write_buffered(chardev, SLIP_ESC);
148 write_buffered(chardev, SLIP_ESC_ESC);
[7b64cf0]149 break;
150 default:
[74017ce]151 write_buffered(chardev, data[i]);
[7b64cf0]152 break;
153 }
154 }
[a17356fd]155
[74017ce]156 write_buffered(chardev, SLIP_END);
157 write_flush(chardev);
[a17356fd]158
[7b64cf0]159 return EOK;
[2093fbe]160}
161
[a17356fd]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
[2093fbe]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
[a17356fd]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
[02a09ed]182int slip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
[2093fbe]183{
184 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_add()");
185 return EOK;
186}
187
[02a09ed]188int slip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
[2093fbe]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
[74017ce]205static uint8_t read_buffered(chardev_t *chardev)
[7b64cf0]206{
207 while (slip_recv_pending == 0) {
[74017ce]208 int rc;
209 size_t nread;
[7b64cf0]210
[74017ce]211 rc = chardev_read(chardev, slip_recv_buf,
212 sizeof(slip_recv_buf), &nread);
213 if (rc != EOK) {
[7b64cf0]214 log_msg(LOG_DEFAULT, LVL_ERROR,
[74017ce]215 "char_dev_read() returned %d", rc);
[7b64cf0]216 }
[74017ce]217
218 if (nread == 0)
219 return SLIP_END;
220
221 slip_recv_pending = nread;
[7b64cf0]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{
[74017ce]230 chardev_t *chardev = (chardev_t *) arg;
[7b64cf0]231 static uint8_t recv_final[SLIP_MTU];
[02a09ed]232 iplink_recv_sdu_t sdu;
[7b64cf0]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); /**/) {
[74017ce]240 ch = read_buffered(chardev);
[7b64cf0]241 switch (ch) {
242 case SLIP_END:
243 if (sdu.size == 0) {
244 /*
[02a09ed]245 * Discard the empty SLIP datagram.
246 */
[7b64cf0]247 break;
248 }
249 goto pass;
250
251 case SLIP_ESC:
[74017ce]252 ch = read_buffered(chardev);
[7b64cf0]253 if (ch == SLIP_ESC_END) {
254 recv_final[sdu.size++] = SLIP_END;
255 break;
[74017ce]256 } else if (ch == SLIP_ESC_ESC) {
[7b64cf0]257 recv_final[sdu.size++] = SLIP_ESC;
258 break;
259 }
260
261 /*
[e141281]262 * The RFC suggests to simply insert the wrongly
263 * escaped character into the packet so we fall
264 * through.
265 */
[dc12262]266 /* Fallthrough */
[e141281]267
[7b64cf0]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:
[417a2ba1]284 rc = iplink_ev_recv(&slip_iplink, &sdu, ip_v4);
[7b64cf0]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
[2093fbe]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;
[3abf0760]299 async_sess_t *sess_in = NULL;
300 async_sess_t *sess_out = NULL;
[74017ce]301 chardev_t *chardev_in = NULL;
302 chardev_t *chardev_out = NULL;
[7b64cf0]303 fid_t fid;
[2093fbe]304 int rc;
305
306 iplink_srv_init(&slip_iplink);
307 slip_iplink.ops = &slip_iplink_ops;
308
[b688fd8]309 async_set_fallback_port_handler(slip_client_conn, NULL);
[2093fbe]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
[7b64cf0]333 /*
[3abf0760]334 * Create two sessions to allow to both read and write from the
335 * char_dev at the same time.
[7b64cf0]336 */
[f9b2cb4c]337 sess_out = loc_service_connect(svcid, INTERFACE_DDF, 0);
[3abf0760]338 if (!sess_out) {
[2093fbe]339 log_msg(LOG_DEFAULT, LVL_ERROR,
340 "Failed to connect to service %s (ID=%d)",
341 svcstr, (int) svcid);
[b11f6fb]342 return ENOENT;
[2093fbe]343 }
[74017ce]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;
[3abf0760]353
[f9b2cb4c]354 sess_in = loc_service_connect(svcid, INTERFACE_DDF, 0);
[3abf0760]355 if (!sess_in) {
356 log_msg(LOG_DEFAULT, LVL_ERROR,
357 "Failed to connect to service %s (ID=%d)",
358 svcstr, (int) svcid);
[b11f6fb]359 rc = ENOENT;
[3abf0760]360 goto fail;
361 }
[2093fbe]362
[74017ce]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
[2093fbe]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,
[7b64cf0]381 "Failed to add service %d (%s) to category %d (%s).",
[2093fbe]382 (int) linksid, linkstr, (int) iplinkcid, CAT_IPLINK);
383 goto fail;
384 }
385
[74017ce]386 fid = fibril_create(slip_recv_fibril, chardev_in);
[7b64cf0]387 if (!fid) {
388 log_msg(LOG_DEFAULT, LVL_ERROR,
389 "Failed to create receive fibril.");
[b11f6fb]390 rc = ENOENT;
[7b64cf0]391 goto fail;
392 }
393 fibril_add_ready(fid);
394
[2093fbe]395 return EOK;
396
397fail:
[74017ce]398 chardev_close(chardev_out);
[3abf0760]399 if (sess_out)
400 async_hangup(sess_out);
[74017ce]401 chardev_close(chardev_in);
[3abf0760]402 if (sess_in)
403 async_hangup(sess_in);
[2093fbe]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.