blob: 5eddb2c593c7e5a350f4d6881613a1cf30d0cfd3 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreau645513a2010-05-24 20:55:15 +02002 * Protocol registration and listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau645513a2010-05-24 20:55:15 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdio.h>
14#include <string.h>
15
16#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010017#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020018#include <common/mini-clist.h>
19#include <common/standard.h>
20
Willy Tarreau645513a2010-05-24 20:55:15 +020021#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010022#include <proto/fd.h>
23
Willy Tarreaudd815982007-10-16 12:25:14 +020024/* List head of all registered protocols */
25static struct list protocols = LIST_HEAD_INIT(protocols);
26
Willy Tarreaudabf2e22007-10-28 21:59:24 +010027/* This function adds the specified listener's file descriptor to the polling
28 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
29 * LI_FULL state depending on its number of connections.
30 */
31void enable_listener(struct listener *listener)
32{
33 if (listener->state == LI_LISTEN) {
34 if (listener->nbconn < listener->maxconn) {
35 EV_FD_SET(listener->fd, DIR_RD);
36 listener->state = LI_READY;
37 } else {
38 listener->state = LI_FULL;
39 }
40 }
41}
42
43/* This function removes the specified listener's file descriptor from the
44 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
45 * enters LI_LISTEN.
46 */
47void disable_listener(struct listener *listener)
48{
49 if (listener->state < LI_READY)
50 return;
51 if (listener->state == LI_READY)
52 EV_FD_CLR(listener->fd, DIR_RD);
53 listener->state = LI_LISTEN;
54}
55
56/* This function adds all of the protocol's listener's file descriptors to the
57 * polling lists when they are in the LI_LISTEN state. It is intended to be
58 * used as a protocol's generic enable_all() primitive, for use after the
59 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
60 * their number of connections. It always returns ERR_NONE.
61 */
62int enable_all_listeners(struct protocol *proto)
63{
64 struct listener *listener;
65
66 list_for_each_entry(listener, &proto->listeners, proto_list)
67 enable_listener(listener);
68 return ERR_NONE;
69}
70
71/* This function removes all of the protocol's listener's file descriptors from
72 * the polling lists when they are in the LI_READY or LI_FULL states. It is
73 * intended to be used as a protocol's generic disable_all() primitive. It puts
74 * the listeners into LI_LISTEN, and always returns ERR_NONE.
75 */
76int disable_all_listeners(struct protocol *proto)
77{
78 struct listener *listener;
79
80 list_for_each_entry(listener, &proto->listeners, proto_list)
81 disable_listener(listener);
82 return ERR_NONE;
83}
84
Willy Tarreaub648d632007-10-28 22:13:50 +010085/* This function closes the listening socket for the specified listener,
86 * provided that it's already in a listening state. The listener enters the
87 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
88 * to be used as a generic function for standard protocols.
89 */
90int unbind_listener(struct listener *listener)
91{
92 if (listener->state == LI_READY)
93 EV_FD_CLR(listener->fd, DIR_RD);
94
95 if (listener->state >= LI_LISTEN) {
96 fd_delete(listener->fd);
97 listener->state = LI_ASSIGNED;
98 }
99 return ERR_NONE;
100}
101
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100102/* This function closes all listening sockets bound to the protocol <proto>,
103 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
104 * detach them from the protocol. It always returns ERR_NONE.
105 */
106int unbind_all_listeners(struct protocol *proto)
107{
108 struct listener *listener;
109
110 list_for_each_entry(listener, &proto->listeners, proto_list)
111 unbind_listener(listener);
112 return ERR_NONE;
113}
114
Willy Tarreau1a64d162007-10-28 22:26:05 +0100115/* Delete a listener from its protocol's list of listeners. The listener's
116 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
117 * number of listeners is updated. Note that the listener must have previously
118 * been unbound. This is the generic function to use to remove a listener.
119 */
120void delete_listener(struct listener *listener)
121{
122 if (listener->state != LI_ASSIGNED)
123 return;
124 listener->state = LI_INIT;
125 LIST_DEL(&listener->proto_list);
126 listener->proto->nb_listeners--;
127}
128
Willy Tarreaudd815982007-10-16 12:25:14 +0200129/* Registers the protocol <proto> */
130void protocol_register(struct protocol *proto)
131{
132 LIST_ADDQ(&protocols, &proto->list);
133}
134
135/* Unregisters the protocol <proto>. Note that all listeners must have
136 * previously been unbound.
137 */
138void protocol_unregister(struct protocol *proto)
139{
140 LIST_DEL(&proto->list);
141 LIST_INIT(&proto->list);
142}
143
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100144/* binds all listeners of all registered protocols. Returns a composition
Willy Tarreaudd815982007-10-16 12:25:14 +0200145 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
146 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200147int protocol_bind_all(char *errmsg, int errlen)
Willy Tarreaudd815982007-10-16 12:25:14 +0200148{
149 struct protocol *proto;
150 int err;
151
152 err = 0;
153 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200154 if (proto->bind_all) {
155 err |= proto->bind_all(proto, errmsg, errlen);
156 if ( err & ERR_ABORT )
157 break;
158 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200159 }
160 return err;
161}
162
163/* unbinds all listeners of all registered protocols. They are also closed.
164 * This must be performed before calling exit() in order to get a chance to
165 * remove file-system based sockets and pipes.
Emeric Bruncf20bf12010-10-22 16:06:11 +0200166 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
Willy Tarreaudd815982007-10-16 12:25:14 +0200167 */
168int protocol_unbind_all(void)
169{
170 struct protocol *proto;
171 int err;
172
173 err = 0;
174 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200175 if (proto->unbind_all) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200176 err |= proto->unbind_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200177 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200178 }
179 return err;
180}
181
182/* enables all listeners of all registered protocols. This is intended to be
183 * used after a fork() to enable reading on all file descriptors. Returns a
184 * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
185 */
186int protocol_enable_all(void)
187{
188 struct protocol *proto;
189 int err;
190
191 err = 0;
192 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200193 if (proto->enable_all) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200194 err |= proto->enable_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200195 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200196 }
197 return err;
198}
199
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100200/* disables all listeners of all registered protocols. This may be used before
201 * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE,
202 * ERR_RETRYABLE, ERR_FATAL.
203 */
204int protocol_disable_all(void)
205{
206 struct protocol *proto;
207 int err;
208
209 err = 0;
210 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200211 if (proto->disable_all) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100212 err |= proto->disable_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200213 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100214 }
215 return err;
216}
217
Willy Tarreau645513a2010-05-24 20:55:15 +0200218/************************************************************************/
219/* All supported ACL keywords must be declared here. */
220/************************************************************************/
221
222/* set test->i to the number of connexions to the same listening socket */
223static int
224acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir,
225 struct acl_expr *expr, struct acl_test *test)
226{
227 test->i = l4->listener->nbconn;
228 return 1;
229}
230
231/* set test->i to the id of the socket (listener) */
232static int
233acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, int dir,
234 struct acl_expr *expr, struct acl_test *test) {
235
236 test->flags = ACL_TEST_F_READ_ONLY;
237
238 test->i = l4->listener->luid;
239
240 return 1;
241}
242
243/* Note: must not be declared <const> as its list will be overwritten */
244static struct acl_kw_list acl_kws = {{ },{
245 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING },
246 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING },
247 { NULL, NULL, NULL, NULL },
248}};
249
250__attribute__((constructor))
251static void __protocols_init(void)
252{
253 acl_register_keywords(&acl_kws);
254}
255
256/*
257 * Local variables:
258 * c-indent-level: 8
259 * c-basic-offset: 8
260 * End:
261 */