Re: Help with UDP data handler

"[email protected] [rabbit-semi]" <[email protected]>
Newsgroups gmane.comp.hardware.rabbit-semiconductor
Message-ID <[email protected]>
The callback functionality works fine for me, so I put this answer for the history:

I use Dynamic C 10.70 and RCM6700.

My udp_open:
  int local_port;
  local_port = 2000; // for example
  udp_open(&sock, local_port, -1, 0, comm_udp_rx_packet);

My callback function:
/*** BeginHeader comm_udp_rx_packet */
int comm_udp_rx_packet(int event, void *s, ll_Gather *g, _udp_datagram_info *info);
/*** EndHeader */
int comm_udp_rx_packet(int event, void *s, ll_Gather *g, _udp_datagram_info *info)
{
  comm_udp_header_t *rx_header; // this is custom struct
  char answer[5];
  char cs;
  int i;

  if (
    (event == UDP_DH_INDATA) &&
    (info->len >= sizeof(comm_udp_header_t)) &&
    (memcmp(g->data2, COMM_UDP_PREAMBLE, strlen(COMM_UDP_PREAMBLE)) == 0)
    )
  {
      rx_header = (comm_udp_header_t*)g->data2;
      rx_header->packet_id = ntohs(rx_header->packet_id);
      rx_header->data_length = ntohs(rx_header->data_length);

      // Checksum
      cs = 0;
      for (i=0; i<g->len2-1; i++) cs += g->data2[i];
      if (cs != g->data2[g->len2-1]) {
        answer[0] = ERROR_BAD_CHECKSUM;
        answer[1] = cs;
        answer[2] = g->data2[g->len2-1];
        comm_udp_tx_packet(COMM_UDP_PACKET_ERROR, answer, 3, rx_header);
      }
      else

      // Protocol Version
      if (rx_header->protocol_version != COMM_UDP_PROTOCOL_VERSION) {
        answer[0] = ERROR_BAD_PROTOCOL_VERSION;
        answer[1] = COMM_UDP_PROTOCOL_VERSION;
        answer[2] = rx_header->protocol_version;
        comm_udp_tx_packet(COMM_UDP_PACKET_ERROR, answer, 3, rx_header);
      }
      else {
        remote_ip = info->remip;
        remote_port = info->remport;

        // Good packet, so call the packet handling function.
        comm_udp_rx_packet(rx_header, g->data2); // g->data2 points to the data part in the udp datagram
      }
  }

  // The whole UDP RX traffic is handled in this function,
  // so I don't add any data to the socket buffer for future use
  return 1;
}



---=== Some info from the documentation ===---

Callback function parameters:

Event:
  UDP_DH_INDATA  - Normal received data
  UDP_DH_ICMPMSG - ICMP message received for this socket

ll_Gather: (documented in NET.LIB)
  typedef struct {
    byte iface;   // Destination interface
    byte spare;
    word len1;    // Length of root data section
    void * data1; // UDP header
    word len2;    // Length of first xmem section
    long data2;   // UDP data - xmem physical address
    word len3;    // Length of second xmem section
    long data3;   // not used
  } ll_Gather;

_udp_datagram_info: (documented in udp_peek)
  typedef struct {
    longword remip; // Remote host IP address
    word remport;   // Remote host port number
    int len;        // Length of datagram
    byte flags;     // Bit mask (defined below)
    byte iface;     // Interface number
  } _udp_datagram_info;
  The flags field may have one of the following values:
    UDI_ICMP_ERROR - This is an ICMP error entry.
    UDI_TOS_MASK - Type-of-service bit mask.
    UDI_BROADCAST_LL - Received on broadcast link layer address.
    UDI_BROADCAST_IP - Received on broadcast network (IP) address.

This callback function should return:
  0 - to continue with normal processing (i.e., add the datagram to the socket buffer)
  1 - to indicate that the datagram has been processed and should not be added to the socket buffer
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.