Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / io.c @ e8f73195

History | View | Annotate | Download (13.8 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 "lib/event.h"
24
#include "nest/iface.h"
25

    
26
#include "lib/unix.h"
27

    
28
/*
29
 *        Timers
30
 */
31

    
32
#define NEAR_TIMER_LIMIT 4
33

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

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

    
47
bird_clock_t now;
48

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

    
54
  tm_stop(t);
55
}
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
215
/*
216
 *        Sockets
217
 */
218

    
219
static list sock_list;
220

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
674
#undef ERR
675

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

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

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

    
700
  /* FIXME: Use poll() if available */
701

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

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

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