Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/obj_type.h |
| 3 | * This file contains function prototypes to manipulate object types |
| 4 | * |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2013 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [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 _PROTO_OBJ_TYPE_H |
| 23 | #define _PROTO_OBJ_TYPE_H |
| 24 | |
| 25 | #include <common/config.h> |
| 26 | #include <common/memory.h> |
Willy Tarreau | 51c2184 | 2013-09-29 09:06:42 +0200 | [diff] [blame] | 27 | #include <types/connection.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 28 | #include <types/listener.h> |
| 29 | #include <types/obj_type.h> |
| 30 | #include <types/proxy.h> |
| 31 | #include <types/server.h> |
| 32 | #include <types/stream_interface.h> |
| 33 | |
| 34 | static inline enum obj_type obj_type(enum obj_type *t) |
| 35 | { |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 36 | if (!t || *t >= OBJ_TYPE_ENTRIES) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 37 | return OBJ_TYPE_NONE; |
| 38 | return *t; |
| 39 | } |
| 40 | |
Willy Tarreau | 55e4ecd | 2012-12-08 17:48:47 +0100 | [diff] [blame] | 41 | static inline const char *obj_type_name(enum obj_type *t) |
| 42 | { |
| 43 | switch (obj_type(t)) { |
| 44 | case OBJ_TYPE_LISTENER: return "LISTENER"; |
| 45 | case OBJ_TYPE_PROXY: return "PROXY"; |
| 46 | case OBJ_TYPE_SERVER: return "SERVER"; |
| 47 | case OBJ_TYPE_APPLET: return "APPLET"; |
Willy Tarreau | 0788f47 | 2013-10-24 22:45:25 +0200 | [diff] [blame] | 48 | case OBJ_TYPE_APPCTX: return "APPCTX"; |
Willy Tarreau | 51c2184 | 2013-09-29 09:06:42 +0200 | [diff] [blame] | 49 | case OBJ_TYPE_CONN: return "CONN"; |
Willy Tarreau | 55e4ecd | 2012-12-08 17:48:47 +0100 | [diff] [blame] | 50 | default: return "NONE"; |
| 51 | } |
| 52 | } |
| 53 | |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 54 | /* Note: for convenience, we provide two versions of each function : |
| 55 | * - __objt_<type> : converts the pointer without any control of its |
| 56 | * value nor type. |
| 57 | * - objt_<type> : same as above except that if the pointer is NULL |
| 58 | * or points to a non-matching type, NULL is returned instead. |
| 59 | */ |
| 60 | |
| 61 | static inline struct listener *__objt_listener(enum obj_type *t) |
| 62 | { |
| 63 | return container_of(t, struct listener, obj_type); |
| 64 | } |
| 65 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 66 | static inline struct listener *objt_listener(enum obj_type *t) |
| 67 | { |
| 68 | if (!t || *t != OBJ_TYPE_LISTENER) |
| 69 | return NULL; |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 70 | return __objt_listener(t); |
| 71 | } |
| 72 | |
| 73 | static inline struct proxy *__objt_proxy(enum obj_type *t) |
| 74 | { |
| 75 | return container_of(t, struct proxy, obj_type); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static inline struct proxy *objt_proxy(enum obj_type *t) |
| 79 | { |
| 80 | if (!t || *t != OBJ_TYPE_PROXY) |
| 81 | return NULL; |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 82 | return __objt_proxy(t); |
| 83 | } |
| 84 | |
| 85 | static inline struct server *__objt_server(enum obj_type *t) |
| 86 | { |
| 87 | return container_of(t, struct server, obj_type); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static inline struct server *objt_server(enum obj_type *t) |
| 91 | { |
| 92 | if (!t || *t != OBJ_TYPE_SERVER) |
| 93 | return NULL; |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 94 | return __objt_server(t); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 97 | static inline struct si_applet *__objt_applet(enum obj_type *t) |
| 98 | { |
| 99 | return container_of(t, struct si_applet, obj_type); |
| 100 | } |
| 101 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 102 | static inline struct si_applet *objt_applet(enum obj_type *t) |
| 103 | { |
| 104 | if (!t || *t != OBJ_TYPE_APPLET) |
| 105 | return NULL; |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 106 | return __objt_applet(t); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Willy Tarreau | 0788f47 | 2013-10-24 22:45:25 +0200 | [diff] [blame] | 109 | static inline struct appctx *__objt_appctx(enum obj_type *t) |
| 110 | { |
| 111 | return container_of(t, struct appctx, obj_type); |
| 112 | } |
| 113 | |
| 114 | static inline struct appctx *objt_appctx(enum obj_type *t) |
| 115 | { |
| 116 | if (!t || *t != OBJ_TYPE_APPCTX) |
| 117 | return NULL; |
| 118 | return __objt_appctx(t); |
| 119 | } |
| 120 | |
Willy Tarreau | 51c2184 | 2013-09-29 09:06:42 +0200 | [diff] [blame] | 121 | static inline struct connection *__objt_conn(enum obj_type *t) |
| 122 | { |
| 123 | return container_of(t, struct connection, obj_type); |
| 124 | } |
| 125 | |
| 126 | static inline struct connection *objt_conn(enum obj_type *t) |
| 127 | { |
| 128 | if (!t || *t != OBJ_TYPE_CONN) |
| 129 | return NULL; |
| 130 | return __objt_conn(t); |
| 131 | } |
| 132 | |
Willy Tarreau | 55e4ecd | 2012-12-08 17:48:47 +0100 | [diff] [blame] | 133 | static inline void *obj_base_ptr(enum obj_type *t) |
| 134 | { |
| 135 | switch (obj_type(t)) { |
Willy Tarreau | 66337a0 | 2013-09-29 09:15:31 +0200 | [diff] [blame] | 136 | case OBJ_TYPE_LISTENER: return __objt_listener(t); |
| 137 | case OBJ_TYPE_PROXY: return __objt_proxy(t); |
| 138 | case OBJ_TYPE_SERVER: return __objt_server(t); |
| 139 | case OBJ_TYPE_APPLET: return __objt_applet(t); |
Willy Tarreau | 0788f47 | 2013-10-24 22:45:25 +0200 | [diff] [blame] | 140 | case OBJ_TYPE_APPCTX: return __objt_appctx(t); |
Willy Tarreau | 51c2184 | 2013-09-29 09:06:42 +0200 | [diff] [blame] | 141 | case OBJ_TYPE_CONN: return __objt_conn(t); |
Willy Tarreau | 55e4ecd | 2012-12-08 17:48:47 +0100 | [diff] [blame] | 142 | default: return NULL; |
| 143 | } |
| 144 | } |
| 145 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 146 | #endif /* _PROTO_OBJ_TYPE_H */ |
| 147 | |
| 148 | /* |
| 149 | * Local variables: |
| 150 | * c-indent-level: 8 |
| 151 | * c-basic-offset: 8 |
| 152 | * End: |
| 153 | */ |