blob: c6073b4d21d14cc6b6fc416e2f77416dfc024ebe [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaube58c382011-07-24 18:28:10 +02002 * 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 Tarreaudd815982007-10-16 12:25:14 +020021
22#ifndef _TYPES_PROTOCOLS_H
23#define _TYPES_PROTOCOLS_H
24
25#include <sys/types.h>
26#include <sys/socket.h>
Willy Tarreaue6ad2b12007-10-18 12:45:54 +020027#include <sys/stat.h>
28#include <sys/un.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020029
Emeric Brun0b8d4d92012-05-18 15:46:21 +020030#ifdef USE_OPENSSL
31#include <openssl/ssl.h>
32#endif
33
Willy Tarreaudd815982007-10-16 12:25:14 +020034#include <common/config.h>
35#include <common/mini-clist.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010036#include <eb32tree.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020037
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +020038#include <types/counters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010039#include <types/task.h>
40
Willy Tarreaudd815982007-10-16 12:25:14 +020041/* max length of a protcol name, including trailing zero */
42#define PROTO_NAME_LEN 16
43
Willy Tarreaudd815982007-10-16 12:25:14 +020044/* listener state */
Willy Tarreaube58c382011-07-24 18:28:10 +020045enum {
46 LI_NEW = 0, /* not initialized yet */
47 LI_INIT, /* all parameters filled in, but not assigned yet */
48 LI_ASSIGNED, /* assigned to the protocol, but not listening yet */
49 LI_PAUSED, /* listener was paused, it's bound but not listening */
50 LI_LISTEN, /* started, listening but not enabled */
51 LI_READY, /* started, listening and enabled */
52 LI_FULL, /* reached its connection limit */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020053 LI_LIMITED, /* transient state: limits have been reached, listener is queued */
Willy Tarreaube58c382011-07-24 18:28:10 +020054};
Willy Tarreaudabf2e22007-10-28 21:59:24 +010055
56/* Listener transitions
57 * calloc() set() add_listener() bind()
58 * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN
59 * <------- <---- <---------- <-----
60 * free() bzero() del_listener() unbind()
61 *
62 * The file descriptor is valid only during these three states :
63 *
64 * disable()
65 * LISTEN <------------ READY
66 * A| ------------> |A
67 * || !max & enable() ||
68 * || ||
69 * || max ||
70 * || max & enable() V| !max
71 * |+---------------> FULL
72 * +-----------------
73 * disable()
74 *
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020075 * The LIMITED state my be used when a limit has been detected just before
76 * using a listener. In this case, the listener MUST be queued into the
77 * appropriate wait queue (either the proxy's or the global one). It may be
78 * set back to the READY state at any instant and for any reason, so one must
79 * not rely on this state.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010080 */
Willy Tarreaudd815982007-10-16 12:25:14 +020081
Willy Tarreau6fb42e02007-10-28 17:02:33 +010082/* listener socket options */
83#define LI_O_NONE 0x0000
84#define LI_O_NOLINGER 0x0001 /* disable linger on this socket */
Willy Tarreaub1e52e82008-01-13 14:49:51 +010085#define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses */
Willy Tarreau9ea05a72009-06-14 12:07:01 +020086#define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */
Willy Tarreaucb6cd432009-10-13 07:34:14 +020087#define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */
Willy Tarreaua5c0ab22010-05-31 10:30:33 +020088#define LI_O_TCP_RULES 0x0010 /* run TCP rules checks on the incoming connection */
Willy Tarreaude3041d2010-05-31 10:56:17 +020089#define LI_O_CHK_MONNET 0x0020 /* check the source against a monitor-net rule */
Willy Tarreau8a956912010-10-15 14:27:08 +020090#define LI_O_ACC_PROXY 0x0040 /* find the proxied address in the first request line */
Willy Tarreau3c63fd82011-09-07 18:00:47 +020091#define LI_O_UNLIMITED 0x0080 /* listener not subject to global limits (peers & stats socket) */
92
93/* Note: if a listener uses LI_O_UNLIMITED, it is highly recommended that it adds its own
94 * maxconn setting to the global.maxsock value so that its resources are reserved.
95 */
Willy Tarreau6fb42e02007-10-28 17:02:33 +010096
Willy Tarreaudd815982007-10-16 12:25:14 +020097/* The listener will be directly referenced by the fdtab[] which holds its
98 * socket. The listener provides the protocol-specific accept() function to
99 * the fdtab.
100 */
101struct listener {
102 int fd; /* the listen socket */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200103 char *name; /* */
104 int luid; /* listener universally unique ID, used for SNMP */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100105 int state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100106 int options; /* socket options : LI_O_* */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200107 struct licounters *counters; /* statistics counters */
Willy Tarreaudd815982007-10-16 12:25:14 +0200108 struct protocol *proto; /* protocol this listener belongs to */
Willy Tarreauc5788912012-08-24 18:12:41 +0200109 struct data_ops *data; /* data-layer operations operations for this socket */
Willy Tarreaudd815982007-10-16 12:25:14 +0200110 int nbconn; /* current number of connections on this listener */
111 int maxconn; /* maximum connections allowed on this listener */
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100112 unsigned int backlog; /* if set, listen backlog */
Willy Tarreaudd815982007-10-16 12:25:14 +0200113 struct listener *next; /* next address for the same proxy, or NULL */
114 struct list proto_list; /* list in the protocol header */
Willy Tarreaueb472682010-05-28 18:46:57 +0200115 int (*accept)(struct listener *l, int fd, struct sockaddr_storage *addr); /* upper layer's accept() */
Willy Tarreau26c25062009-03-08 09:38:41 +0100116 struct task * (*handler)(struct task *t); /* protocol handler. It is a task */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200117 int *timeout; /* pointer to client-side timeout */
Willy Tarreaueb472682010-05-28 18:46:57 +0200118 struct proxy *frontend; /* the frontend this listener belongs to, or NULL */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200119 struct list wait_queue; /* link element to make the listener wait for something (LI_LIMITED) */
Willy Tarreau3bc13772008-12-07 11:50:35 +0100120 unsigned int analysers; /* bitmap of required protocol analysers */
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200121 int nice; /* nice value to assign to the instanciated tasks */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200122 union { /* protocol-dependant access restrictions */
123 struct { /* UNIX socket permissions */
124 uid_t uid; /* -1 to leave unchanged */
125 gid_t gid; /* -1 to leave unchanged */
126 mode_t mode; /* 0 to leave unchanged */
Willy Tarreau6162db22009-10-10 17:13:00 +0200127 int level; /* access level (ACCESS_LVL_*) */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200128 } ux;
129 } perm;
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100130 char *interface; /* interface name or NULL */
Willy Tarreaube1b9182009-06-14 18:48:19 +0200131 int maxseg; /* for TCP, advertised MSS */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200132
Emeric Brun0b8d4d92012-05-18 15:46:21 +0200133#ifdef USE_OPENSSL
134 struct {
135 SSL_CTX *ctx;
136 } ssl_ctx;
137#endif
Willy Tarreau7b815632011-10-21 18:51:57 +0200138 /* warning: this struct is huge, keep it at the bottom */
139 struct sockaddr_storage addr; /* the address we listen to */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200140 struct {
141 const char *file; /* file where the section appears */
142 int line; /* line where the section appears */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200143 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200144 } conf; /* config information */
Willy Tarreaudd815982007-10-16 12:25:14 +0200145};
146
Willy Tarreau14f8e862012-08-30 22:23:13 +0200147struct connection;
Willy Tarreau26d8c592012-05-07 18:12:14 +0200148
Willy Tarreaudd815982007-10-16 12:25:14 +0200149/* This structure contains all information needed to easily handle a protocol.
150 * Its primary goal is to ease listeners maintenance. Specifically, the
151 * bind_all() primitive must be used before any fork(), and the enable_all()
152 * primitive must be called after the fork() to enable all fds. Last, the
153 * unbind_all() primitive closes all listeners.
154 */
155struct protocol {
156 char name[PROTO_NAME_LEN]; /* protocol name, zero-terminated */
157 int sock_domain; /* socket domain, as passed to socket() */
158 int sock_type; /* socket type, as passed to socket() */
159 int sock_prot; /* socket protocol, as passed to socket() */
160 sa_family_t sock_family; /* socket family, for sockaddr */
Willy Tarreau106bf272007-10-28 12:09:45 +0100161 socklen_t sock_addrlen; /* socket address length, used by bind() */
162 int l3_addrlen; /* layer3 address length, used by hashes */
Willy Tarreaueb472682010-05-28 18:46:57 +0200163 int (*accept)(int fd); /* generic accept function */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200164 int (*bind)(struct listener *l, char *errmsg, int errlen); /* bind a listener */
165 int (*bind_all)(struct protocol *proto, char *errmsg, int errlen); /* bind all unbound listeners */
Willy Tarreaudd815982007-10-16 12:25:14 +0200166 int (*unbind_all)(struct protocol *proto); /* unbind all bound listeners */
167 int (*enable_all)(struct protocol *proto); /* enable all bound listeners */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100168 int (*disable_all)(struct protocol *proto); /* disable all bound listeners */
Willy Tarreau14f8e862012-08-30 22:23:13 +0200169 int (*connect)(struct connection *, int data); /* connect function if any */
Willy Tarreau59b94792012-05-11 16:16:40 +0200170 int (*get_src)(int fd, struct sockaddr *, socklen_t, int dir); /* syscall used to retrieve src addr */
171 int (*get_dst)(int fd, struct sockaddr *, socklen_t, int dir); /* syscall used to retrieve dst addr */
Willy Tarreau26d8c592012-05-07 18:12:14 +0200172
Willy Tarreaudd815982007-10-16 12:25:14 +0200173 struct list listeners; /* list of listeners using this protocol */
174 int nb_listeners; /* number of listeners */
175 struct list list; /* list of registered protocols */
176};
177
178#endif /* _TYPES_PROTOCOLS_H */
179
180/*
181 * Local variables:
182 * c-indent-level: 8
183 * c-basic-offset: 8
184 * End:
185 */