blob: 70d04de4caae153408995ccdda4e44a20f0c9a8c [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
2 * Protocol registration functions.
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
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
21#include <types/protocols.h>
22
Willy Tarreaub648d632007-10-28 22:13:50 +010023#include <proto/fd.h>
24
Willy Tarreaudd815982007-10-16 12:25:14 +020025/* List head of all registered protocols */
26static struct list protocols = LIST_HEAD_INIT(protocols);
27
Willy Tarreaudabf2e22007-10-28 21:59:24 +010028/* This function adds the specified listener's file descriptor to the polling
29 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
30 * LI_FULL state depending on its number of connections.
31 */
32void enable_listener(struct listener *listener)
33{
34 if (listener->state == LI_LISTEN) {
35 if (listener->nbconn < listener->maxconn) {
36 EV_FD_SET(listener->fd, DIR_RD);
37 listener->state = LI_READY;
38 } else {
39 listener->state = LI_FULL;
40 }
41 }
42}
43
44/* This function removes the specified listener's file descriptor from the
45 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
46 * enters LI_LISTEN.
47 */
48void disable_listener(struct listener *listener)
49{
50 if (listener->state < LI_READY)
51 return;
52 if (listener->state == LI_READY)
53 EV_FD_CLR(listener->fd, DIR_RD);
54 listener->state = LI_LISTEN;
55}
56
57/* This function adds all of the protocol's listener's file descriptors to the
58 * polling lists when they are in the LI_LISTEN state. It is intended to be
59 * used as a protocol's generic enable_all() primitive, for use after the
60 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
61 * their number of connections. It always returns ERR_NONE.
62 */
63int enable_all_listeners(struct protocol *proto)
64{
65 struct listener *listener;
66
67 list_for_each_entry(listener, &proto->listeners, proto_list)
68 enable_listener(listener);
69 return ERR_NONE;
70}
71
72/* This function removes all of the protocol's listener's file descriptors from
73 * the polling lists when they are in the LI_READY or LI_FULL states. It is
74 * intended to be used as a protocol's generic disable_all() primitive. It puts
75 * the listeners into LI_LISTEN, and always returns ERR_NONE.
76 */
77int disable_all_listeners(struct protocol *proto)
78{
79 struct listener *listener;
80
81 list_for_each_entry(listener, &proto->listeners, proto_list)
82 disable_listener(listener);
83 return ERR_NONE;
84}
85
Willy Tarreaub648d632007-10-28 22:13:50 +010086/* This function closes the listening socket for the specified listener,
87 * provided that it's already in a listening state. The listener enters the
88 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
89 * to be used as a generic function for standard protocols.
90 */
91int unbind_listener(struct listener *listener)
92{
93 if (listener->state == LI_READY)
94 EV_FD_CLR(listener->fd, DIR_RD);
95
96 if (listener->state >= LI_LISTEN) {
97 fd_delete(listener->fd);
98 listener->state = LI_ASSIGNED;
99 }
100 return ERR_NONE;
101}
102
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100103/* This function closes all listening sockets bound to the protocol <proto>,
104 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
105 * detach them from the protocol. It always returns ERR_NONE.
106 */
107int unbind_all_listeners(struct protocol *proto)
108{
109 struct listener *listener;
110
111 list_for_each_entry(listener, &proto->listeners, proto_list)
112 unbind_listener(listener);
113 return ERR_NONE;
114}
115
Willy Tarreau1a64d162007-10-28 22:26:05 +0100116/* Delete a listener from its protocol's list of listeners. The listener's
117 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
118 * number of listeners is updated. Note that the listener must have previously
119 * been unbound. This is the generic function to use to remove a listener.
120 */
121void delete_listener(struct listener *listener)
122{
123 if (listener->state != LI_ASSIGNED)
124 return;
125 listener->state = LI_INIT;
126 LIST_DEL(&listener->proto_list);
127 listener->proto->nb_listeners--;
128}
129
Willy Tarreaudd815982007-10-16 12:25:14 +0200130/* Registers the protocol <proto> */
131void protocol_register(struct protocol *proto)
132{
133 LIST_ADDQ(&protocols, &proto->list);
134}
135
136/* Unregisters the protocol <proto>. Note that all listeners must have
137 * previously been unbound.
138 */
139void protocol_unregister(struct protocol *proto)
140{
141 LIST_DEL(&proto->list);
142 LIST_INIT(&proto->list);
143}
144
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100145/* binds all listeners of all registered protocols. Returns a composition
Willy Tarreaudd815982007-10-16 12:25:14 +0200146 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
147 */
148int protocol_bind_all(void)
149{
150 struct protocol *proto;
151 int err;
152
153 err = 0;
154 list_for_each_entry(proto, &protocols, list) {
155 if (proto->bind_all)
156 err |= proto->bind_all(proto);
157 }
158 return err;
159}
160
161/* unbinds all listeners of all registered protocols. They are also closed.
162 * This must be performed before calling exit() in order to get a chance to
163 * remove file-system based sockets and pipes.
164 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
165 */
166int protocol_unbind_all(void)
167{
168 struct protocol *proto;
169 int err;
170
171 err = 0;
172 list_for_each_entry(proto, &protocols, list) {
173 if (proto->unbind_all)
174 err |= proto->unbind_all(proto);
175 }
176 return err;
177}
178
179/* enables all listeners of all registered protocols. This is intended to be
180 * used after a fork() to enable reading on all file descriptors. Returns a
181 * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
182 */
183int protocol_enable_all(void)
184{
185 struct protocol *proto;
186 int err;
187
188 err = 0;
189 list_for_each_entry(proto, &protocols, list) {
190 if (proto->enable_all)
191 err |= proto->enable_all(proto);
192 }
193 return err;
194}
195
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100196/* disables all listeners of all registered protocols. This may be used before
197 * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE,
198 * ERR_RETRYABLE, ERR_FATAL.
199 */
200int protocol_disable_all(void)
201{
202 struct protocol *proto;
203 int err;
204
205 err = 0;
206 list_for_each_entry(proto, &protocols, list) {
207 if (proto->disable_all)
208 err |= proto->disable_all(proto);
209 }
210 return err;
211}
212