⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udp.c

📁 lwip-1.4.0
💻 C
📖 第 1 页 / 共 3 页
字号:
      /* chksum zero must become 0xffff, as zero means 'no checksum' */      if (udpchksum == 0x0000) {        udpchksum = 0xffff;      }      udphdr->chksum = udpchksum;    }#endif /* CHECKSUM_GEN_UDP */    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));    /* output to IP */#if LWIP_NETIF_HWADDRHINT    netif->addr_hint = &(pcb->addr_hint);#endif /* LWIP_NETIF_HWADDRHINT*/    err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);#if LWIP_NETIF_HWADDRHINT    netif->addr_hint = NULL;#endif /* LWIP_NETIF_HWADDRHINT*/  }  /* TODO: must this be increased even if error occured? */  snmp_inc_udpoutdatagrams();  /* did we chain a separate header pbuf earlier? */  if (q != p) {    /* free the header pbuf */    pbuf_free(q);    q = NULL;    /* p is still referenced by the caller, and will live on */  }  UDP_STATS_INC(udp.xmit);  return err;}/** * Bind an UDP PCB. * * @param pcb UDP PCB to be bound with a local address ipaddr and port. * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to * bind to all local interfaces. * @param port local UDP port to bind with. Use 0 to automatically bind * to a random port between UDP_LOCAL_PORT_RANGE_START and * UDP_LOCAL_PORT_RANGE_END. * * ipaddr & port are expected to be in the same byte order as in the pcb. * * @return lwIP error code. * - ERR_OK. Successful. No error occured. * - ERR_USE. The specified ipaddr and port are already bound to by * another UDP PCB. * * @see udp_disconnect() */err_tudp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port){  struct udp_pcb *ipcb;  u8_t rebind;  LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));  ip_addr_debug_print(UDP_DEBUG, ipaddr);  LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));  rebind = 0;  /* Check for double bind and rebind of the same pcb */  for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {    /* is this UDP PCB already on active list? */    if (pcb == ipcb) {      /* pcb may occur at most once in active list */      LWIP_ASSERT("rebind == 0", rebind == 0);      /* pcb already in list, just rebind */      rebind = 1;    }    /* By default, we don't allow to bind to a port that any other udp       PCB is alread bound to, unless *all* PCBs with that port have tha       REUSEADDR flag set. */#if SO_REUSE    else if (((pcb->so_options & SOF_REUSEADDR) == 0) &&             ((ipcb->so_options & SOF_REUSEADDR) == 0)) {#else /* SO_REUSE */    /* port matches that of PCB in list and REUSEADDR not set -> reject */    else {#endif /* SO_REUSE */      if ((ipcb->local_port == port) &&          /* IP address matches, or one is IP_ADDR_ANY? */          (ip_addr_isany(&(ipcb->local_ip)) ||           ip_addr_isany(ipaddr) ||           ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {        /* other PCB already binds to this local IP and port */        LWIP_DEBUGF(UDP_DEBUG,                    ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));        return ERR_USE;      }    }  }  ip_addr_set(&pcb->local_ip, ipaddr);  /* no port specified? */  if (port == 0) {#ifndef UDP_LOCAL_PORT_RANGE_START/* From http://www.iana.org/assignments/port-numbers:   "The Dynamic and/or Private Ports are those from 49152 through 65535" */#define UDP_LOCAL_PORT_RANGE_START  0xc000#define UDP_LOCAL_PORT_RANGE_END    0xffff#endif    port = UDP_LOCAL_PORT_RANGE_START;    ipcb = udp_pcbs;    while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {      if (ipcb->local_port == port) {        /* port is already used by another udp_pcb */        port++;        /* restart scanning all udp pcbs */        ipcb = udp_pcbs;      } else {        /* go on with next udp pcb */        ipcb = ipcb->next;      }    }    if (ipcb != NULL) {      /* no more ports available in local range */      LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));      return ERR_USE;    }  }  pcb->local_port = port;  snmp_insert_udpidx_tree(pcb);  /* pcb not active yet? */  if (rebind == 0) {    /* place the PCB on the active list if not already there */    pcb->next = udp_pcbs;    udp_pcbs = pcb;  }  LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,              ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",               ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),               ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),               pcb->local_port));  return ERR_OK;}/** * Connect an UDP PCB. * * This will associate the UDP PCB with the remote address. * * @param pcb UDP PCB to be connected with remote address ipaddr and port. * @param ipaddr remote IP address to connect with. * @param port remote UDP port to connect with. * * @return lwIP error code * * ipaddr & port are expected to be in the same byte order as in the pcb. * * The udp pcb is bound to a random local port if not already bound. * * @see udp_disconnect() */err_tudp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port){  struct udp_pcb *ipcb;  if (pcb->local_port == 0) {    err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);    if (err != ERR_OK) {      return err;    }  }  ip_addr_set(&pcb->remote_ip, ipaddr);  pcb->remote_port = port;  pcb->flags |= UDP_FLAGS_CONNECTED;/** TODO: this functionality belongs in upper layers */#ifdef LWIP_UDP_TODO  /* Nail down local IP for netconn_addr()/getsockname() */  if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {    struct netif *netif;    if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {      LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));      UDP_STATS_INC(udp.rterr);      return ERR_RTE;    }    /** TODO: this will bind the udp pcb locally, to the interface which        is used to route output packets to the remote address. However, we        might want to accept incoming packets on any interface! */    pcb->local_ip = netif->ip_addr;  } else if (ip_addr_isany(&pcb->remote_ip)) {    pcb->local_ip.addr = 0;  }#endif  LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,              ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",               ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),               ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),               pcb->local_port));  /* Insert UDP PCB into the list of active UDP PCBs. */  for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {    if (pcb == ipcb) {      /* already on the list, just return */      return ERR_OK;    }  }  /* PCB not yet on the list, add PCB now */  pcb->next = udp_pcbs;  udp_pcbs = pcb;  return ERR_OK;}/** * Disconnect a UDP PCB * * @param pcb the udp pcb to disconnect. */voidudp_disconnect(struct udp_pcb *pcb){  /* reset remote address association */  ip_addr_set_any(&pcb->remote_ip);  pcb->remote_port = 0;  /* mark PCB as unconnected */  pcb->flags &= ~UDP_FLAGS_CONNECTED;}/** * Set a receive callback for a UDP PCB * * This callback will be called when receiving a datagram for the pcb. * * @param pcb the pcb for wich to set the recv callback * @param recv function pointer of the callback function * @param recv_arg additional argument to pass to the callback function */voidudp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg){  /* remember recv() callback and user data */  pcb->recv = recv;  pcb->recv_arg = recv_arg;}/** * Remove an UDP PCB. * * @param pcb UDP PCB to be removed. The PCB is removed from the list of * UDP PCB's and the data structure is freed from memory. * * @see udp_new() */voidudp_remove(struct udp_pcb *pcb){  struct udp_pcb *pcb2;  snmp_delete_udpidx_tree(pcb);  /* pcb to be removed is first in list? */  if (udp_pcbs == pcb) {    /* make list start at 2nd pcb */    udp_pcbs = udp_pcbs->next;    /* pcb not 1st in list */  } else {    for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {      /* find pcb in udp_pcbs list */      if (pcb2->next != NULL && pcb2->next == pcb) {        /* remove pcb from list */        pcb2->next = pcb->next;      }    }  }  memp_free(MEMP_UDP_PCB, pcb);}/** * Create a UDP PCB. * * @return The UDP PCB which was created. NULL if the PCB data structure * could not be allocated. * * @see udp_remove() */struct udp_pcb *udp_new(void){  struct udp_pcb *pcb;  pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);  /* could allocate UDP PCB? */  if (pcb != NULL) {    /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0     * which means checksum is generated over the whole datagram per default     * (recommended as default by RFC 3828). */    /* initialize PCB to all zeroes */    memset(pcb, 0, sizeof(struct udp_pcb));    pcb->ttl = UDP_TTL;  }  return pcb;}#if UDP_DEBUG/** * Print UDP header information for debug purposes. * * @param udphdr pointer to the udp header in memory. */voidudp_debug_print(struct udp_hdr *udphdr){  LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));  LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src port, dest port)\n",                          ntohs(udphdr->src), ntohs(udphdr->dest)));  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));  LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, chksum)\n",                          ntohs(udphdr->len), ntohs(udphdr->chksum)));  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));}#endif /* UDP_DEBUG */#endif /* LWIP_UDP */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -