| Commit | Line | Data |
|---|---|---|
| 9ab15106 MD |
1 | /* |
| 2 | * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved. | |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Matthew Dillon <dillon@dragonflybsd.org> | |
| 6 | * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> | |
| 7 | * | |
| 8 | * Redistribution and use in source and binary forms, with or without | |
| 9 | * modification, are permitted provided that the following conditions | |
| 10 | * are met: | |
| 11 | * | |
| 12 | * 1. Redistributions of source code must retain the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer. | |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 15 | * notice, this list of conditions and the following disclaimer in | |
| 16 | * the documentation and/or other materials provided with the | |
| 17 | * distribution. | |
| 18 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 19 | * contributors may be used to endorse or promote products derived | |
| 20 | * from this software without specific, prior written permission. | |
| 21 | * | |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 33 | * SUCH DAMAGE. | |
| 34 | */ | |
| 35 | ||
| 36 | #include "hammer2.h" | |
| 37 | ||
| 62efe6ec | 38 | static void *master_accept(void *data); |
| 29ead430 MD |
39 | static void master_auth_signal(hammer2_router_t *router); |
| 40 | static void master_auth_rxmsg(hammer2_msg_t *msg); | |
| 41 | static void master_link_signal(hammer2_router_t *router); | |
| 42 | static void master_link_rxmsg(hammer2_msg_t *msg); | |
| 9ab15106 MD |
43 | |
| 44 | /* | |
| 45 | * Start-up the master listener daemon for the machine. | |
| 46 | * | |
| 47 | * The master listener serves as a rendezvous point in the cluster, accepting | |
| 48 | * connections, performing registrations and authentications, maintaining | |
| 49 | * the spanning tree, and keeping track of message state so disconnects can | |
| 50 | * be handled properly. | |
| 51 | * | |
| 52 | * Once authenticated only low-level messaging protocols (which includes | |
| 53 | * tracking persistent messages) are handled by this daemon. This daemon | |
| 54 | * does not run the higher level quorum or locking protocols. | |
| 55 | * | |
| 56 | * This daemon can also be told to maintain connections to other nodes, | |
| 57 | * forming a messaging backbone, which in turn allows PFS's (if desired) to | |
| 58 | * simply connect to the master daemon via localhost if desired. | |
| 59 | * Backbones are specified via /etc/hammer2.conf. | |
| 60 | */ | |
| 61 | int | |
| 62efe6ec | 62 | cmd_service(void) |
| 9ab15106 MD |
63 | { |
| 64 | struct sockaddr_in lsin; | |
| 65 | int on; | |
| 66 | int lfd; | |
| 67 | ||
| 68 | /* | |
| 69 | * Acquire socket and set options | |
| 70 | */ | |
| 71 | if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
| 62efe6ec | 72 | fprintf(stderr, "master_listen: socket(): %s\n", |
| 9ab15106 MD |
73 | strerror(errno)); |
| 74 | return 1; | |
| 75 | } | |
| 76 | on = 1; | |
| 77 | setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
| 78 | ||
| 79 | /* | |
| 80 | * Setup listen port and try to bind. If the bind fails we assume | |
| 81 | * that a master listener process is already running and silently | |
| 82 | * fail. | |
| 83 | */ | |
| 84 | bzero(&lsin, sizeof(lsin)); | |
| 85 | lsin.sin_family = AF_INET; | |
| 86 | lsin.sin_addr.s_addr = INADDR_ANY; | |
| 87 | lsin.sin_port = htons(HAMMER2_LISTEN_PORT); | |
| 88 | if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) { | |
| 89 | close(lfd); | |
| 9b8b748f MD |
90 | if (QuietOpt == 0) { |
| 91 | fprintf(stderr, | |
| 92 | "master listen: daemon already running\n"); | |
| 93 | } | |
| 9ab15106 MD |
94 | return 0; |
| 95 | } | |
| 9b8b748f MD |
96 | if (QuietOpt == 0) |
| 97 | fprintf(stderr, "master listen: startup\n"); | |
| 9ab15106 MD |
98 | listen(lfd, 50); |
| 99 | ||
| 100 | /* | |
| 101 | * Fork and disconnect the controlling terminal and parent process, | |
| 102 | * executing the specified function as a pthread. | |
| 103 | * | |
| 104 | * Returns to the original process which can then continue running. | |
| 105 | * In debug mode this call will create the pthread without forking | |
| 106 | * and set NormalExit to 0, instead of fork. | |
| 107 | */ | |
| 62efe6ec | 108 | hammer2_demon(master_accept, (void *)(intptr_t)lfd); |
| 9ab15106 MD |
109 | if (NormalExit) |
| 110 | close(lfd); | |
| 111 | return 0; | |
| 112 | } | |
| 113 | ||
| 114 | /* | |
| 115 | * Master listen/accept thread. Accept connections on the master socket, | |
| 116 | * starting a pthread for each one. | |
| 117 | */ | |
| 118 | static | |
| 119 | void * | |
| 62efe6ec | 120 | master_accept(void *data) |
| 9ab15106 MD |
121 | { |
| 122 | struct sockaddr_in asin; | |
| 123 | socklen_t alen; | |
| 124 | pthread_t thread; | |
| 125 | int lfd = (int)(intptr_t)data; | |
| 126 | int fd; | |
| 127 | ||
| 128 | /* | |
| 129 | * Nobody waits for us | |
| 130 | */ | |
| 131 | setproctitle("hammer2 master listen"); | |
| 132 | pthread_detach(pthread_self()); | |
| 133 | ||
| 134 | /* | |
| 135 | * Accept connections and create pthreads to handle them after | |
| 136 | * validating the IP. | |
| 137 | */ | |
| 138 | for (;;) { | |
| 139 | alen = sizeof(asin); | |
| 140 | fd = accept(lfd, (struct sockaddr *)&asin, &alen); | |
| 141 | if (fd < 0) { | |
| 142 | if (errno == EINTR) | |
| 143 | continue; | |
| 144 | break; | |
| 145 | } | |
| 146 | thread = NULL; | |
| 62efe6ec | 147 | fprintf(stderr, "master_accept: accept fd %d\n", fd); |
| 9ab15106 | 148 | pthread_create(&thread, NULL, |
| 62efe6ec | 149 | master_service, (void *)(intptr_t)fd); |
| 9ab15106 MD |
150 | } |
| 151 | return (NULL); | |
| 152 | } | |
| 153 | ||
| 154 | /* | |
| 155 | * Service an accepted connection (runs as a pthread) | |
| 02454b3e MD |
156 | * |
| 157 | * (also called from a couple of other places) | |
| 9ab15106 | 158 | */ |
| 9ab15106 | 159 | void * |
| 62efe6ec | 160 | master_service(void *data) |
| 9ab15106 MD |
161 | { |
| 162 | hammer2_iocom_t iocom; | |
| 163 | int fd; | |
| 164 | ||
| 165 | fd = (int)(intptr_t)data; | |
| 5903497c | 166 | hammer2_iocom_init(&iocom, fd, -1, |
| 29ead430 MD |
167 | master_auth_signal, |
| 168 | master_auth_rxmsg, | |
| 169 | NULL); | |
| 5903497c | 170 | hammer2_iocom_core(&iocom); |
| 9ab15106 MD |
171 | |
| 172 | fprintf(stderr, | |
| 173 | "iocom on fd %d terminated error rx=%d, tx=%d\n", | |
| 174 | fd, iocom.ioq_rx.error, iocom.ioq_tx.error); | |
| 175 | close(fd); | |
| 176 | ||
| 177 | return (NULL); | |
| 178 | } | |
| 179 | ||
| 62efe6ec MD |
180 | /************************************************************************ |
| 181 | * AUTHENTICATION * | |
| 182 | ************************************************************************ | |
| 183 | * | |
| 1b195a98 MD |
184 | * Callback via hammer2_iocom_core(). |
| 185 | * | |
| 62efe6ec MD |
186 | * Additional messaging-based authentication must occur before normal |
| 187 | * message operation. The connection has already been encrypted at | |
| 188 | * this point. | |
| 189 | */ | |
| 29ead430 | 190 | static void master_auth_conn_rx(hammer2_msg_t *msg); |
| 5903497c MD |
191 | |
| 192 | static | |
| 193 | void | |
| 29ead430 | 194 | master_auth_signal(hammer2_router_t *router) |
| 5903497c MD |
195 | { |
| 196 | hammer2_msg_t *msg; | |
| 197 | ||
| 198 | /* | |
| 199 | * Transmit LNK_CONN, enabling the SPAN protocol if both sides | |
| 200 | * agree. | |
| 201 | * | |
| 202 | * XXX put additional authentication states here | |
| 203 | */ | |
| 29ead430 MD |
204 | msg = hammer2_msg_alloc(router, 0, HAMMER2_LNK_CONN | |
| 205 | HAMMER2_MSGF_CREATE, | |
| 206 | master_auth_conn_rx, NULL); | |
| 5903497c | 207 | snprintf(msg->any.lnk_conn.label, sizeof(msg->any.lnk_conn.label), "*"); |
| 29ead430 | 208 | hammer2_msg_write(msg); |
| 5903497c | 209 | |
| 29ead430 MD |
210 | hammer2_router_restate(router, |
| 211 | master_link_signal, | |
| 212 | master_link_rxmsg, | |
| 213 | NULL); | |
| 5903497c MD |
214 | } |
| 215 | ||
| 62efe6ec MD |
216 | static |
| 217 | void | |
| 29ead430 | 218 | master_auth_conn_rx(hammer2_msg_t *msg) |
| 62efe6ec | 219 | { |
| 5903497c | 220 | if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) |
| 29ead430 | 221 | hammer2_msg_reply(msg, 0); |
| 62efe6ec MD |
222 | } |
| 223 | ||
| 224 | static | |
| 225 | void | |
| 29ead430 | 226 | master_auth_rxmsg(hammer2_msg_t *msg __unused) |
| 62efe6ec | 227 | { |
| 62efe6ec MD |
228 | } |
| 229 | ||
| 1b195a98 MD |
230 | /************************************************************************ |
| 231 | * POST-AUTHENTICATION SERVICE MSGS * | |
| 232 | ************************************************************************ | |
| 233 | * | |
| 234 | * Callback via hammer2_iocom_core(). | |
| 9ab15106 MD |
235 | */ |
| 236 | static | |
| 237 | void | |
| 29ead430 | 238 | master_link_signal(hammer2_router_t *router) |
| 9ab15106 | 239 | { |
| 29ead430 | 240 | hammer2_msg_lnk_signal(router); |
| 9ab15106 MD |
241 | } |
| 242 | ||
| 9ab15106 MD |
243 | static |
| 244 | void | |
| 29ead430 | 245 | master_link_rxmsg(hammer2_msg_t *msg) |
| 9ab15106 | 246 | { |
| 5903497c MD |
247 | hammer2_state_t *state; |
| 248 | uint32_t cmd; | |
| 249 | ||
| 250 | /* | |
| 251 | * If the message state has a function established we just | |
| 252 | * call the function, otherwise we call the appropriate | |
| 253 | * link-level protocol related to the original command and | |
| 254 | * let it sort it out. | |
| 255 | * | |
| 256 | * Non-transactional one-off messages, on the otherhand, | |
| 257 | * might have REPLY set. | |
| 258 | */ | |
| 259 | state = msg->state; | |
| 81666e1b MD |
260 | cmd = state ? state->msg->any.head.cmd : msg->any.head.cmd; |
| 261 | ||
| 262 | fprintf(stderr, "service-receive: %s\n", hammer2_msg_str(msg)); | |
| 263 | ||
| 5903497c MD |
264 | if (state && state->func) { |
| 265 | assert(state->func != NULL); | |
| 29ead430 | 266 | state->func(msg); |
| 5903497c MD |
267 | } else { |
| 268 | switch(cmd & HAMMER2_MSGF_PROTOS) { | |
| 269 | case HAMMER2_MSG_PROTO_LNK: | |
| 29ead430 | 270 | hammer2_msg_lnk(msg); |
| 5903497c MD |
271 | break; |
| 272 | case HAMMER2_MSG_PROTO_DBG: | |
| 29ead430 | 273 | hammer2_msg_dbg(msg); |
| 5903497c MD |
274 | break; |
| 275 | default: | |
| 29ead430 | 276 | hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP); |
| 5903497c MD |
277 | break; |
| 278 | } | |
| 279 | } | |
| 9ab15106 | 280 | } |