Exemple #1
0
void ns_hexdump_connection(struct ns_connection *nc, const char *path,
                           int num_bytes, int ev) {
  const struct mbuf *io = ev == NS_SEND ? &nc->send_mbuf : &nc->recv_mbuf;
  FILE *fp;
  char *buf, src[60], dst[60];
  int buf_size = num_bytes * 5 + 100;

  if ((fp = fopen(path, "a")) != NULL) {
    ns_sock_to_str(nc->sock, src, sizeof(src), 3);
    ns_sock_to_str(nc->sock, dst, sizeof(dst), 7);
    fprintf(fp, "%lu %p %s %s %s %d\n", (unsigned long) time(NULL),
            nc->user_data, src,
            ev == NS_RECV ? "<-" : ev == NS_SEND ? "->" : ev == NS_ACCEPT
                                                              ? "<A"
                                                              : ev == NS_CONNECT
                                                                    ? "C>"
                                                                    : "XX",
            dst, num_bytes);
    if (num_bytes > 0 && (buf = (char *) NS_MALLOC(buf_size)) != NULL) {
      ns_hexdump(io->buf + (ev == NS_SEND ? 0 : io->len) -
                     (ev == NS_SEND ? 0 : num_bytes),
                 num_bytes, buf, buf_size);
      fprintf(fp, "%s", buf);
      NS_FREE(buf);
    }
    fclose(fp);
  }
}
Exemple #2
0
static const char *test_hexdump(void) {
  const char *src = "\1\2\3\4abcd";
  char got[256];

  const char *want ="0000  01 02 03 04 61 62 63 64"
                    "                          ....abcd\n\n";
  ASSERT(ns_hexdump(src, strlen(src), got, sizeof(got)) == (int)strlen(want));
  ASSERT(strcmp(got, want) == 0);
  return NULL;
}
Exemple #3
0
static void ev_handler(struct ns_connection *nc, int ev, void *p) {
  struct ns_mqtt_message *msg = (struct ns_mqtt_message *)p;
  (void) nc;

#if 0
  if (ev != NS_POLL)
    printf("USER HANDLER GOT %d\n", ev);
#endif

  switch (ev) {
    case NS_CONNECT:
      ns_set_protocol_mqtt(nc);
      ns_send_mqtt_handshake(nc, "dummy");
      break;
    case NS_MQTT_CONNACK:
      if (msg->connack_ret_code != NS_MQTT_CONNACK_ACCEPTED) {
        printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
        exit(1);
      }
      printf("Subscribing to '/stuff'\n");
      ns_mqtt_subscribe(nc, topic_expressions, sizeof(topic_expressions)/sizeof(*topic_expressions), 42);
      break;
    case NS_MQTT_PUBACK:
      printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
      break;
    case NS_MQTT_SUBACK:
      printf("Subscription acknowledged, forwarding to '/test'\n");
      break;
    case NS_MQTT_PUBLISH:
      {
#if 0
        char hex[1024] = {0};
        ns_hexdump(nc->recv_iobuf.buf, msg->payload.len, hex, sizeof(hex));
        printf("Got incoming message %s:\n%s", msg->topic, hex);
#else
        printf("Got incoming message %s: %.*s\n", msg->topic, (int)msg->payload.len, msg->payload.p);
#endif

        printf("Forwarding to /test\n");
        ns_mqtt_publish(nc, "/test", 65, NS_MQTT_QOS(0), msg->payload.p, msg->payload.len);
      }
      break;
    case NS_CLOSE:
      printf("Connection closed\n");
      exit(1);
  }
}