]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/socket.h
Rewritten the I/O loop. All socket operations are now safe, meaning that
[thirdparty/bird.git] / lib / socket.h
1 /*
2 * BIRD Socket Interface
3 *
4 * (c) 1998--2004 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_SOCKET_H_
10 #define _BIRD_SOCKET_H_
11
12 #include "lib/resource.h"
13
14 typedef struct birdsock {
15 resource r;
16 pool *pool; /* Pool where incoming connections should be allocated (for SK_xxx_PASSIVE) */
17 int type; /* Socket type */
18 void *data; /* User data */
19 ip_addr saddr, daddr; /* IPA_NONE = unspecified */
20 unsigned sport, dport; /* 0 = unspecified (for IP: protocol type) */
21 int tos; /* TOS and priority, -1 = default */
22 int ttl; /* Time To Live, -1 = default */
23 struct iface *iface; /* Interface; specify this for broad/multicast sockets */
24
25 byte *rbuf, *rpos; /* NULL=allocate automatically */
26 unsigned rbsize;
27 int (*rx_hook)(struct birdsock *, int size); /* NULL=receiving turned off, returns 1 to clear rx buffer */
28
29 byte *tbuf, *tpos; /* NULL=allocate automatically */
30 byte *ttx; /* Internal */
31 unsigned tbsize;
32 void (*tx_hook)(struct birdsock *);
33
34 void (*err_hook)(struct birdsock *, int); /* errno or zero if EOF */
35
36 ip_addr faddr; /* For packet protocols: source of current packet */
37 unsigned fport;
38
39 int fd; /* System-dependent data */
40 node n;
41 void *rbuf_alloc, *tbuf_alloc;
42 } sock;
43
44 sock *sk_new(pool *); /* Allocate new socket */
45 int sk_open(sock *); /* Open socket */
46 int sk_send(sock *, unsigned len); /* Send data, <0=err, >0=ok, 0=sleep */
47 int sk_send_to(sock *, unsigned len, ip_addr to, unsigned port); /* sk_send to given destination */
48 void sk_dump_all(void);
49
50 static inline int
51 sk_send_buffer_empty(sock *sk)
52 {
53 return sk->tbuf == sk->tpos;
54 }
55
56 /*
57 * Socket types SA SP DA DP IF TTL SendTo (?=may, -=must not, *=must)
58 */
59
60 #define SK_TCP_PASSIVE 0 /* ? * - - - ? - */
61 #define SK_TCP_ACTIVE 1 /* ? ? * * - ? - */
62 #define SK_TCP 2
63 #define SK_UDP 3 /* ? ? - - - ? ? */
64 #define SK_UDP_MC 4 /* ? ? * * * * - */
65 #define SK_IP 5 /* ? - - * - ? ? */
66 #define SK_IP_MC 6 /* ? - * * * * - */
67 #define SK_MAGIC 7 /* Internal use by sysdep code */
68 #define SK_UNIX_PASSIVE 8
69 #define SK_UNIX 9
70
71 /*
72 * Multicast sockets are slightly different from the other ones:
73 * If you want to send packets only, just set the destination
74 * address to the corresponding multicast group and iface to
75 * the interface to be used. If you also want receiving, set
76 * source address to the same multicast group as well.
77 */
78
79 #endif