Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 2 | * include/types/protocols.h |
| 3 | * This file defines the structures used by generic network protocols. |
| 4 | * |
| 5 | * Copyright (C) 2000-2011 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 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 | */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 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 */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 41 | enum { |
| 42 | LI_NEW = 0, /* not initialized yet */ |
| 43 | LI_INIT, /* all parameters filled in, but not assigned yet */ |
| 44 | LI_ASSIGNED, /* assigned to the protocol, but not listening yet */ |
| 45 | LI_PAUSED, /* listener was paused, it's bound but not listening */ |
| 46 | LI_LISTEN, /* started, listening but not enabled */ |
| 47 | LI_READY, /* started, listening and enabled */ |
| 48 | LI_FULL, /* reached its connection limit */ |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 49 | LI_LIMITED, /* transient state: limits have been reached, listener is queued */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 50 | }; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 51 | |
| 52 | /* Listener transitions |
| 53 | * calloc() set() add_listener() bind() |
| 54 | * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN |
| 55 | * <------- <---- <---------- <----- |
| 56 | * free() bzero() del_listener() unbind() |
| 57 | * |
| 58 | * The file descriptor is valid only during these three states : |
| 59 | * |
| 60 | * disable() |
| 61 | * LISTEN <------------ READY |
| 62 | * A| ------------> |A |
| 63 | * || !max & enable() || |
| 64 | * || || |
| 65 | * || max || |
| 66 | * || max & enable() V| !max |
| 67 | * |+---------------> FULL |
| 68 | * +----------------- |
| 69 | * disable() |
| 70 | * |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 71 | * The LIMITED state my be used when a limit has been detected just before |
| 72 | * using a listener. In this case, the listener MUST be queued into the |
| 73 | * appropriate wait queue (either the proxy's or the global one). It may be |
| 74 | * set back to the READY state at any instant and for any reason, so one must |
| 75 | * not rely on this state. |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 76 | */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 78 | /* listener socket options */ |
| 79 | #define LI_O_NONE 0x0000 |
| 80 | #define LI_O_NOLINGER 0x0001 /* disable linger on this socket */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 81 | #define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses */ |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 82 | #define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */ |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 83 | #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] | 84 | #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] | 85 | #define LI_O_CHK_MONNET 0x0020 /* check the source against a monitor-net rule */ |
Willy Tarreau | 8a95691 | 2010-10-15 14:27:08 +0200 | [diff] [blame] | 86 | #define LI_O_ACC_PROXY 0x0040 /* find the proxied address in the first request line */ |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 87 | #define LI_O_UNLIMITED 0x0080 /* listener not subject to global limits (peers & stats socket) */ |
| 88 | |
| 89 | /* Note: if a listener uses LI_O_UNLIMITED, it is highly recommended that it adds its own |
| 90 | * maxconn setting to the global.maxsock value so that its resources are reserved. |
| 91 | */ |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 92 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 93 | /* The listener will be directly referenced by the fdtab[] which holds its |
| 94 | * socket. The listener provides the protocol-specific accept() function to |
| 95 | * the fdtab. |
| 96 | */ |
| 97 | struct listener { |
| 98 | int fd; /* the listen socket */ |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 99 | char *name; /* */ |
| 100 | int luid; /* listener universally unique ID, used for SNMP */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 101 | int state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */ |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 102 | int options; /* socket options : LI_O_* */ |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 103 | struct licounters *counters; /* statistics counters */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 104 | struct protocol *proto; /* protocol this listener belongs to */ |
| 105 | int nbconn; /* current number of connections on this listener */ |
| 106 | int maxconn; /* maximum connections allowed on this listener */ |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 107 | unsigned int backlog; /* if set, listen backlog */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 108 | struct listener *next; /* next address for the same proxy, or NULL */ |
| 109 | struct list proto_list; /* list in the protocol header */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 110 | 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] | 111 | struct task * (*handler)(struct task *t); /* protocol handler. It is a task */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 112 | int *timeout; /* pointer to client-side timeout */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 113 | struct proxy *frontend; /* the frontend this listener belongs to, or NULL */ |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 114 | struct list wait_queue; /* link element to make the listener wait for something (LI_LIMITED) */ |
Willy Tarreau | 3bc1377 | 2008-12-07 11:50:35 +0100 | [diff] [blame] | 115 | unsigned int analysers; /* bitmap of required protocol analysers */ |
Willy Tarreau | 2c9f5b1 | 2009-08-16 19:12:36 +0200 | [diff] [blame] | 116 | int nice; /* nice value to assign to the instanciated tasks */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 117 | union { /* protocol-dependant access restrictions */ |
| 118 | struct { /* UNIX socket permissions */ |
| 119 | uid_t uid; /* -1 to leave unchanged */ |
| 120 | gid_t gid; /* -1 to leave unchanged */ |
| 121 | mode_t mode; /* 0 to leave unchanged */ |
Willy Tarreau | 6162db2 | 2009-10-10 17:13:00 +0200 | [diff] [blame] | 122 | int level; /* access level (ACCESS_LVL_*) */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 123 | } ux; |
| 124 | } perm; |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 125 | char *interface; /* interface name or NULL */ |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 126 | int maxseg; /* for TCP, advertised MSS */ |
Willy Tarreau | 90a570f | 2009-10-04 20:54:54 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | 7b81563 | 2011-10-21 18:51:57 +0200 | [diff] [blame] | 128 | /* warning: this struct is huge, keep it at the bottom */ |
| 129 | struct sockaddr_storage addr; /* the address we listen to */ |
Willy Tarreau | 90a570f | 2009-10-04 20:54:54 +0200 | [diff] [blame] | 130 | struct { |
| 131 | const char *file; /* file where the section appears */ |
| 132 | int line; /* line where the section appears */ |
Willy Tarreau | 53fb4ae | 2009-10-04 23:04:08 +0200 | [diff] [blame] | 133 | struct eb32_node id; /* place in the tree of used IDs */ |
Willy Tarreau | 90a570f | 2009-10-04 20:54:54 +0200 | [diff] [blame] | 134 | } conf; /* config information */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 137 | struct stream_interface; |
| 138 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 139 | /* This structure contains all information needed to easily handle a protocol. |
| 140 | * Its primary goal is to ease listeners maintenance. Specifically, the |
| 141 | * bind_all() primitive must be used before any fork(), and the enable_all() |
| 142 | * primitive must be called after the fork() to enable all fds. Last, the |
| 143 | * unbind_all() primitive closes all listeners. |
| 144 | */ |
| 145 | struct protocol { |
| 146 | char name[PROTO_NAME_LEN]; /* protocol name, zero-terminated */ |
| 147 | int sock_domain; /* socket domain, as passed to socket() */ |
| 148 | int sock_type; /* socket type, as passed to socket() */ |
| 149 | int sock_prot; /* socket protocol, as passed to socket() */ |
| 150 | sa_family_t sock_family; /* socket family, for sockaddr */ |
Willy Tarreau | 106bf27 | 2007-10-28 12:09:45 +0100 | [diff] [blame] | 151 | socklen_t sock_addrlen; /* socket address length, used by bind() */ |
| 152 | int l3_addrlen; /* layer3 address length, used by hashes */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 153 | int (*accept)(int fd); /* generic accept function */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 154 | int (*bind)(struct listener *l, char *errmsg, int errlen); /* bind a listener */ |
| 155 | int (*bind_all)(struct protocol *proto, char *errmsg, int errlen); /* bind all unbound listeners */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 156 | int (*unbind_all)(struct protocol *proto); /* unbind all bound listeners */ |
| 157 | int (*enable_all)(struct protocol *proto); /* enable all bound listeners */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 158 | int (*disable_all)(struct protocol *proto); /* disable all bound listeners */ |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 159 | int (*connect)(struct stream_interface *); /* connect function if any */ |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 160 | int (*get_src)(int fd, struct sockaddr *, socklen_t, int dir); /* syscall used to retrieve src addr */ |
| 161 | int (*get_dst)(int fd, struct sockaddr *, socklen_t, int dir); /* syscall used to retrieve dst addr */ |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 162 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 163 | struct list listeners; /* list of listeners using this protocol */ |
| 164 | int nb_listeners; /* number of listeners */ |
| 165 | struct list list; /* list of registered protocols */ |
| 166 | }; |
| 167 | |
| 168 | #endif /* _TYPES_PROTOCOLS_H */ |
| 169 | |
| 170 | /* |
| 171 | * Local variables: |
| 172 | * c-indent-level: 8 |
| 173 | * c-basic-offset: 8 |
| 174 | * End: |
| 175 | */ |