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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ddb1922 was ddb1922, checked in by Jakub Jermar <jakub@…>, 12 years ago

Adapt the SLIP server to the new network address infrastructure.

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