Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/types/protocols.h |
| 3 | This file defines the structures used by generic network protocols. |
| 4 | |
Willy Tarreau | 2c9f5b1 | 2009-08-16 19:12:36 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 6 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _TYPES_PROTOCOLS_H |
| 23 | #define _TYPES_PROTOCOLS_H |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/socket.h> |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/un.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 29 | |
| 30 | #include <common/config.h> |
| 31 | #include <common/mini-clist.h> |
Willy Tarreau | 45cb4fb | 2009-10-26 21:10:04 +0100 | [diff] [blame] | 32 | #include <eb32tree.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 33 | |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 34 | #include <types/counters.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 35 | #include <types/task.h> |
| 36 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 37 | /* max length of a protcol name, including trailing zero */ |
| 38 | #define PROTO_NAME_LEN 16 |
| 39 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 40 | /* listener state */ |
| 41 | #define LI_NEW 0 /* not initialized yet */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 42 | #define LI_INIT 1 /* all parameters filled in, but not assigned yet */ |
| 43 | #define LI_ASSIGNED 2 /* assigned to the protocol, but not listening yet */ |
| 44 | #define LI_LISTEN 3 /* started, listening but not enabled */ |
| 45 | #define LI_READY 4 /* started, listening and enabled */ |
| 46 | #define LI_FULL 5 /* reached its connection limit */ |
| 47 | |
| 48 | /* Listener transitions |
| 49 | * calloc() set() add_listener() bind() |
| 50 | * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN |
| 51 | * <------- <---- <---------- <----- |
| 52 | * free() bzero() del_listener() unbind() |
| 53 | * |
| 54 | * The file descriptor is valid only during these three states : |
| 55 | * |
| 56 | * disable() |
| 57 | * LISTEN <------------ READY |
| 58 | * A| ------------> |A |
| 59 | * || !max & enable() || |
| 60 | * || || |
| 61 | * || max || |
| 62 | * || max & enable() V| !max |
| 63 | * |+---------------> FULL |
| 64 | * +----------------- |
| 65 | * disable() |
| 66 | * |
| 67 | */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 69 | /* listener socket options */ |
| 70 | #define LI_O_NONE 0x0000 |
| 71 | #define LI_O_NOLINGER 0x0001 /* disable linger on this socket */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 72 | #define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses */ |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 73 | #define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */ |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 74 | #define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */ |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 75 | #define LI_O_TCP_RULES 0x0010 /* run TCP rules checks on the incoming connection */ |
Willy Tarreau | de3041d | 2010-05-31 10:56:17 +0200 | [diff] [blame] | 76 | #define LI_O_CHK_MONNET 0x0020 /* check the source against a monitor-net rule */ |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 78 | /* The listener will be directly referenced by the fdtab[] which holds its |
| 79 | * socket. The listener provides the protocol-specific accept() function to |
| 80 | * the fdtab. |
| 81 | */ |
| 82 | struct listener { |
| 83 | int fd; /* the listen socket */ |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 84 | char *name; /* */ |
| 85 | int luid; /* listener universally unique ID, used for SNMP */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 86 | int state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */ |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 87 | int options; /* socket options : LI_O_* */ |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 88 | struct licounters *counters; /* statistics counters */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 89 | struct sockaddr_storage addr; /* the address we listen to */ |
| 90 | struct protocol *proto; /* protocol this listener belongs to */ |
| 91 | int nbconn; /* current number of connections on this listener */ |
| 92 | int maxconn; /* maximum connections allowed on this listener */ |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 93 | unsigned int backlog; /* if set, listen backlog */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 94 | struct listener *next; /* next address for the same proxy, or NULL */ |
| 95 | struct list proto_list; /* list in the protocol header */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 96 | int (*accept)(struct listener *l, int fd, struct sockaddr_storage *addr); /* upper layer's accept() */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 97 | struct task * (*handler)(struct task *t); /* protocol handler. It is a task */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 98 | int *timeout; /* pointer to client-side timeout */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 99 | struct proxy *frontend; /* the frontend this listener belongs to, or NULL */ |
Willy Tarreau | 3bc1377 | 2008-12-07 11:50:35 +0100 | [diff] [blame] | 100 | unsigned int analysers; /* bitmap of required protocol analysers */ |
Willy Tarreau | 2c9f5b1 | 2009-08-16 19:12:36 +0200 | [diff] [blame] | 101 | int nice; /* nice value to assign to the instanciated tasks */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 102 | union { /* protocol-dependant access restrictions */ |
| 103 | struct { /* UNIX socket permissions */ |
| 104 | uid_t uid; /* -1 to leave unchanged */ |
| 105 | gid_t gid; /* -1 to leave unchanged */ |
| 106 | mode_t mode; /* 0 to leave unchanged */ |
Willy Tarreau | 6162db2 | 2009-10-10 17:13:00 +0200 | [diff] [blame] | 107 | int level; /* access level (ACCESS_LVL_*) */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 108 | } ux; |
| 109 | } perm; |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 110 | char *interface; /* interface name or NULL */ |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 111 | int maxseg; /* for TCP, advertised MSS */ |
Willy Tarreau | 90a570f | 2009-10-04 20:54:54 +0200 | [diff] [blame] | 112 | |
| 113 | struct { |
| 114 | const char *file; /* file where the section appears */ |
| 115 | int line; /* line where the section appears */ |
Willy Tarreau | 53fb4ae | 2009-10-04 23:04:08 +0200 | [diff] [blame] | 116 | struct eb32_node id; /* place in the tree of used IDs */ |
Willy Tarreau | 90a570f | 2009-10-04 20:54:54 +0200 | [diff] [blame] | 117 | } conf; /* config information */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* This structure contains all information needed to easily handle a protocol. |
| 121 | * Its primary goal is to ease listeners maintenance. Specifically, the |
| 122 | * bind_all() primitive must be used before any fork(), and the enable_all() |
| 123 | * primitive must be called after the fork() to enable all fds. Last, the |
| 124 | * unbind_all() primitive closes all listeners. |
| 125 | */ |
| 126 | struct protocol { |
| 127 | char name[PROTO_NAME_LEN]; /* protocol name, zero-terminated */ |
| 128 | int sock_domain; /* socket domain, as passed to socket() */ |
| 129 | int sock_type; /* socket type, as passed to socket() */ |
| 130 | int sock_prot; /* socket protocol, as passed to socket() */ |
| 131 | sa_family_t sock_family; /* socket family, for sockaddr */ |
Willy Tarreau | 106bf27 | 2007-10-28 12:09:45 +0100 | [diff] [blame] | 132 | socklen_t sock_addrlen; /* socket address length, used by bind() */ |
| 133 | int l3_addrlen; /* layer3 address length, used by hashes */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 134 | int (*accept)(int fd); /* generic accept function */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 135 | int (*read)(int fd); /* generic read function */ |
| 136 | int (*write)(int fd); /* generic write function */ |
| 137 | int (*bind_all)(struct protocol *proto); /* bind all unbound listeners */ |
| 138 | int (*unbind_all)(struct protocol *proto); /* unbind all bound listeners */ |
| 139 | int (*enable_all)(struct protocol *proto); /* enable all bound listeners */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 140 | int (*disable_all)(struct protocol *proto); /* disable all bound listeners */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 141 | struct list listeners; /* list of listeners using this protocol */ |
| 142 | int nb_listeners; /* number of listeners */ |
| 143 | struct list list; /* list of registered protocols */ |
| 144 | }; |
| 145 | |
| 146 | #endif /* _TYPES_PROTOCOLS_H */ |
| 147 | |
| 148 | /* |
| 149 | * Local variables: |
| 150 | * c-indent-level: 8 |
| 151 | * c-basic-offset: 8 |
| 152 | * End: |
| 153 | */ |