Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 316 lines (302 sloc) 10.333 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/*
* Copyright (c) 2013 OmniTI Computer Consulting, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "fqd.h"
#include "fq_dtrace.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <poll.h>
#include <errno.h>

static int
fqd_ccs_auth(remote_client *client) {
  uint16_t cmd, method;
  fq_rk queue_name;

  if(fq_read_uint16(client->fd, &cmd) ||
     ntohs(cmd) != FQ_PROTO_AUTH_CMD) {
    ERRTOFD(client->fd, "auth command expected");
    return -1;
  }
  if(fq_read_uint16(client->fd, &method)) {
    ERRTOFD(client->fd, "auth method read failed");
    return -2;
  }
  method = ntohs(method);
  if(method == 0) {
    char buf[128];
    unsigned char pass[10240];
    char queue_detail[1024], *end_of_qd;
    char *qtype = NULL, *qparams = NULL;
    int len;
    len = fq_read_short_cmd(client->fd, sizeof(client->user.name),
                            client->user.name);
    if(len < 0 || len > (int)sizeof(client->user.name)) {
      ERRTOFD(client->fd, "user name is too long");
      return -3;
    }
    client->user.len = len & 0xff;
    len = fq_read_short_cmd(client->fd, sizeof(queue_detail)-1,
                            queue_detail);
    if(len < 0) return -4;
    queue_detail[len] = '\0';
    end_of_qd = memchr(queue_detail, '\0', len);
    if(!end_of_qd) {
      if(len < 0 || len > (int)sizeof(queue_name.name)) {
        ERRTOFD(client->fd, "queue name is too long");
        return -4;
      }
      queue_name.len = len & 0xff;
      memcpy(queue_name.name, queue_detail, queue_name.len);
      if(queue_name.len < sizeof(queue_name.name))
       memset(queue_name.name + queue_name.len, 0,
              sizeof(queue_name.name) - queue_name.len);
    }
    else if(end_of_qd - queue_detail <= 0xff) {
      queue_name.len = end_of_qd - queue_detail;
      memcpy(queue_name.name, queue_detail, queue_name.len);
      if(queue_name.len < sizeof(queue_name.name))
       memset(queue_name.name + queue_name.len, 0,
              sizeof(queue_name.name) - queue_name.len);
      qtype = end_of_qd + 1;
      if(*qtype) qparams = strchr(qtype, ':');
      else qtype = NULL;
      if(qparams) *qparams++ = '\0';
    }
    else {
      ERRTOFD(client->fd, "queue name is too long");
      return -4;
    }
    len = fq_read_short_cmd(client->fd, sizeof(pass), pass);
    if(len < 0 || len > (int)sizeof(queue_name.name)) {
      ERRTOFD(client->fd, "queue name is too long");
      return -4;
    }

    client->queue = fqd_queue_get(&queue_name, qtype, qparams,
                                  sizeof(buf), buf);
    if(client->queue == NULL) {
      ERRTOFD(client->fd, buf);
      return -6;
    }

    /* do AUTH */
    buf[0] = '\0';
    inet_ntop(AF_INET, &client->remote.sin_addr, buf, sizeof(buf));
    snprintf(client->pretty, sizeof(client->pretty), "%.*s/%.*s@%s:%d",
             client->user.len, client->user.name,
             queue_name.len, queue_name.name,
             buf, ntohs(client->remote.sin_port));
    if(FQ_CLIENT_AUTH_ENABLED()) {
      fq_dtrace_remote_client_t dclient;
      DTRACE_PACK_CLIENT(&dclient, client);
      FQ_CLIENT_AUTH(&dclient);
    }
    return 0;
  }
  ERRTOFD(client->fd, "unsupported auth method");
  return -1;
}

static int
fqd_ccs_key_client(remote_client *client) {
  int i;
  client->key.len = sizeof(client->key.name);
  for(i=0;i<client->key.len;i++) client->key.name[i] = random() & 0xf;

  if(fqd_queue_register_client(client->queue, client)) {
    ERRTOFD(client->fd, "can't add you to queue");
    return -1;
  }

  if(fq_write_uint16(client->fd, FQ_PROTO_AUTH_RESP) ||
     fq_write_short_cmd(client->fd,
                        client->key.len, client->key.name) < 0) {
    return -2;
  }
#ifdef DEBUG
    {
      char hex[260];
      if(fq_rk_to_hex(hex, sizeof(hex), &client->key) >= 0)
        fq_debug(FQ_DEBUG_CONN, "client keyed:\n%s\n", hex);
    }
#endif

  return 0;
}

static int
fqd_css_heartbeat(remote_client *client) {
#ifdef DEBUG
  fq_debug(FQ_DEBUG_CONN, "heartbeat -> %s\n", client->pretty);
#endif
  return fq_write_uint16(client->fd, FQ_PROTO_HB);
}

static int
fqd_css_status(remote_client *client) {
  remote_data_client *data = client->data;
#ifdef DEBUG
  fq_debug(FQ_DEBUG_CONN, "status -> %s\n", client->pretty);
#endif
  if(fq_write_uint16(client->fd, FQ_PROTO_STATUS) < 0) return -1;
#define write_uintkey(name, v) do { \
if(fq_write_short_cmd(client->fd, strlen(name), name) < 0) return -1; \
if(fq_write_uint32(client->fd, v) < 0) return -1; \
} while(0)
  if(data) {
    write_uintkey("no_exchange", data->no_exchange);
    write_uintkey("no_route", data->no_route);
    write_uintkey("routed", data->routed);
    write_uintkey("dropped", data->dropped);
    write_uintkey("msgs_in", data->msgs_in);
    write_uintkey("msgs_out", data->msgs_out);
  }
  if(fq_write_uint16(client->fd, 0) < 0) return -1;
  return 0;
}

static int
fqd_ccs_loop(remote_client *client) {
  int poll_timeout = 10;
  while(1) {
    int rv;
    struct pollfd pfd;
    uint16_t cmd;
    unsigned long long hb_us;
    hrtime_t t;
    pfd.fd = client->fd;
    pfd.events = POLLIN|POLLHUP;
    pfd.revents = 0;
    rv = poll(&pfd, 1, poll_timeout);
    if(rv < 0) {
#ifdef DEBUG
      fq_debug(FQ_DEBUG_CONN, "poll() failed on %s: %s\n", client->pretty,
               strerror(errno));
#endif
      break;
    }
    if(rv > 0) poll_timeout = 10;
    else poll_timeout *= 2;
    if(poll_timeout > 4000) poll_timeout = 4000;
    t = fq_gethrtime();
    hb_us = ((unsigned long long)client->heartbeat_ms) * 1000000ULL;
    if(client->heartbeat_ms &&
       (unsigned long long)client->last_heartbeat < (unsigned long long)(t - hb_us)) {
      if(fqd_css_heartbeat(client)) break;
      client->last_heartbeat = t;
    }
    if(hb_us &&
       (unsigned long long)client->last_activity < (unsigned long long)(t - hb_us * 3)) {
      ERRTOFD(client->fd, "heartbeat failed");
#ifdef DEBUG
      fq_debug(FQ_DEBUG_CONN, "heartbeat failed from %s\n", client->pretty);
#endif
      break;
    }
    if(rv > 0) {
      if(fq_read_uint16(client->fd, &cmd) != 0) break;
      client->last_heartbeat = client->last_activity = fq_gethrtime();
      switch(cmd) {
        case FQ_PROTO_HB:
#ifdef DEBUG
          fq_debug(FQ_DEBUG_CONN, "heartbeat <- %s\n", client->pretty);
#endif
          break;
        case FQ_PROTO_HBREQ:
        {
          uint16_t ms;
          if(fq_read_uint16(client->fd, &ms) < 0) return -1;
#ifdef DEBUG
          fq_debug(FQ_DEBUG_CONN, "setting client(%p) heartbeat to %d\n",
                  (void *)client, ms);
#endif
          client->heartbeat_ms = ms;
          break;
        }
        case FQ_PROTO_STATUSREQ:
          if(fqd_css_status(client)) return -1;
          break;
        case FQ_PROTO_BINDREQ:
        {
          int len;
          uint16_t flags;
          uint32_t route_id;
          uint64_t cgen;
          char program[0xffff];
          fq_rk exchange;
          if(fq_read_uint16(client->fd, &flags)) return -1;
          len = fq_read_short_cmd(client->fd, sizeof(exchange.name),
                                  exchange.name);
          if(len < 0 || len > (int)sizeof(exchange.name)) return -3;
          exchange.len = len & 0xff;
          len = fq_read_short_cmd(client->fd, sizeof(program)-1, program);
          if(len < 0 || len > (int)sizeof(program)-1) return -1;
          program[len] = '\0';
          route_id = fqd_config_bind(&exchange, flags, program,
                                     client->queue, &cgen);
          fqd_config_wait(cgen, 100);
          if(fq_write_uint16(client->fd, FQ_PROTO_BIND) != 0) return -1;
          if(fq_write_uint32(client->fd, route_id) != 0) return -1;
          break;
        }
        case FQ_PROTO_UNBINDREQ:
        {
          uint32_t route_id;
          fq_rk exchange;
          int success, len;
          if(fq_read_uint32(client->fd, &route_id)) return -1;
          len = fq_read_short_cmd(client->fd, sizeof(exchange.name),
                                  exchange.name);
          if(len < 0 || len > (int)sizeof(exchange.name)) return -1;
          exchange.len = len & 0xff;
          success = fqd_config_unbind(&exchange, route_id, client->queue, NULL);
          if(fq_write_uint16(client->fd, FQ_PROTO_UNBIND) != 0) return -1;
          if(fq_write_uint32(client->fd, success ? route_id : FQ_BIND_ILLEGAL))
            return -1;
          break;
        }
        default:
          return -1;
      }
    }
  }
  return -1;
}

extern void
fqd_command_and_control_server(remote_client *client) {
  /* auth */
  int rv, registered = 0;
  uint64_t cgen;
  fq_debug(FQ_DEBUG_CONN, "--> ccs thread\n");
  if((rv = fqd_ccs_auth(client)) != 0) {
    fq_debug(FQ_DEBUG_CONN, "client auth failed: %d\n", rv);
    (void)rv;
    goto out;
  }
  if(fqd_config_register_client(client, &cgen)) {
    fq_debug(FQ_DEBUG_CONN, "client registration failed\n");
    goto out;
  }
  registered = 1;
  fqd_config_wait(cgen, 100);
  if(fqd_ccs_key_client(client) != 0) {
    fq_debug(FQ_DEBUG_CONN, "client keying failed: %d\n", rv);
    goto out;
  }
  fqd_ccs_loop(client);
out:
  if(registered) fqd_config_deregister_client(client, NULL);
  fq_debug(FQ_DEBUG_CONN, "<-- ccs thread\n");
}
Something went wrong with that request. Please try again.