Microchip® Advanced Software Framework

udp.h File Reference
#include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "lwip/netif.h"
#include "lwip/ip_addr.h"
#include "lwip/ip.h"

Data Structures

struct  udp_hdr
 
struct  udp_pcb
 

Macros

#define udp_debug_print(udphdr)
 
#define udp_flags(pcb)   ((pcb)->flags)
 
#define UDP_FLAGS_CONNECTED   0x04U
 
#define UDP_FLAGS_MULTICAST_LOOP   0x08U
 
#define UDP_FLAGS_NOCHKSUM   0x01U
 
#define UDP_FLAGS_UDPLITE   0x02U
 
#define UDP_HLEN   8
 
#define udp_setflags(pcb, f)   ((pcb)->flags = (f))
 

Typedefs

typedef void(* udp_recv_fn )(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
 Function prototype for udp pcb receive callback functions addr and port are in same byte order as in the pcb The callback is responsible for freeing the pbuf if it's not used any more. More...
 

Functions

err_t udp_bind (struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
 Bind an UDP PCB. More...
 
err_t udp_connect (struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
 Connect an UDP PCB. More...
 
void udp_disconnect (struct udp_pcb *pcb)
 Disconnect a UDP PCB. More...
 
void udp_init (void)
 Initialize this module. More...
 
void udp_input (struct pbuf *p, struct netif *inp)
 Process an incoming UDP datagram. More...
 
struct udp_pcbudp_new (void)
 Create a UDP PCB. More...
 
void udp_recv (struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
 Set a receive callback for a UDP PCB. More...
 
void udp_remove (struct udp_pcb *pcb)
 Remove an UDP PCB. More...
 
err_t udp_send (struct udp_pcb *pcb, struct pbuf *p)
 Send data using UDP. More...
 
err_t udp_sendto (struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, u16_t dst_port)
 Send data to a specified address using UDP. More...
 
err_t udp_sendto_if (struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
 Send data to a specified address using UDP. More...
 

Variables

PACK_STRUCT_BEGIN struct udp_hdr PACK_STRUCT_STRUCT
 
struct udp_pcbudp_pcbs
 

#define udp_debug_print (   udphdr)

Referenced by udp_input().

#define udp_flags (   pcb)    ((pcb)->flags)
#define UDP_FLAGS_CONNECTED   0x04U
#define UDP_FLAGS_MULTICAST_LOOP   0x08U
#define UDP_FLAGS_NOCHKSUM   0x01U
#define UDP_FLAGS_UDPLITE   0x02U
#define UDP_HLEN   8

Referenced by udp_input().

#define udp_setflags (   pcb,
 
)    ((pcb)->flags = (f))

typedef void(* udp_recv_fn)(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)

Function prototype for udp pcb receive callback functions addr and port are in same byte order as in the pcb The callback is responsible for freeing the pbuf if it's not used any more.

ATTENTION: Be aware that 'addr' points into the pbuf 'p' so freeing this pbuf makes 'addr' invalid, too.

Parameters
arguser supplied argument (udp_pcb.recv_arg)
pcbthe udp_pcb which received data
pthe packet buffer that was received
addrthe remote IP address from which the packet was received
portthe remote port from which the packet was received

err_t udp_bind ( struct udp_pcb pcb,
ip_addr_t ipaddr,
u16_t  port 
)

Bind an UDP PCB.

Parameters
pcbUDP PCB to be bound with a local address ipaddr and port.
ipaddrlocal IP address to bind with. Use IP_ADDR_ANY to bind to all local interfaces.
portlocal 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.

Returns
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 Also
udp_disconnect()

References ERR_OK, ERR_USE, ip4_addr1_16, ip4_addr2_16, ip4_addr3_16, ip4_addr4_16, ip_addr_cmp, ip_addr_debug_print, ip_addr_isany, ip_addr_set, ip_get_option, udp_pcb::local_port, LWIP_ASSERT, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, udp_pcb::next, NULL, snmp_insert_udpidx_tree, SOF_REUSEADDR, UDP_DEBUG, udp_new_port(), and udp_pcbs.

Referenced by udp_connect().

err_t udp_connect ( struct udp_pcb pcb,
ip_addr_t ipaddr,
u16_t  port 
)

Connect an UDP PCB.

This will associate the UDP PCB with the remote address.

Parameters
pcbUDP PCB to be connected with remote address ipaddr and port.
ipaddrremote IP address to connect with.
portremote UDP port to connect with.
Returns
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 Also
udp_disconnect()

TODO: this functionality belongs in upper layers

References ERR_OK, ERR_RTE, udp_pcb::flags, ip4_addr1_16, ip4_addr2_16, ip4_addr3_16, ip4_addr4_16, netif::ip_addr, ip_addr_isany, ip_addr_set, ip_route(), udp_pcb::local_port, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, udp_pcb::next, NULL, udp_pcb::remote_port, udp_bind(), UDP_DEBUG, UDP_FLAGS_CONNECTED, udp_pcbs, and UDP_STATS_INC.

void udp_disconnect ( struct udp_pcb pcb)

Disconnect a UDP PCB.

Parameters
pcbthe udp pcb to disconnect.

References udp_pcb::flags, ip_addr_set_any, udp_pcb::remote_port, and UDP_FLAGS_CONNECTED.

void udp_init ( void  )

Initialize this module.

References UDP_ENSURE_LOCAL_PORT_RANGE, and udp_port.

Referenced by lwip_init().

void udp_input ( struct pbuf p,
struct netif inp 
)
struct udp_pcb* udp_new ( void  )

Create a UDP PCB.

Returns
The UDP PCB which was created. NULL if the PCB data structure could not be allocated.
See Also
udp_remove()

References memp_malloc(), and NULL.

void udp_recv ( struct udp_pcb pcb,
udp_recv_fn  recv,
void recv_arg 
)

Set a receive callback for a UDP PCB.

This callback will be called when receiving a datagram for the pcb.

Parameters
pcbthe pcb for wich to set the recv callback
recvfunction pointer of the callback function
recv_argadditional argument to pass to the callback function

References udp_pcb::recv, and udp_pcb::recv_arg.

void udp_remove ( struct udp_pcb pcb)

Remove an UDP PCB.

Parameters
pcbUDP PCB to be removed. The PCB is removed from the list of UDP PCB's and the data structure is freed from memory.
See Also
udp_new()

References memp_free(), udp_pcb::next, NULL, and snmp_delete_udpidx_tree.

err_t udp_send ( struct udp_pcb pcb,
struct pbuf p 
)

Send data using UDP.

Parameters
pcbUDP PCB used to send the data.
pchain of pbuf's to be sent.

The datagram will be sent to the current remote_ip & remote_port stored in pcb. If the pcb is not bound to a port, it will automatically be bound to a random port.

Returns
lwIP error code.
  • ERR_OK. Successful. No error occured.
  • ERR_MEM. Out of memory.
  • ERR_RTE. Could not find route to destination address.
  • More errors could be returned by lower protocol layers.
See Also
udp_disconnect() udp_sendto()

References udp_pcb::remote_port, and udp_sendto().

err_t udp_sendto ( struct udp_pcb pcb,
struct pbuf p,
ip_addr_t dst_ip,
u16_t  dst_port 
)

Send data to a specified address using UDP.

Parameters
pcbUDP PCB used to send the data.
pchain of pbuf's to be sent.
dst_ipDestination IP address.
dst_portDestination UDP port.

dst_ip & dst_port are expected to be in the same byte order as in the pcb.

If the PCB already has a remote address association, it will be restored after the data is sent.

Returns
lwIP error code (
See Also
udp_send for possible error codes)
udp_disconnect() udp_send()

Referenced by udp_send().

err_t udp_sendto_if ( struct udp_pcb pcb,
struct pbuf p,
ip_addr_t dst_ip,
u16_t  dst_port,
struct netif netif 
)

Send data to a specified address using UDP.

The netif used for sending can be specified.

This function exists mainly for DHCP, to be able to send UDP packets on a netif that is still down.

Parameters
pcbUDP PCB used to send the data.
pchain of pbuf's to be sent.
dst_ipDestination IP address.
dst_portDestination UDP port.
netifthe netif used for sending.

dst_ip & dst_port are expected to be in the same byte order as in the pcb.

Returns
lwIP error code (
See Also
udp_send for possible error codes)
udp_disconnect() udp_send()

PACK_STRUCT_BEGIN struct udp_hdr PACK_STRUCT_STRUCT
struct udp_pcb* udp_pcbs