blob: a333ea2fc6b5aa17ebb681d1b29ca75eb3b91ca9 [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
30#include <common/config.h>
31#include <common/mini-clist.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010032#include <eb32tree.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020033
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +020034#include <types/counters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <types/task.h>
36
Willy Tarreaudd815982007-10-16 12:25:14 +020037/* max length of a protcol name, including trailing zero */
38#define PROTO_NAME_LEN 16
39
Willy Tarreaudd815982007-10-16 12:25:14 +020040/* listener state */
Willy Tarreaube58c382011-07-24 18:28:10 +020041enum {
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 */
49};
Willy Tarreaudabf2e22007-10-28 21:59:24 +010050
51/* Listener transitions
52 * calloc() set() add_listener() bind()
53 * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN
54 * <------- <---- <---------- <-----
55 * free() bzero() del_listener() unbind()
56 *
57 * The file descriptor is valid only during these three states :
58 *
59 * disable()
60 * LISTEN <------------ READY
61 * A| ------------> |A
62 * || !max & enable() ||
63 * || ||
64 * || max ||
65 * || max & enable() V| !max
66 * |+---------------> FULL
67 * +-----------------
68 * disable()
69 *
70 */
Willy Tarreaudd815982007-10-16 12:25:14 +020071
Willy Tarreau6fb42e02007-10-28 17:02:33 +010072/* listener socket options */
73#define LI_O_NONE 0x0000
74#define LI_O_NOLINGER 0x0001 /* disable linger on this socket */
Willy Tarreaub1e52e82008-01-13 14:49:51 +010075#define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses */
Willy Tarreau9ea05a72009-06-14 12:07:01 +020076#define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */
Willy Tarreaucb6cd432009-10-13 07:34:14 +020077#define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */
Willy Tarreaua5c0ab22010-05-31 10:30:33 +020078#define LI_O_TCP_RULES 0x0010 /* run TCP rules checks on the incoming connection */
Willy Tarreaude3041d2010-05-31 10:56:17 +020079#define LI_O_CHK_MONNET 0x0020 /* check the source against a monitor-net rule */
Willy Tarreau8a956912010-10-15 14:27:08 +020080#define LI_O_ACC_PROXY 0x0040 /* find the proxied address in the first request line */
Willy Tarreau6fb42e02007-10-28 17:02:33 +010081
Willy Tarreaudd815982007-10-16 12:25:14 +020082/* The listener will be directly referenced by the fdtab[] which holds its
83 * socket. The listener provides the protocol-specific accept() function to
84 * the fdtab.
85 */
86struct listener {
87 int fd; /* the listen socket */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +020088 char *name; /* */
89 int luid; /* listener universally unique ID, used for SNMP */
Willy Tarreaudabf2e22007-10-28 21:59:24 +010090 int state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
Willy Tarreau6fb42e02007-10-28 17:02:33 +010091 int options; /* socket options : LI_O_* */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +020092 struct licounters *counters; /* statistics counters */
Willy Tarreaudd815982007-10-16 12:25:14 +020093 struct sockaddr_storage addr; /* the address we listen to */
94 struct protocol *proto; /* protocol this listener belongs to */
95 int nbconn; /* current number of connections on this listener */
96 int maxconn; /* maximum connections allowed on this listener */
Willy Tarreauc73ce2b2008-01-06 10:55:10 +010097 unsigned int backlog; /* if set, listen backlog */
Willy Tarreaudd815982007-10-16 12:25:14 +020098 struct listener *next; /* next address for the same proxy, or NULL */
99 struct list proto_list; /* list in the protocol header */
Willy Tarreaueb472682010-05-28 18:46:57 +0200100 int (*accept)(struct listener *l, int fd, struct sockaddr_storage *addr); /* upper layer's accept() */
Willy Tarreau26c25062009-03-08 09:38:41 +0100101 struct task * (*handler)(struct task *t); /* protocol handler. It is a task */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200102 int *timeout; /* pointer to client-side timeout */
Willy Tarreaueb472682010-05-28 18:46:57 +0200103 struct proxy *frontend; /* the frontend this listener belongs to, or NULL */
Willy Tarreau3bc13772008-12-07 11:50:35 +0100104 unsigned int analysers; /* bitmap of required protocol analysers */
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200105 int nice; /* nice value to assign to the instanciated tasks */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200106 union { /* protocol-dependant access restrictions */
107 struct { /* UNIX socket permissions */
108 uid_t uid; /* -1 to leave unchanged */
109 gid_t gid; /* -1 to leave unchanged */
110 mode_t mode; /* 0 to leave unchanged */
Willy Tarreau6162db22009-10-10 17:13:00 +0200111 int level; /* access level (ACCESS_LVL_*) */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200112 } ux;
113 } perm;
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100114 char *interface; /* interface name or NULL */
Willy Tarreaube1b9182009-06-14 18:48:19 +0200115 int maxseg; /* for TCP, advertised MSS */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200116
117 struct {
118 const char *file; /* file where the section appears */
119 int line; /* line where the section appears */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200120 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200121 } conf; /* config information */
Willy Tarreaudd815982007-10-16 12:25:14 +0200122};
123
124/* This structure contains all information needed to easily handle a protocol.
125 * Its primary goal is to ease listeners maintenance. Specifically, the
126 * bind_all() primitive must be used before any fork(), and the enable_all()
127 * primitive must be called after the fork() to enable all fds. Last, the
128 * unbind_all() primitive closes all listeners.
129 */
130struct protocol {
131 char name[PROTO_NAME_LEN]; /* protocol name, zero-terminated */
132 int sock_domain; /* socket domain, as passed to socket() */
133 int sock_type; /* socket type, as passed to socket() */
134 int sock_prot; /* socket protocol, as passed to socket() */
135 sa_family_t sock_family; /* socket family, for sockaddr */
Willy Tarreau106bf272007-10-28 12:09:45 +0100136 socklen_t sock_addrlen; /* socket address length, used by bind() */
137 int l3_addrlen; /* layer3 address length, used by hashes */
Willy Tarreaueb472682010-05-28 18:46:57 +0200138 int (*accept)(int fd); /* generic accept function */
Willy Tarreaudd815982007-10-16 12:25:14 +0200139 int (*read)(int fd); /* generic read function */
140 int (*write)(int fd); /* generic write function */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200141 int (*bind)(struct listener *l, char *errmsg, int errlen); /* bind a listener */
142 int (*bind_all)(struct protocol *proto, char *errmsg, int errlen); /* bind all unbound listeners */
Willy Tarreaudd815982007-10-16 12:25:14 +0200143 int (*unbind_all)(struct protocol *proto); /* unbind all bound listeners */
144 int (*enable_all)(struct protocol *proto); /* enable all bound listeners */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100145 int (*disable_all)(struct protocol *proto); /* disable all bound listeners */
Willy Tarreaudd815982007-10-16 12:25:14 +0200146 struct list listeners; /* list of listeners using this protocol */
147 int nb_listeners; /* number of listeners */
148 struct list list; /* list of registered protocols */
149};
150
151#endif /* _TYPES_PROTOCOLS_H */
152
153/*
154 * Local variables:
155 * c-indent-level: 8
156 * c-basic-offset: 8
157 * End:
158 */