blob: c13c73d361b8c1193475e1eb72cd2c3fd4567168 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
2 include/types/protocols.h
3 This file defines the structures used by generic network protocols.
4
Willy Tarreau2c9f5b12009-08-16 19:12:36 +02005 Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01006
Willy Tarreaudd815982007-10-16 12:25:14 +02007 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 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 */
41#define LI_NEW 0 /* not initialized yet */
Willy Tarreaudabf2e22007-10-28 21:59:24 +010042#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 Tarreaudd815982007-10-16 12:25:14 +020068
Willy Tarreau6fb42e02007-10-28 17:02:33 +010069/* listener socket options */
70#define LI_O_NONE 0x0000
71#define LI_O_NOLINGER 0x0001 /* disable linger on this socket */
Willy Tarreaub1e52e82008-01-13 14:49:51 +010072#define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses */
Willy Tarreau9ea05a72009-06-14 12:07:01 +020073#define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */
Willy Tarreaucb6cd432009-10-13 07:34:14 +020074#define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */
Willy Tarreau6fb42e02007-10-28 17:02:33 +010075
Willy Tarreaudd815982007-10-16 12:25:14 +020076/* The listener will be directly referenced by the fdtab[] which holds its
77 * socket. The listener provides the protocol-specific accept() function to
78 * the fdtab.
79 */
80struct listener {
81 int fd; /* the listen socket */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +020082 char *name; /* */
83 int luid; /* listener universally unique ID, used for SNMP */
Willy Tarreaudabf2e22007-10-28 21:59:24 +010084 int state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
Willy Tarreau6fb42e02007-10-28 17:02:33 +010085 int options; /* socket options : LI_O_* */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +020086 struct licounters *counters; /* statistics counters */
Willy Tarreaudd815982007-10-16 12:25:14 +020087 struct sockaddr_storage addr; /* the address we listen to */
88 struct protocol *proto; /* protocol this listener belongs to */
89 int nbconn; /* current number of connections on this listener */
90 int maxconn; /* maximum connections allowed on this listener */
Willy Tarreauc73ce2b2008-01-06 10:55:10 +010091 unsigned int backlog; /* if set, listen backlog */
Willy Tarreaudd815982007-10-16 12:25:14 +020092 struct listener *next; /* next address for the same proxy, or NULL */
93 struct list proto_list; /* list in the protocol header */
94 int (*accept)(int fd); /* accept() function passed to fdtab[] */
Willy Tarreau26c25062009-03-08 09:38:41 +010095 struct task * (*handler)(struct task *t); /* protocol handler. It is a task */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020096 int *timeout; /* pointer to client-side timeout */
Willy Tarreaudd815982007-10-16 12:25:14 +020097 void *private; /* any private data which may be used by accept() */
Willy Tarreau3bc13772008-12-07 11:50:35 +010098 unsigned int analysers; /* bitmap of required protocol analysers */
Willy Tarreau2c9f5b12009-08-16 19:12:36 +020099 int nice; /* nice value to assign to the instanciated tasks */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200100 union { /* protocol-dependant access restrictions */
101 struct { /* UNIX socket permissions */
102 uid_t uid; /* -1 to leave unchanged */
103 gid_t gid; /* -1 to leave unchanged */
104 mode_t mode; /* 0 to leave unchanged */
Willy Tarreau6162db22009-10-10 17:13:00 +0200105 int level; /* access level (ACCESS_LVL_*) */
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200106 } ux;
107 } perm;
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100108 char *interface; /* interface name or NULL */
Willy Tarreaube1b9182009-06-14 18:48:19 +0200109 int maxseg; /* for TCP, advertised MSS */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200110
111 struct {
112 const char *file; /* file where the section appears */
113 int line; /* line where the section appears */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200114 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200115 } conf; /* config information */
Willy Tarreaudd815982007-10-16 12:25:14 +0200116};
117
118/* This structure contains all information needed to easily handle a protocol.
119 * Its primary goal is to ease listeners maintenance. Specifically, the
120 * bind_all() primitive must be used before any fork(), and the enable_all()
121 * primitive must be called after the fork() to enable all fds. Last, the
122 * unbind_all() primitive closes all listeners.
123 */
124struct protocol {
125 char name[PROTO_NAME_LEN]; /* protocol name, zero-terminated */
126 int sock_domain; /* socket domain, as passed to socket() */
127 int sock_type; /* socket type, as passed to socket() */
128 int sock_prot; /* socket protocol, as passed to socket() */
129 sa_family_t sock_family; /* socket family, for sockaddr */
Willy Tarreau106bf272007-10-28 12:09:45 +0100130 socklen_t sock_addrlen; /* socket address length, used by bind() */
131 int l3_addrlen; /* layer3 address length, used by hashes */
Willy Tarreaudd815982007-10-16 12:25:14 +0200132 int (*read)(int fd); /* generic read function */
133 int (*write)(int fd); /* generic write function */
134 int (*bind_all)(struct protocol *proto); /* bind all unbound listeners */
135 int (*unbind_all)(struct protocol *proto); /* unbind all bound listeners */
136 int (*enable_all)(struct protocol *proto); /* enable all bound listeners */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100137 int (*disable_all)(struct protocol *proto); /* disable all bound listeners */
Willy Tarreaudd815982007-10-16 12:25:14 +0200138 struct list listeners; /* list of listeners using this protocol */
139 int nb_listeners; /* number of listeners */
140 struct list list; /* list of registered protocols */
141};
142
143#endif /* _TYPES_PROTOCOLS_H */
144
145/*
146 * Local variables:
147 * c-indent-level: 8
148 * c-basic-offset: 8
149 * End:
150 */