Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/types/protocols.h |
| 3 | This file defines the structures used by generic network protocols. |
| 4 | |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 5 | Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 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 | */ |
| 21 | |
| 22 | #ifndef _TYPES_PROTOCOLS_H |
| 23 | #define _TYPES_PROTOCOLS_H |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/socket.h> |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/un.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 29 | |
| 30 | #include <common/config.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | |
| 33 | /* max length of a protcol name, including trailing zero */ |
| 34 | #define PROTO_NAME_LEN 16 |
| 35 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 36 | /* listener state */ |
| 37 | #define LI_NEW 0 /* not initialized yet */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 38 | #define LI_INIT 1 /* all parameters filled in, but not assigned yet */ |
| 39 | #define LI_ASSIGNED 2 /* assigned to the protocol, but not listening yet */ |
| 40 | #define LI_LISTEN 3 /* started, listening but not enabled */ |
| 41 | #define LI_READY 4 /* started, listening and enabled */ |
| 42 | #define LI_FULL 5 /* reached its connection limit */ |
| 43 | |
| 44 | /* Listener transitions |
| 45 | * calloc() set() add_listener() bind() |
| 46 | * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN |
| 47 | * <------- <---- <---------- <----- |
| 48 | * free() bzero() del_listener() unbind() |
| 49 | * |
| 50 | * The file descriptor is valid only during these three states : |
| 51 | * |
| 52 | * disable() |
| 53 | * LISTEN <------------ READY |
| 54 | * A| ------------> |A |
| 55 | * || !max & enable() || |
| 56 | * || || |
| 57 | * || max || |
| 58 | * || max & enable() V| !max |
| 59 | * |+---------------> FULL |
| 60 | * +----------------- |
| 61 | * disable() |
| 62 | * |
| 63 | */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 65 | /* listener socket options */ |
| 66 | #define LI_O_NONE 0x0000 |
| 67 | #define LI_O_NOLINGER 0x0001 /* disable linger on this socket */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 68 | #define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses */ |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 69 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 70 | /* The listener will be directly referenced by the fdtab[] which holds its |
| 71 | * socket. The listener provides the protocol-specific accept() function to |
| 72 | * the fdtab. |
| 73 | */ |
| 74 | struct listener { |
| 75 | int fd; /* the listen socket */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 76 | int state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */ |
Willy Tarreau | 6fb42e0 | 2007-10-28 17:02:33 +0100 | [diff] [blame] | 77 | int options; /* socket options : LI_O_* */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 78 | struct sockaddr_storage addr; /* the address we listen to */ |
| 79 | struct protocol *proto; /* protocol this listener belongs to */ |
| 80 | int nbconn; /* current number of connections on this listener */ |
| 81 | int maxconn; /* maximum connections allowed on this listener */ |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 82 | unsigned int backlog; /* if set, listen backlog */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 83 | struct listener *next; /* next address for the same proxy, or NULL */ |
| 84 | struct list proto_list; /* list in the protocol header */ |
| 85 | int (*accept)(int fd); /* accept() function passed to fdtab[] */ |
| 86 | void (*handler)(struct task *t, struct timeval *next); /* protocol handler */ |
| 87 | struct timeval *timeout; /* pointer to client-side timeout */ |
| 88 | void *private; /* any private data which may be used by accept() */ |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 89 | union { /* protocol-dependant access restrictions */ |
| 90 | struct { /* UNIX socket permissions */ |
| 91 | uid_t uid; /* -1 to leave unchanged */ |
| 92 | gid_t gid; /* -1 to leave unchanged */ |
| 93 | mode_t mode; /* 0 to leave unchanged */ |
| 94 | } ux; |
| 95 | } perm; |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /* This structure contains all information needed to easily handle a protocol. |
| 99 | * Its primary goal is to ease listeners maintenance. Specifically, the |
| 100 | * bind_all() primitive must be used before any fork(), and the enable_all() |
| 101 | * primitive must be called after the fork() to enable all fds. Last, the |
| 102 | * unbind_all() primitive closes all listeners. |
| 103 | */ |
| 104 | struct protocol { |
| 105 | char name[PROTO_NAME_LEN]; /* protocol name, zero-terminated */ |
| 106 | int sock_domain; /* socket domain, as passed to socket() */ |
| 107 | int sock_type; /* socket type, as passed to socket() */ |
| 108 | int sock_prot; /* socket protocol, as passed to socket() */ |
| 109 | sa_family_t sock_family; /* socket family, for sockaddr */ |
Willy Tarreau | 106bf27 | 2007-10-28 12:09:45 +0100 | [diff] [blame] | 110 | socklen_t sock_addrlen; /* socket address length, used by bind() */ |
| 111 | int l3_addrlen; /* layer3 address length, used by hashes */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 112 | int (*read)(int fd); /* generic read function */ |
| 113 | int (*write)(int fd); /* generic write function */ |
| 114 | int (*bind_all)(struct protocol *proto); /* bind all unbound listeners */ |
| 115 | int (*unbind_all)(struct protocol *proto); /* unbind all bound listeners */ |
| 116 | int (*enable_all)(struct protocol *proto); /* enable all bound listeners */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 117 | int (*disable_all)(struct protocol *proto); /* disable all bound listeners */ |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 118 | struct list listeners; /* list of listeners using this protocol */ |
| 119 | int nb_listeners; /* number of listeners */ |
| 120 | struct list list; /* list of registered protocols */ |
| 121 | }; |
| 122 | |
| 123 | #endif /* _TYPES_PROTOCOLS_H */ |
| 124 | |
| 125 | /* |
| 126 | * Local variables: |
| 127 | * c-indent-level: 8 |
| 128 | * c-basic-offset: 8 |
| 129 | * End: |
| 130 | */ |