2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: libunimsg/sscop/common.c,v 1.5 2005/05/23 11:46:16 brandt_h Exp $
32 #include <sys/types.h>
33 #include <sys/socket.h>
48 #include <netnatm/unimsg.h>
49 #include <netnatm/saal/sscop.h>
66 #ifndef USE_LIBBEGEMOT
73 * This function get's called from sscop to put out verbose messages
76 sscop_verbose(struct sscop *sscop __unused, void *u __unused,
82 vfprintf(stderr, fmt, ap);
83 fprintf(stderr, "\n");
87 verb(const char *fmt, ...)
92 vfprintf(stderr, fmt, ap);
93 fprintf(stderr, "\n");
98 * Dump a buffer in hex to stderr.
101 dump_buf(const char *w, const u_char *buf, size_t len)
105 fprintf(stderr, "%s %zu: ", w, len);
106 for(i = 0; i < len; i++) {
107 if (i % 4 == 0 && i != 0)
108 fprintf(stderr, " ");
109 fprintf(stderr, "%02x", *buf++);
111 fprintf(stderr, "\n");
115 * SSCOP file descriptor is ready. Allocate and read one message
116 * and dispatch a signal.
119 proto_msgin(int fd __unused)
121 struct uni_msg *m = NULL;
127 if ((size = read(sscop_fd, &flen, 4)) == -1)
128 err(1, "error reading frame hdr");
134 errx(1, "short frame header: %zd", size);
135 if ((m = uni_msg_alloc(flen)) == NULL)
137 for (got = 0; got < flen; got += (size_t)size) {
138 size = read(sscop_fd, m->b_rptr + got, flen - got);
140 err(1, "error reading frame");
148 if ((m = uni_msg_alloc(MAXMSG)) == NULL)
150 if ((size = read(sscop_fd, m->b_rptr, MAXMSG)) == -1)
151 err(1, "error reading message");
157 #ifdef USE_LIBBEGEMOT
158 poll_unregister(sscop_h);
160 evDeselectFD(evctx, sscop_h);
162 (void)close(sscop_fd);
166 VERBOSE(("EOF on sscop file descriptor"));
169 m->b_wptr = m->b_rptr + got;
172 dump_buf("SSCOP INPUT", m->b_rptr, got);
178 * User file descriptor ready - read a message
181 user_msgin(int fd __unused)
183 struct uni_msg *m = NULL;
189 if ((size = read(user_fd, &flen, 4)) == -1)
190 err(1, "error reading frame hdr");
196 errx(1, "short frame header: %zd", size);
197 if ((m = uni_msg_alloc(flen)) == NULL)
199 for (got = 0; got < flen; got++) {
200 size = read(user_fd, m->b_rptr + got, flen - got);
202 err(1, "error reading frame");
211 if ((m = uni_msg_alloc(MAXMSG)) == NULL)
213 if ((size = read(user_fd, m->b_rptr, MAXMSG)) == -1)
214 err(1, "error reading message");
220 #ifdef USE_LIBBEGEMOT
221 poll_unregister(user_h);
223 evDeselectFD(evctx, user_h);
227 VERBOSE(("EOF on user connection"));
230 m->b_wptr = m->b_rptr + size;
236 * Write message to the SSCOP file descriptor.
237 * Here we have a problem: we should have a means to check how much space
238 * we have. If the pipe is full, we could declare the lower layer busy and
239 * drop the message. However, how do we know, when a message will fit?
240 * Selecting for WRITE doesn't help, because it will return even if a single
241 * byte can be written. For this reason, we switch the file descriptor to
242 * blocking mode, and hope everything is fast enough to not timeout us.
243 * Alternatively we could just drop the message. Using kevent would help here.
246 proto_msgout(struct uni_msg *m)
254 if (verbose & 0x0002)
255 dump_buf("send", m->b_rptr, uni_msg_len(m));
256 if (loose > 0 && (sent++ % loose) == loose - 1) {
257 VERBOSE(("loosing message"));
262 flen = uni_msg_len(m);
264 iov[0].iov_len = sscopframe ? 4 : 0;
265 iov[0].iov_base = (caddr_t)&flen;
266 iov[1].iov_len = uni_msg_len(m);
267 iov[1].iov_base = m->b_rptr;
269 if ((fl = fcntl(sscop_fd, F_GETFL, 0)) == -1)
270 err(1, "cannot get flags for sscop fd");
272 if (fcntl(sscop_fd, F_SETFL, fl) == -1)
273 err(1, "cannot set flags for sscop fd");
275 if ((size = writev(sscop_fd, iov, 2)) == -1)
276 err(1, "write sscop");
277 if ((size_t)size != iov[0].iov_len + iov[1].iov_len)
278 err(1, "short sscop write %zu %zu %zd",
279 iov[0].iov_len, iov[1].iov_len, size);
282 if (fcntl(sscop_fd, F_SETFL, fl) == -1)
283 err(1, "cannot restore flags for sscop fd");
289 * output a message to the user
292 user_msgout(struct uni_msg *m)
298 flen = uni_msg_len(m);
300 iov[0].iov_len = useframe ? 4 : 0;
301 iov[0].iov_base = (caddr_t)&flen;
302 iov[1].iov_len = uni_msg_len(m);
303 iov[1].iov_base = m->b_rptr;
305 if ((size = writev(user_out_fd, iov, 2)) == -1)
306 err(1, "write sscop");
307 if ((size_t)size != iov[0].iov_len + iov[1].iov_len)
308 errx(1, "short sscop write");
314 parse_param(struct sscop_param *param, u_int *pmask, int opt, char *arg)
320 param->flags |= SSCOP_ROBUST;
321 *pmask |= SSCOP_SET_ROBUST;
325 param->flags |= SSCOP_POLLREX;
326 *pmask |= SSCOP_SET_POLLREX;
330 val = (u_int)strtoul(optarg, &end, 0);
333 errx(1, "bad number to -W '%s'", optarg);
334 if(val >= (1 << 24) - 1)
335 errx(1, "window too large: 0x%x", val);
337 *pmask |= SSCOP_SET_MR;
341 if((p = strchr(arg, '=')) == NULL)
342 errx(1, "need '=' in argument to -%c", opt);
345 errx(1, "argument to -%c %s empty", opt, arg);
346 val = strtoul(p, &end, 0);
348 errx(1, "bad number in -%c %s=%s", opt, arg, p);
351 if(strcmp(arg, "cc") == 0) {
352 param->timer_cc = val;
353 *pmask |= SSCOP_SET_TCC;
354 } else if(strcmp(arg, "poll") == 0) {
355 param->timer_poll = val;
356 *pmask |= SSCOP_SET_TPOLL;
357 } else if(strcmp(arg, "ka") == 0) {
358 param->timer_keep_alive = val;
359 *pmask |= SSCOP_SET_TKA;
360 } else if(strcmp(arg, "nr") == 0) {
361 param->timer_no_response = val;
362 *pmask |= SSCOP_SET_TNR;
363 } else if(strcmp(arg, "idle") == 0) {
364 param->timer_idle = val;
365 *pmask |= SSCOP_SET_TIDLE;
367 errx(1, "bad timer name '%s'", arg);
372 if(strcmp(arg, "j") == 0) {
374 *pmask |= SSCOP_SET_MAXJ;
375 } else if(strcmp(arg, "k") == 0) {
377 *pmask |= SSCOP_SET_MAXK;
378 } else if(strcmp(arg, "cc") == 0) {
380 *pmask |= SSCOP_SET_MAXCC;
381 } else if(strcmp(arg, "pd") == 0) {
383 *pmask |= SSCOP_SET_MAXPD;
384 } else if(strcmp(arg, "stat") == 0) {
385 param->maxstat = val;
386 *pmask |= SSCOP_SET_MAXSTAT;
388 errx(1, "bad parameter '%s'", arg);
396 #ifdef USE_LIBBEGEMOT
398 tfunc(int tid __unused, void *uap)
401 tfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
402 struct timespec inter __unused)
405 struct timer *t = uap;
415 sscop_start_timer(struct sscop *sscop, void *arg __unused, u_int msec,
416 void (*func)(void *))
419 #ifndef USE_LIBBEGEMOT
423 if ((t = malloc(sizeof(*t))) == NULL)
428 #ifdef USE_LIBBEGEMOT
429 if ((t->id = poll_start_timer(msec, 0, tfunc, t)) == -1)
430 err(1, "cannot start timer");
432 due = evAddTime(evNowTime(),
433 evConsTime((time_t)msec/1000, (long)(msec%1000)*1000));
435 if (evSetTimer(evctx, tfunc, t, due, evConsTime(0, 0), &t->id))
436 err(1, "cannot start timer");
446 sscop_stop_timer(struct sscop *sscop __unused, void *arg __unused, void *tp)
448 struct timer *t = tp;
450 #ifdef USE_LIBBEGEMOT
451 poll_stop_timer(t->id);
453 evClearTimer(evctx, t->id);