/*
 * Connect to MQTT broker.
 *
 * N.B. Non-blocking call.
 */
mqtt_status_t
mqtt_connect(struct mqtt_connection *conn, char *host, uint16_t port,
             uint16_t keep_alive)
{
  uip_ip6addr_t ip6addr;
  uip_ipaddr_t *ipaddr;
  ipaddr = &ip6addr;

  /* Check if we are already trying to connect */
  if(conn->state > MQTT_CONN_STATE_NOT_CONNECTED) {
    return MQTT_STATUS_OK;
  }

  conn->server_host = host;
  conn->keep_alive = keep_alive;
  conn->server_port = port;
  conn->out_buffer_ptr = conn->out_buffer;
  conn->out_packet.qos_state = MQTT_QOS_STATE_NO_ACK;
  conn->connect_vhdr_flags |= MQTT_VHDR_CLEAN_SESSION_FLAG;

  /* convert the string IPv6 address to a numeric IPv6 address */
  uiplib_ip6addrconv(host, &ip6addr);

  uip_ipaddr_copy(&(conn->server_ip), ipaddr);

  /*
   * Initiate the connection if the IP could be resolved. Otherwise the
   * connection will be initiated when the DNS lookup is finished, in the main
   * event loop.
   */
  process_post(&mqtt_process, mqtt_do_connect_tcp_event, conn);

  return MQTT_STATUS_OK;
}
Exemple #2
0
int ip_from_string (const char *name, const char *uri, const char *s)
{
    /* Returns 1 if successful, only copy valid address */
    if (uiplib_ip6addrconv (s, &tmp_addr)) {
        uip_ip6addr_copy (&server_ipaddr, &tmp_addr);
        return 0;
    }
    return -1;
}
Exemple #3
0
/**
 * Connect to MQTT broker.
 *
 * N.B. None-blocking call.
 */
mqtt_status_t
mqtt_connect(struct mqtt_connection* conn,
             const char* host,
             uint16_t port,
             uint16_t keep_alive)
{
  uip_ip6addr_t ip6addr;
  uip_ip4addr_t ip4addr;
  uip_ipaddr_t *ipaddr;
  ipaddr = &ip6addr;

  /* Sanity check */
  assert(conn != NULL &&
         host != NULL);

  /* Check if we are already trying to connect */
  if(conn->state > MQTT_CONN_STATE_NOT_CONNECTED) {
    return MQTT_STATUS_OK;
  }

  conn->server_host = host;
  conn->keep_alive = keep_alive;
  conn->server_port = port;
  conn->out_buffer_ptr = conn->out_buffer;
  conn->out_packet.qos_state = MQTT_QOS_STATE_NO_ACK;

  /* First check if the host is an IP address. If not, try to look it up. */
  if(uiplib_ip6addrconv(host, &ip6addr) == 0) {
    if(uiplib_ip4addrconv(host, &ip4addr) != 0) {
      ip64_addr_4to6(&ip4addr, &ip6addr);
    } else {
#if UIP_UDP
      ipaddr = mdns_lookup(host);

      if(ipaddr == NULL) {
        printf("MQTT - Resolving host...\r\n");
        mdns_query(host);
        conn->state = MQTT_CONN_STATE_DNS_LOOKUP;
        return MQTT_STATUS_OK;
      }
#else /* UIP_UDP */
      DBG("Error looking up hostname when mDNS is not used due to UIP_UDP = 0.");
      conn->state = MQTT_CONN_STATE_ERROR;
      return MQTT_STATUS_DNS_ERROR;
#endif /* UIP_UDP */
    }
  }
  uip_ipaddr_copy(&(conn->server_ip), ipaddr);

  /* Initiate the connection if the IP could be resolved. Otherwise the
   * connection will be initiated when the DNS lookup is finished, in the main
   * event loop.
   */
  process_post(&mqtt_process, mqtt_do_connect_tcp_event, conn);

  return MQTT_STATUS_OK;
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
static int
start_request(struct http_socket *s)
{
  uip_ip4addr_t ip4addr;
  uip_ip6addr_t ip6addr;
  uip_ip6addr_t *addr;
  char host[MAX_HOSTLEN];
  char path[MAX_PATHLEN];
  uint16_t port;
  int ret;

  if(parse_url(s->url, host, &port, path)) {

    printf("url %s host %s port %d path %s\n",
           s->url, host, port, path);

    /* Check if we are to route the request through a proxy. */
    if(s->proxy_port != 0) {
      /* The proxy address should be an IPv6 address. */
      uip_ip6addr_copy(&ip6addr, &s->proxy_addr);
      port = s->proxy_port;
    } else if(uiplib_ip6addrconv(host, &ip6addr) == 0) {
      /* First check if the host is an IP address. */
      if(uiplib_ip4addrconv(host, &ip4addr) != 0) {
        ip64_addr_4to6(&ip4addr, &ip6addr);
      } else {
        /* Try to lookup the hostname. If it fails, we initiate a hostname
           lookup. */
        ret = resolv_lookup(host, &addr);
        if(ret == RESOLV_STATUS_UNCACHED ||
           ret == RESOLV_STATUS_EXPIRED) {
          resolv_query(host);
          puts("Resolving host...");
          return HTTP_SOCKET_OK;
        }
        if(addr != NULL) {
          s->did_tcp_connect = 1;
          tcp_socket_connect(&s->s, addr, port);
          return HTTP_SOCKET_OK;
        } else {
          return HTTP_SOCKET_ERR;
        }
      }
    }
    tcp_socket_connect(&s->s, &ip6addr, port);
    return HTTP_SOCKET_OK;
  } else {
    return HTTP_SOCKET_ERR;
  }
}
SOL_API const struct sol_network_link_addr *
sol_network_link_addr_from_str(struct sol_network_link_addr *addr, const char *buf)
{
    SOL_NULL_CHECK(addr, NULL);
    SOL_NULL_CHECK(buf, NULL);

    if (sol_bluetooth_is_addr_str(buf))
        return sol_bluetooth_addr_from_str(addr, buf);

    if (addr->family != SOL_NETWORK_FAMILY_INET6)
        return NULL;

    uiplib_ip6addrconv(buf, (uip_ip6addr_t *)&addr->addr.in6);

    return addr;
}
/*-----------------------------------------------------------------------------------*/
unsigned char
websocket_http_client_get(struct websocket_http_client_state *s,
			  const char *host, uint16_t port, const char *file,
                          const char *subprotocol)
{
  struct uip_conn *conn;
  uip_ip6addr_t ip6addr;
  uip_ip4addr_t ip4addr;
  uip_ipaddr_t *ipaddr;

  ipaddr = &ip6addr;
  /* First check if the host is an IP address. */
  if(uiplib_ip6addrconv(host, &ip6addr) == 0) {
    if(uiplib_ip4addrconv(host, &ip4addr) != 0) {
      ip64_addr_4to6(&ip4addr, &ip6addr);
    } else {
#if UIP_UDP
      ipaddr = mdns_lookup(host);
      
      if(ipaddr == NULL) {
	return 0;
      }
#else /* UIP_UDP */
      return 0;
#endif /* UIP_UDP */
    }
  }
  
  conn = tcp_connect(ipaddr, uip_htons(port), NULL);
  
  if(conn == NULL) {
    return 0;
  }
  tcp_markconn(conn, s);
  s->conn = conn;
  s->port = port;
  strncpy(s->file, file, sizeof(s->file));
  strncpy(s->host, host, sizeof(s->host));
  strncpy(s->subprotocol, subprotocol, sizeof(s->subprotocol));
  init_connection(s);
  return 1;
}
Exemple #7
0
/*
 * \brief Attempts to convert a string representation of an IPv6 address to a
 * numeric one.
 * \param buf The buffer with the string to be converted.
 * \return ADDRESS_CONVERSION_OK or ADDRESS_CONVERSION_ERROR
 *
 * ToDo: Add support for NAT64 conversion in case the incoming address is a v4
 * This is now supported in the current master, so when we pull it in this will
 * be very straightforward.
 */
static int
set_new_ip_address(char *buf)
{
  /*
   * uiplib_ip6addrconv will immediately start writing into the supplied buffer
   * even if it subsequently fails. Thus, pass an intermediate buffer
   */
  uip_ip6addr_t tmp_addr;

  int rv = uiplib_ip6addrconv(buf, &tmp_addr);

  if(rv == ADDRESS_CONVERSION_OK) {
    /* Conversion OK, copy to our main buffer */
    memcpy(&remote_addr, &tmp_addr, sizeof(remote_addr));

    PRINTF("Updated remote address ");
    PRINT6ADDR(&remote_addr);
    PRINTF("\n");
  }

  return rv;
}
Exemple #8
0
/*---------------------------------------------------------------------------*/
static int
start_request(struct http_socket *s)
{
  uip_ip4addr_t ip4addr;
  uip_ip6addr_t ip6addr;
  uip_ip6addr_t *addr;
  char host[MAX_HOSTLEN];
  char path[MAX_PATHLEN];
  uint16_t port;

  if(parse_url(s->url, host, &port, path)) {

    printf("url %s host %s port %d path %s\n\r",
           s->url, host, port, path);

    /* First check if the host is an IP address. */
    if(uiplib_ip6addrconv(host, &ip6addr) == 0) {
      if(uiplib_ip4addrconv(host, &ip4addr) != 0) {
        ip64_addr_4to6(&ip4addr, &ip6addr);
      } else {
        /* Try to lookup the hostname. If it fails, we initiate a hostname
           lookup. */
        addr = mdns_lookup(host);
        if(addr == NULL) {
          mdns_query(host);
          puts("Resolving host...");
          return HTTP_SOCKET_OK;
        }
        tcp_socket_connect(&s->s, addr, port);
        return HTTP_SOCKET_OK;
      }
    }
    tcp_socket_connect(&s->s, &ip6addr, port);
    return HTTP_SOCKET_OK;
  } else {
    return HTTP_SOCKET_ERR;
  }
}
Exemple #9
0
/*---------------------------------------------------------------------------*/
static int
start_request(struct http_socket *s)
{
  uip_ip4addr_t ip4addr;
  uip_ip6addr_t ip6addr;
  uip_ipaddr_t *addr;
  char host[MAX_HOSTLEN];
  char path[MAX_PATHLEN];
  uint16_t port;

  if(parse_url(s->url, host, &port, path)) {

    PRINTF("%s url %s host %s port %d path %s\n",
           HTTP_METHOD_STR(s->method), s->url, host, port, path);

    /* First check if the host is an IP address. */
    if(uiplib_ip6addrconv(host, &ip6addr) == 0) {
      if(uiplib_ip4addrconv(host, &ip4addr) != 0) {
        ip64_addr_4to6(&ip4addr, &ip6addr);
      } else {
        /* Try to lookup the hostname. If it fails, we initiate a hostname
           lookup. */
        if(resolv_lookup(host, &addr) != RESOLV_STATUS_CACHED) {
          resolv_query(host);
          PRINTF("Resolving host...\n");
          return HTTP_SOCKET_OK;
        }
        tcp_socket_connect(&s->s, addr, port);
        return HTTP_SOCKET_OK;
      }
    }
    tcp_socket_connect(&s->s, (uip_ipaddr_t *)&ip6addr, port);
    return HTTP_SOCKET_OK;
  } else {
    return HTTP_SOCKET_ERR;
  }
}