Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

/opentcp/udp.c File Reference


Detailed Description

Author:
Version:
1.0
Date:
15.7.2002
Bug:
Warning:
Todo:
OpenTCP UDP implementation. All functions necessary for UDP packet processing are present here. Note that only a small subset of these functions must be used for "normal" applications that are using the UDP for communciation. For function declarations and lots of other usefull stuff see inet/tcp_ip.h.

For examples how to use UDP and write applications that communicate using UDP see inet/demo/main_demo.c and inet/demo/udp_demo.c.

Definition in file udp.c.

Go to the source code of this file.

Functions

INT8 udp_init (void)
 Initialize UDP socket pool.

INT8 udp_getsocket (UINT8 tos, INT32(*listener)(INT8, UINT8, UINT32, UINT16, UINT16, UINT16), UINT8 opts)
 Allocate a free socket in UDP socket pool.

INT8 udp_releasesocket (INT8 sochandle)
 Release a given socket.

INT8 udp_open (INT8 sochandle, UINT16 locport)
 Open a given UDP socket for communication.

INT8 udp_close (INT8 sochandle)
 Close given socket for communication.

INT16 udp_send (INT8 sochandle, UINT32 remip, UINT16 remport, UINT8 *buf, UINT16 blen, UINT16 dlen)
 Send data to remote host using given UDP socket.

INT16 process_udp_in (struct ip_frame *frame, UINT16 len)
 Process received UDP frame.

UINT16 udp_getfreeport (void)
 Returns next free (not used) local port number.


Variables

ucb udp_socket [NO_OF_UDPSOCKETS]
 UDP table holding socket parameters for every UDP socket.

udp_frame received_udp_packet
 Used for storing field information about the received UDP packet.


Function Documentation

INT8 udp_init void   
 

Author:
Date:
26.07.2002
Returns:
  • -1 - Error
  • >0 - Number of UDP sockets initialized
Warning:
  • This function must be invoked before any other UDP-related function is called
This function initializes UDP socket pool to get everything into a known state at startup.

Definition at line 118 of file udp.c.

References ucb::locport, ucb::opts, ucb::state, ucb::tos, UDP_OPT_CHECK_CS, UDP_OPT_SEND_CS, and UDP_STATE_FREE.

INT8 udp_getsocket UINT8    tos,
INT32(*    listener)(INT8, UINT8, UINT32, UINT16, UINT16, UINT16),
UINT8    opts
 

Author:
Date:
26.07.2002
Parameters:
tos  type of service for socket. For now nothing implemented so 0.
listener  pointer to callback function that will be invoked by the TCP/IP stack to inform socket application of UDP_DATA_ARRIVAL event (for now only this, in future maybe others!)
opts  Options for checksum generation & inspection. Can be one of the following:
Returns:
  • -1 - Error
  • >=0 - Handle to reserved socket
Invoke this function to try to obtain a free socket from UDP socket pool. Function returns a handle to the free socket that is later used for accessing the allocated socket.

Definition at line 175 of file udp.c.

References ucb::locport, ucb::opts, ucb::state, ucb::tos, UDP_OPT_CHECK_CS, UDP_OPT_SEND_CS, UDP_STATE_CLOSED, and UDP_STATE_FREE.

Referenced by bootpc_init(), dhcpc_init(), dns_init(), and tftps_init().

INT8 udp_releasesocket INT8    sochandle
 

Author:
Date:
26.07.2002
Parameters:
sochandle  handle of UDP socket to be released
Returns:
  • -1 - error
  • >=0 - OK (returns handle to release socket)
This function releases UDP socket. This means that the socket entry is marked as free and all of the ucb fields are initialized to default values.

Definition at line 244 of file udp.c.

References ucb::locport, ucb::opts, ucb::state, ucb::tos, UDP_OPT_CHECK_CS, UDP_OPT_SEND_CS, and UDP_STATE_FREE.

INT8 udp_open INT8    sochandle,
UINT16    locport
 

Author:
Date:
26.07.2002
Parameters:
sochandle  handle to socket to be opened
locport  local port number
Returns:
  • -1 - Error
  • >=0 - Handle to opened socket
This function binds local port to given UDP socket and opens the socket (virtually) in order to enable communication.

Definition at line 291 of file udp.c.

References ucb::locport, ucb::state, udp_getfreeport(), and UDP_STATE_OPENED.

Referenced by bootpc_init(), dhcpc_init(), dns_init(), and tftps_init().

INT8 udp_close INT8    sochandle
 

Author:
Date:
26.07.2002
Parameters:
sochandle  handle to socket to be closed
Returns:
  • -1 - Error
  • >=0 - handle to closed socket
Closes a given socket in order to disable further communication over it.

Definition at line 340 of file udp.c.

References ucb::state, and UDP_STATE_CLOSED.

INT16 udp_send INT8    sochandle,
UINT32    remip,
UINT16    remport,
UINT8 *    buf,
UINT16    blen,
UINT16    dlen
 

Author:
Date:
26.07.2002
Parameters:
sochandle  handle to UDP socket to use
remip  remote IP address to which data should be sent
remport  remote port number to which data should be sent
buf  pointer to data buffer (start of user data)
blen  buffer length in bytes (without space reserved at the beginning of buffer for headers)
dlen  length of user data to be sent (in bytes)
Returns:
  • -1 - Error (general error, e.g. parameters)
  • -2 - ARP or lower layer not ready, try again later
  • -3 - Socket closed or invalid local port
  • >0 - OK (number represents number of bytes actually sent)
Warning:
  • buf parameter is a pointer to data to be sent in user buffer. But note that there MUST be sufficient free buffer space before that data for UDP header (of UDP_HLEN size).
Use this function to send data over an already opened UDP socket.

Definition at line 395 of file udp.c.

References ip_checksum(), ip_checksum_buf(), IP_UDP, ucb::locport, ucb::opts, process_ip_out(), ucb::state, ucb::tos, UDP_HLEN, UDP_OPT_SEND_CS, and UDP_STATE_OPENED.

Referenced by bootpc_run(), dhcpc_send_message(), and get_host_by_name().

INT16 process_udp_in struct ip_frame   frame,
UINT16    len
 

Author:
Date:
15.07.2002
Parameters:
frame  pointer to received IP frame structure
len  length of data in bytes
Returns:
  • -1 - Error (packet not UDP, header errror or no socket for it)
  • >0 - Packet OK
Invoke this function to process received UDP frames. See main_demo.c for an example on how to accomplish this.

Definition at line 550 of file udp.c.

References udp_frame::buf_index, ip_frame::buf_index, udp_frame::checksum, ip_frame::dip, udp_frame::dport, ip_checksum(), IP_UDP, ucb::locport, NETWORK_RECEIVE_INITIALIZE, ucb::opts, ip_frame::protocol, RECEIVE_NETWORK_B, ip_frame::sip, udp_frame::sport, ucb::state, udp_frame::tlen, UDP_EVENT_DATA, UDP_HLEN, UDP_OPT_CHECK_CS, and UDP_STATE_OPENED.

UINT16 udp_getfreeport void   
 

Author:
Date:
19.10.2002
Returns:
  • 0 - no free ports!
  • >0 - free local TCP port number
Function attempts to find new local port number that can be used to establish a connection.

Definition at line 684 of file udp.c.

References ucb::locport, ucb::state, UDP_PORTS_END, and UDP_STATE_CLOSED.

Referenced by udp_open().


Variable Documentation

struct ucb udp_socket[NO_OF_UDPSOCKETS]
 

UDP table is an array of ucb structures holding all of the necessary information about the state, listener, port numbers and other info about the UDP sockets opened. Number of UDP sockets that can be opened at any given time is defined by the NO_OF_UDPSOCKETS and may be changed in order to change the amount of used RAM memory. See ucb definition for more information about the structure itself.

Definition at line 94 of file udp.c.

struct udp_frame received_udp_packet
 

Various fields from the received UDP packet are stored in this variable. See udp_frame definition for struct information.

Definition at line 101 of file udp.c.


Generated on Sun Aug 3 20:33:00 2003 for OpenTCP by doxygen1.2.18