Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / io.c @ 08c69a77

History | View | Annotate | Download (13.7 KB)

1
/*
2
 *        BIRD Internet Routing Daemon -- Unix I/O
3
 *
4
 *        (c) 1998 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <sys/time.h>
12
#include <sys/types.h>
13
#include <sys/socket.h>
14
#include <sys/fcntl.h>
15
#include <unistd.h>
16
#include <errno.h>
17

    
18
#include "nest/bird.h"
19
#include "lib/lists.h"
20
#include "lib/resource.h"
21
#include "lib/timer.h"
22
#include "lib/socket.h"
23
#include "nest/iface.h"
24

    
25
#include "lib/unix.h"
26

    
27
/*
28
 *        Timers
29
 */
30

    
31
#define NEAR_TIMER_LIMIT 4
32

    
33
#ifdef TIME_T_IS_64BIT
34
#define TIME_INFINITY 0x7fffffffffffffff
35
#else
36
#ifdef TIME_T_IS_SIGNED
37
#define TIME_INFINITY 0x7fffffff
38
#else
39
#define TIME_INFINITY 0xffffffff
40
#endif
41
#endif
42

    
43
static list near_timers, far_timers;
44
static bird_clock_t first_far_timer = TIME_INFINITY;
45

    
46
bird_clock_t now;
47

    
48
static void
49
tm_free(resource *r)
50
{
51
  timer *t = (timer *) r;
52

    
53
  tm_stop(t);
54
}
55

    
56
static void
57
tm_dump(resource *r)
58
{
59
  timer *t = (timer *) r;
60

    
61
  debug("(code %p, data %p, ");
62
  if (t->randomize)
63
    debug("rand %d, ", t->randomize);
64
  if (t->recurrent)
65
    debug("recur %d, ", t->recurrent);
66
  if (t->expires)
67
    debug("expires in %d sec)\n", t->expires - now);
68
  else
69
    debug("inactive)\n");
70
}
71

    
72
static struct resclass tm_class = {
73
  "Timer",
74
  sizeof(timer),
75
  tm_free,
76
  tm_dump
77
};
78

    
79
timer *
80
tm_new(pool *p)
81
{
82
  timer *t = ralloc(p, &tm_class);
83
  t->hook = NULL;
84
  t->data = NULL;
85
  t->randomize = 0;
86
  t->expires = 0;
87
  return t;
88
}
89

    
90
static inline void
91
tm_insert_near(timer *t)
92
{
93
  node *n = HEAD(near_timers);
94

    
95
  while (n->next && (SKIP_BACK(timer, n, n)->expires < t->expires))
96
    n = n->next;
97
  insert_node(&t->n, n->prev);
98
}
99

    
100
void
101
tm_start(timer *t, unsigned after)
102
{
103
  bird_clock_t when;
104

    
105
  if (t->randomize)
106
    after += random() % (t->randomize + 1);
107
  when = now + after;
108
  if (t->expires == when)
109
    return;
110
  if (t->expires)
111
    rem_node(&t->n);
112
  t->expires = when;
113
  if (after <= NEAR_TIMER_LIMIT)
114
    tm_insert_near(t);
115
  else
116
    {
117
      if (!first_far_timer || first_far_timer > when)
118
        first_far_timer = when;
119
      add_tail(&far_timers, &t->n);
120
    }
121
}
122

    
123
void
124
tm_stop(timer *t)
125
{
126
  if (t->expires)
127
    {
128
      rem_node(&t->n);
129
      t->expires = 0;
130
    }
131
}
132

    
133
static void
134
tm_dump_them(char *name, list *l)
135
{
136
  node *n;
137
  timer *t;
138

    
139
  debug("%s timers:\n", name);
140
  WALK_LIST(n, *l)
141
    {
142
      t = SKIP_BACK(timer, n, n);
143
      debug("%p ", t);
144
      tm_dump(&t->r);
145
    }
146
  debug("\n");
147
}
148

    
149
void
150
tm_dump_all(void)
151
{
152
  tm_dump_them("Near", &near_timers);
153
  tm_dump_them("Far", &far_timers);
154
}
155

    
156
static inline time_t
157
tm_first_shot(void)
158
{
159
  time_t x = first_far_timer;
160

    
161
  if (!EMPTY_LIST(near_timers))
162
    {
163
      timer *t = SKIP_BACK(timer, n, HEAD(near_timers));
164
      if (t->expires < x)
165
        x = t->expires;
166
    }
167
  return x;
168
}
169

    
170
static void
171
tm_shot(void)
172
{
173
  timer *t;
174
  node *n, *m;
175

    
176
  if (first_far_timer <= now)
177
    {
178
      bird_clock_t limit = now + NEAR_TIMER_LIMIT;
179
      first_far_timer = TIME_INFINITY;
180
      n = HEAD(far_timers);
181
      while (m = n->next)
182
        {
183
          t = SKIP_BACK(timer, n, n);
184
          if (t->expires <= limit)
185
            {
186
              rem_node(n);
187
              tm_insert_near(t);
188
            }
189
          else if (t->expires < first_far_timer)
190
            first_far_timer = t->expires;
191
          n = m;
192
        }
193
    }
194
  while ((n = HEAD(near_timers)) -> next)
195
    {
196
      int delay;
197
      t = SKIP_BACK(timer, n, n);
198
      if (t->expires > now)
199
        break;
200
      rem_node(n);
201
      delay = t->expires - now;
202
      t->expires = 0;
203
      if (t->recurrent)
204
        {
205
          int i = t->recurrent - delay;
206
          if (i < 0)
207
            i = 0;
208
          tm_start(t, i);
209
        }
210
      t->hook(t);
211
    }
212
}
213

    
214
/*
215
 *        Sockets
216
 */
217

    
218
static list sock_list;
219

    
220
static void
221
sk_free(resource *r)
222
{
223
  sock *s = (sock *) r;
224

    
225
  if (s->fd >= 0)
226
    rem_node(&s->n);
227
}
228

    
229
static void
230
sk_dump(resource *r)
231
{
232
  sock *s = (sock *) r;
233
  static char *sk_type_names[] = { "TCP<", "TCP>", "TCP", "UDP", "UDP/MC", "IP", "IP/MC" };
234

    
235
  debug("(%s, ud=%p, sa=%08x, sp=%d, da=%08x, dp=%d, tos=%d, ttl=%d, if=%s)\n",
236
        sk_type_names[s->type],
237
        s->data,
238
        s->saddr,
239
        s->sport,
240
        s->daddr,
241
        s->dport,
242
        s->tos,
243
        s->ttl,
244
        s->iface ? s->iface->name : "none");
245
}
246

    
247
static struct resclass sk_class = {
248
  "Socket",
249
  sizeof(sock),
250
  sk_free,
251
  sk_dump
252
};
253

    
254
sock *
255
sk_new(pool *p)
256
{
257
  sock *s = ralloc(p, &sk_class);
258
  s->pool = p;
259
  s->data = NULL;
260
  s->saddr = s->daddr = IPA_NONE;
261
  s->sport = s->dport = 0;
262
  s->tos = s->ttl = -1;
263
  s->iface = NULL;
264
  s->rbuf = NULL;
265
  s->rx_hook = NULL;
266
  s->rbsize = 0;
267
  s->tbuf = NULL;
268
  s->tx_hook = NULL;
269
  s->tbsize = 0;
270
  s->err_hook = NULL;
271
  s->fd = -1;
272
  return s;
273
}
274

    
275
#define ERR(x) do { err = x; goto bad; } while(0)
276

    
277
static inline void
278
set_inaddr(struct in_addr *ia, ip_addr a)
279
{
280
  a = ipa_hton(a);
281
  memcpy(&ia->s_addr, &a, sizeof(a));
282
}
283

    
284
void
285
fill_in_sockaddr(struct sockaddr_in *sa, ip_addr a, unsigned port)
286
{
287
  sa->sin_family = AF_INET;
288
  sa->sin_port = htons(port);
289
  set_inaddr(&sa->sin_addr, a);
290
}
291

    
292
void
293
get_sockaddr(struct sockaddr_in *sa, ip_addr *a, unsigned *port)
294
{
295
  if (sa->sin_family != AF_INET)
296
    bug("get_sockaddr called for wrong address family");
297
  if (port)
298
    *port = ntohs(sa->sin_port);
299
  memcpy(a, &sa->sin_addr.s_addr, sizeof(*a));
300
  *a = ipa_ntoh(*a);
301
}
302

    
303
static char *
304
sk_setup(sock *s)
305
{
306
  int fd = s->fd;
307
  int one = 1;
308
  char *err;
309

    
310
  if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
311
    ERR("fcntl(O_NONBLOCK)");
312
  if ((s->tos >= 0) && setsockopt(fd, SOL_IP, IP_TOS, &s->tos, sizeof(s->tos)) < 0)
313
    ERR("IP_TOS");
314
  if (s->ttl >= 0)
315
    {
316
      if (setsockopt(fd, SOL_IP, IP_TTL, &s->ttl, sizeof(s->ttl)) < 0)
317
        ERR("IP_TTL");
318
      if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &one, sizeof(one)) < 0)
319
        ERR("SO_DONTROUTE");
320
    }
321
#ifdef IP_PMTUDISC
322
  if (s->type != SK_TCP_PASSIVE && s->type != SK_TCP_ACTIVE)
323
    {
324
      int dont = IP_PMTUDISC_DONT;
325
      if (setsockopt(fd, SOL_IP, IP_PMTUDISC, &dont, sizeof(dont)) < 0)
326
        ERR("IP_PMTUDISC");
327
    }
328
#endif
329
  /* FIXME: Set send/receive buffers? */
330
  /* FIXME: Set keepalive for TCP connections? */
331
  err = NULL;
332
bad:
333
  return err;
334
}
335

    
336
static void
337
sk_alloc_bufs(sock *s)
338
{
339
  if (!s->rbuf && s->rbsize)
340
    s->rbuf = mb_alloc(s->pool, s->rbsize);
341
  s->rpos = s->rbuf;
342
  if (!s->tbuf && s->tbsize)
343
    s->tbuf = mb_alloc(s->pool, s->tbsize);
344
  s->tpos = s->ttx = s->tbuf;
345
}
346

    
347
void
348
sk_tcp_connected(sock *s)
349
{
350
  s->rx_hook(s, 0);
351
  s->type = SK_TCP;
352
  sk_alloc_bufs(s);
353
}
354

    
355
int
356
sk_open(sock *s)
357
{
358
  int fd, e;
359
  struct sockaddr_in sa;
360
  int zero = 0;
361
  int one = 1;
362
  int type = s->type;
363
  int has_src = ipa_nonzero(s->saddr) || s->sport;
364
  int has_dest = ipa_nonzero(s->daddr);
365
  char *err;
366

    
367
  switch (type)
368
    {
369
    case SK_TCP_ACTIVE:
370
    case SK_TCP_PASSIVE:
371
      fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
372
      break;
373
    case SK_UDP:
374
    case SK_UDP_MC:
375
      fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
376
      break;
377
    case SK_IP:
378
    case SK_IP_MC:
379
      fd = socket(PF_INET, SOCK_RAW, s->dport);
380
      break;
381
    default:
382
      bug("sk_open() called for invalid sock type %d", s->type);
383
    }
384
  if (fd < 0)
385
    die("sk_open: socket: %m");
386
  s->fd = fd;
387

    
388
  if (err = sk_setup(s))
389
    goto bad;
390
  switch (type)
391
    {
392
    case SK_UDP:
393
    case SK_IP:
394
      if (s->iface)                        /* It's a broadcast socket */
395
        if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0)
396
          ERR("SO_BROADCAST");
397
      break;
398
    case SK_UDP_MC:
399
    case SK_IP_MC:
400
      {
401
#ifdef HAVE_IP_MREQN
402
        struct ip_mreqn mreq;
403
#define mreq_add mreq
404
        mreq.imr_ifindex = s->iface->index;
405
        if (has_src)
406
          set_inaddr(&mreq.imr_address, s->saddr);
407
        else
408
          set_inaddr(&mreq.imr_address, s->iface->ifa->ip);
409
#else
410
        struct in_addr mreq;
411
        struct ip_mreq mreq_add;
412
        if (has_src)
413
          set_inaddr(&mreq, s->saddr);
414
        else
415
          set_inaddr(&mreq, s->iface->ip);
416
        memcpy(&mreq_add.imr_interface, &mreq, sizeof(struct in_addr));
417
#endif
418
        set_inaddr(&mreq_add.imr_multiaddr, s->daddr);
419
        if (has_dest)
420
          {
421
            if (
422
#ifdef IP_DEFAULT_MULTICAST_TTL
423
                s->ttl != IP_DEFAULT_MULTICAST_TTL &&
424
#endif
425
                setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0)
426
              ERR("IP_MULTICAST_TTL");
427
            if (
428
#ifdef IP_DEFAULT_MULTICAST_LOOP
429
                IP_DEFAULT_MULTICAST_LOOP &&
430
#endif
431
                setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
432
              ERR("IP_MULTICAST_LOOP");
433
            if (setsockopt(fd, SOL_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq)) < 0)
434
              ERR("IP_MULTICAST_IF");
435
        }
436
      if (has_src && setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq_add, sizeof(mreq_add)) < 0)
437
        ERR("IP_ADD_MEMBERSHIP");
438
      break;
439
      }
440
    }
441
  if (has_src)
442
    {
443
      int port;
444

    
445
      if (type == SK_IP || type == SK_IP_MC)
446
        port = 0;
447
      else
448
        {
449
          port = s->sport;
450
          if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
451
            ERR("SO_REUSEADDR");
452
        }
453
      fill_in_sockaddr(&sa, s->saddr, port);
454
      if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
455
        ERR("bind");
456
    }
457
  fill_in_sockaddr(&sa, s->daddr, s->dport);
458
  switch (type)
459
    {
460
    case SK_TCP_ACTIVE:
461
      if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) >= 0)
462
        sk_tcp_connected(s);
463
      else if (errno != EINTR && errno != EAGAIN)
464
        ERR("connect");
465
      break;
466
    case SK_TCP_PASSIVE:
467
      if (listen(fd, 8))
468
        ERR("listen");
469
      break;
470
    }
471

    
472
  sk_alloc_bufs(s);
473
  add_tail(&sock_list, &s->n);
474
  return 0;
475

    
476
bad:
477
  log(L_ERR "sk_open: %s: %m", err);
478
  close(fd);
479
  s->fd = -1;
480
  return -1;
481
}
482

    
483
static int
484
sk_maybe_write(sock *s)
485
{
486
  int e;
487

    
488
  switch (s->type)
489
    {
490
    case SK_TCP:
491
      while (s->ttx != s->tpos)
492
        {
493
          e = write(s->fd, s->ttx, s->tpos - s->ttx);
494
          if (e < 0)
495
            {
496
              if (errno != EINTR && errno != EAGAIN)
497
                {
498
                  log(L_ERR "write: %m");
499
                  s->err_hook(s, errno);
500
                  return -1;
501
                }
502
              return 0;
503
            }
504
          s->ttx += e;
505
        }
506
      s->ttx = s->tpos = s->tbuf;
507
      return 1;
508
    case SK_UDP:
509
    case SK_UDP_MC:
510
    case SK_IP:
511
    case SK_IP_MC:
512
      {
513
        struct sockaddr_in sa;
514

    
515
        if (s->tbuf == s->tpos)
516
          return 1;
517
        fill_in_sockaddr(&sa, s->faddr, s->fport);
518
        e = sendto(s->fd, s->tbuf, s->tpos - s->tbuf, 0, (struct sockaddr *) &sa, sizeof(sa));
519
        if (e < 0)
520
          {
521
            if (errno != EINTR && errno != EAGAIN)
522
              {
523
                log(L_ERR "sendto: %m");
524
                s->err_hook(s, errno);
525
                return -1;
526
              }
527
            return 0;
528
          }
529
        s->tpos = s->tbuf;
530
        return 1;
531
      }
532
    default:
533
      bug("sk_maybe_write: unknown socket type %d", s->type);
534
    }
535
}
536

    
537
int
538
sk_send(sock *s, unsigned len)
539
{
540
  s->faddr = s->daddr;
541
  s->fport = s->dport;
542
  s->ttx = s->tbuf;
543
  s->tpos = s->tbuf + len;
544
  return sk_maybe_write(s);
545
}
546

    
547
int
548
sk_send_to(sock *s, unsigned len, ip_addr addr, unsigned port)
549
{
550
  s->faddr = addr;
551
  s->fport = port;
552
  s->ttx = s->tbuf;
553
  s->tpos = s->tbuf + len;
554
  return sk_maybe_write(s);
555
}
556

    
557
static int
558
sk_read(sock *s)
559
{
560
  switch (s->type)
561
    {
562
    case SK_TCP_ACTIVE:
563
      {
564
        struct sockaddr_in sa;
565
        fill_in_sockaddr(&sa, s->daddr, s->dport);
566
        if (connect(s->fd, (struct sockaddr *) &sa, sizeof(sa)) >= 0)
567
          sk_tcp_connected(s);
568
        else if (errno != EINTR && errno != EAGAIN)
569
          {
570
            log(L_ERR "connect: %m");
571
            s->err_hook(s, errno);
572
          }
573
        return 0;
574
      }
575
    case SK_TCP_PASSIVE:
576
      {
577
        struct sockaddr_in sa;
578
        int al = sizeof(sa);
579
        int fd = accept(s->fd, (struct sockaddr *) &sa, &al);
580
        if (fd >= 0)
581
          {
582
            sock *t = sk_new(s->pool);
583
            char *err;
584
            t->type = SK_TCP;
585
            t->fd = fd;
586
            add_tail(&sock_list, &t->n);
587
            s->rx_hook(t, 0);
588
            if (err = sk_setup(t))
589
              {
590
                log(L_ERR "Incoming connection: %s: %m", err);
591
                s->err_hook(s, errno);
592
                return 0;
593
              }
594
            sk_alloc_bufs(t);
595
            return 1;
596
          }
597
        else if (errno != EINTR && errno != EAGAIN)
598
          {
599
            log(L_ERR "accept: %m");
600
            s->err_hook(s, errno);
601
          }
602
        return 0;
603
      }
604
    case SK_TCP:
605
      {
606
        int c = read(s->fd, s->rpos, s->rbuf + s->rbsize - s->rpos);
607

    
608
        if (c < 0)
609
          {
610
            if (errno != EINTR && errno != EAGAIN)
611
              {
612
                log(L_ERR "read: %m");
613
                s->err_hook(s, errno);
614
              }
615
          }
616
        else if (!c)
617
          s->err_hook(s, 0);
618
        else
619
          {
620
            s->rpos += c;
621
            if (s->rx_hook(s, s->rpos - s->rbuf))
622
              s->rpos = s->rbuf;
623
            return 1;
624
          }
625
        return 0;
626
      }
627
    default:
628
      {
629
        struct sockaddr_in sa;
630
        int al = sizeof(sa);
631
        int e = recvfrom(s->fd, s->rbuf, s->rbsize, 0, (struct sockaddr *) &sa, &al);
632

    
633
        if (e < 0)
634
          {
635
            if (errno != EINTR && errno != EAGAIN)
636
              {
637
                log(L_ERR "recvfrom: %m");
638
                s->err_hook(s, errno);
639
              }
640
            return 0;
641
          }
642
        s->rpos = s->rbuf + e;
643
        get_sockaddr(&sa, &s->faddr, &s->fport);
644
        s->rx_hook(s, e);
645
        return 1;
646
      }
647
    }
648
}
649

    
650
static void
651
sk_write(sock *s)
652
{
653
  while (s->ttx != s->tbuf && sk_maybe_write(s) > 0)
654
    s->tx_hook(s);
655
}
656

    
657
void
658
sk_dump_all(void)
659
{
660
  node *n;
661
  sock *s;
662

    
663
  debug("Open sockets:\n");
664
  WALK_LIST(n, sock_list)
665
    {
666
      s = SKIP_BACK(sock, n, n);
667
      debug("%p ", s);
668
      sk_dump(&s->r);
669
    }
670
  debug("\n");
671
}
672

    
673
#undef ERR
674

    
675
/*
676
 *        Main I/O Loop
677
 */
678

    
679
void
680
io_init(void)
681
{
682
  init_list(&near_timers);
683
  init_list(&far_timers);
684
  init_list(&sock_list);
685
  now = time(NULL);
686
}
687

    
688
void
689
io_loop(void)
690
{
691
  fd_set rd, wr;
692
  struct timeval timo;
693
  time_t tout;
694
  int hi;
695
  sock *s;
696
  node *n;
697

    
698
  /* FIXME: Use poll() if available */
699

    
700
  FD_ZERO(&rd);
701
  FD_ZERO(&wr);
702
  for(;;)
703
    {
704
      now = time(NULL);
705
      tout = tm_first_shot();
706
      if (tout <= now)
707
        {
708
          tm_shot();
709
          continue;
710
        }
711
      else
712
        {
713
          timo.tv_sec = tout - now;
714
          timo.tv_usec = 0;
715
        }
716

    
717
      hi = 0;
718
      WALK_LIST(n, sock_list)
719
        {
720
          s = SKIP_BACK(sock, n, n);
721
          if (s->rx_hook)
722
            {
723
              FD_SET(s->fd, &rd);
724
              if (s->fd > hi)
725
                hi = s->fd;
726
            }
727
          if (s->tx_hook && s->ttx != s->tpos)
728
            {
729
              FD_SET(s->fd, &wr);
730
              if (s->fd > hi)
731
                hi = s->fd;
732
            }
733
        }
734

    
735
      hi = select(hi+1, &rd, &wr, NULL, &timo);
736
      if (hi < 0)
737
        {
738
          if (errno == EINTR || errno == EAGAIN)
739
            continue;
740
          die("select: %m");
741
        }
742
      if (hi)
743
        {
744
          WALK_LIST(n, sock_list)
745
            {
746
              s = SKIP_BACK(sock, n, n);
747
              if (FD_ISSET(s->fd, &rd))
748
                {
749
                  FD_CLR(s->fd, &rd);
750
                  while (sk_read(s))
751
                    ;
752
                }
753
              if (FD_ISSET(s->fd, &wr))
754
                {
755
                  FD_CLR(s->fd, &wr);
756
                  sk_write(s);
757
                }
758
            }
759
        }
760
    }
761
}