示例#1
0
/*---------------------------------------------------------------------------*/
static int prepare_and_send_buf(coap_context_t *ctx, session_t *session,
				uint8_t *data, size_t len)
{
  struct net_buf *buf;
  int max_data_len;

  /* This net_buf gets sent to network, so it is not released
   * by this function unless there was an error and buf was
   * not actually sent.
   */
  buf = ip_buf_get_tx(ctx->net_ctx);
  if (!buf) {
    len = -ENOBUFS;
    goto out;
  }

  max_data_len = IP_BUF_MAX_DATA - UIP_IPUDPH_LEN;

  PRINTF("%s: reply to peer data %p len %d\n", __FUNCTION__, data, len);

  if (len > max_data_len) {
    PRINTF("%s: too much (%d bytes) data to send (max %d bytes)\n",
	  __FUNCTION__, len, max_data_len);
    ip_buf_unref(buf);
    len = -EINVAL;
    goto out;
  }

  /* Note that we have reversed the addresses here
   * because net_reply() will reverse them again.
   */
#ifdef CONFIG_NETWORKING_WITH_IPV6
  uip_ip6addr_copy(&NET_BUF_IP(buf)->destipaddr, (uip_ip6addr_t *)&ctx->my_addr.in6_addr);
  uip_ip6addr_copy(&NET_BUF_IP(buf)->srcipaddr,
		   (uip_ip6addr_t *)&session->addr.ipaddr);
#else
  uip_ip4addr_copy(&NET_BUF_IP(buf)->destipaddr,
		   (uip_ip4addr_t *)&ctx->my_addr.in_addr);
  uip_ip4addr_copy(&NET_BUF_IP(buf)->srcipaddr,
		   (uip_ip4addr_t *)&session->addr.ipaddr);
#endif
  NET_BUF_UDP(buf)->destport = uip_ntohs(ctx->my_port);
  NET_BUF_UDP(buf)->srcport = session->addr.port;

  uip_set_udp_conn(buf) = net_context_get_udp_connection(ctx->net_ctx);

  memcpy(net_buf_add(buf, len), data, len);
  ip_buf_appdatalen(buf) = len;
  ip_buf_appdata(buf) = buf->data + ip_buf_reserve(buf);

  if (net_reply(ctx->net_ctx, buf)) {
    ip_buf_unref(buf);
  }
out:
  return len;
}
示例#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;
}
示例#3
0
/*---------------------------------------------------------------------------*/
struct ip64_addrmap_entry *
ip64_addrmap_create(const uip_ip6addr_t *ip6addr,
                    uint16_t ip6port,
                    const uip_ip4addr_t *ip4addr,
                    uint16_t ip4port,
                    uint8_t protocol)
{
    struct ip64_addrmap_entry *m;

    check_age();
    m = memb_alloc(&entrymemb);
    if(m == NULL) {
        /* We could not allocate an entry, try to recycle one and try to
           allocate again. */
        if(recycle()) {
            m = memb_alloc(&entrymemb);
        }
    }
    if(m != NULL) {
        uip_ip4addr_copy(&m->ip4addr, ip4addr);
        m->ip4port = ip4port;
        uip_ip6addr_copy(&m->ip6addr, ip6addr);
        m->ip6port = ip6port;
        m->protocol = protocol;
        m->flags = FLAGS_NONE;
        m->ip6to4 = 1;
        m->ip4to6 = 0;
        timer_set(&m->timer, 0);

        /* Pick a new, unused local port. First make sure that the
           mapped_port number does not belong to any active connection. If
           so, we keep increasing the mapped_port until we're free. */
        {
            struct ip64_addrmap_entry *n;
            n = list_head(entrylist);
            while(n != NULL) {
                if(n->mapped_port == mapped_port) {
                    increase_mapped_port();
                    n = list_head(entrylist);
                } else {
                    n = list_item_next(m);
                }
            }
        }
        m->mapped_port = mapped_port;
        increase_mapped_port();

        list_add(entrylist, m);
        return m;
    }
    return NULL;
}
示例#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;
  }
}
示例#5
0
void uip_neighbor_add(struct uip_stack *ustack,
		      struct in6_addr *addr6, struct uip_eth_addr *addr)
{
	int i, oldest;
	u8_t oldest_time;
	char buf[INET6_ADDRSTRLEN];

	inet_ntop(AF_INET6, addr6, buf, sizeof(buf));

	pthread_mutex_lock(&ustack->lock);

	/* Find the first unused entry or the oldest used entry. */
	oldest_time = 0;
	oldest = 0;
	for (i = 0; i < UIP_NEIGHBOR_ENTRIES; ++i) {
		if (ustack->neighbor_entries[i].time == MAX_TIME) {
			oldest = i;
			break;
		}
		if (uip_ip6addr_cmp
		    (ustack->neighbor_entries[i].ipaddr.s6_addr, addr6)) {
			oldest = i;
			break;
		}
		if (ustack->neighbor_entries[i].time > oldest_time) {
			oldest = i;
			oldest_time = ustack->neighbor_entries[i].time;
		}
	}

	/* Use the oldest or first free entry (either pointed to by the
	   "oldest" variable). */
	ustack->neighbor_entries[oldest].time = 0;
	uip_ip6addr_copy(ustack->neighbor_entries[oldest].ipaddr.s6_addr,
			 addr6);
	memcpy(&ustack->neighbor_entries[oldest].mac_addr, addr,
	       sizeof(struct uip_eth_addr));

	LOG_DEBUG("Adding neighbor %s with "
		  "mac address %02x:%02x:%02x:%02x:%02x:%02x at %d",
		  buf, addr->addr[0], addr->addr[1], addr->addr[2],
		  addr->addr[3], addr->addr[4], addr->addr[5], oldest);

	pthread_mutex_unlock(&ustack->lock);
}