Libraries
network_udp/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program sends and receives four packets via two UDP sockets and logs
// info and error messages. Program uses RegisterPrintMessage,
// StartLoggingMessages, CreateUnicastUDPSocketDevice,
// RegisterReceivePacketFromSocket and SendPacketFromUDPSocketDevice API
// functions.
//
// ////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "ds_shared.h"
#include "trek_error.h"
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif
// ////////////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
if (mess_struct_ptr->category == MSG_CAT_INFO ||
mess_struct_ptr->category == MSG_CAT_INFO_ALERT ||
mess_struct_ptr->category == MSG_CAT_ERROR ||
mess_struct_ptr->category == MSG_CAT_ERROR_ALERT)
{
printf("%s\t%s\n",GetMessageCategoryAsString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
}
// ////////////////////////////////////////////////////////////////////////////
//
// ReceivePacket's packet_buffer_ptr points to the newly received packet.
//
// ////////////////////////////////////////////////////////////////////////////
void ReceivePacket (const char *device_key,
int packet_length,
unsigned char *packet_buffer_ptr,
const char *received_from_ip_address,
unsigned short recieved_from_port)
{
// Print the data zone packets.
printf("Data\t%s\n",(char *)packet_buffer_ptr + 5);
}
// ////////////////////////////////////////////////////////////////////////////
//
// CreateDataPackets creates the data packets with a three byte header.
// The first three header bytes contains a sync hex pattern = fafbfc.
// The fourth and fifth header bytes contain the overall packet length in big
// endian byte order.
//
// ////////////////////////////////////////////////////////////////////////////
void CreateDataPackets(unsigned char *packet1_ptr,
unsigned char *packet2_ptr,
unsigned char *packet3_ptr,
unsigned char *packet4_ptr,
unsigned short *length1_ptr,
unsigned short *length2_ptr,
unsigned short *length3_ptr,
unsigned short *length4_ptr)
{
// Determine if this is a little endian or big endian platform .
unsigned short tester= 1;
unsigned char tester_byte[1];
unsigned char sync_pattern[3];
unsigned short big_endian_order = 1;
memcpy(tester_byte,&tester,1);
if (tester_byte[0] == 0x01)
{
big_endian_order = 0;
}
// Zero fill the packets prior to populating.
memset(packet1_ptr,0x00,50);
memset(packet2_ptr,0x00,50);
memset(packet3_ptr,0x00,50);
memset(packet4_ptr,0x00,50);
// Create a five byte header for all the packets. The header is populated
// with the sync bytes and a big endian two byte overall packet length.
// Populate packet data zone
strcpy((char *)packet1_ptr+5,"Mary had a little lamb");
strcpy((char *)packet2_ptr+5,"Its feet were black as soot");
strcpy((char *)packet3_ptr+5,"And into Mary's bread and jam ");
strcpy((char *)packet4_ptr+5,"Its sooty foot it put");
// Get packet length including 5 byte header and null terminator.
*length1_ptr = strlen((char *)packet1_ptr+5) + 6;
*length2_ptr = strlen((char *)packet2_ptr+5) + 6;
*length3_ptr = strlen((char *)packet3_ptr+5) + 6;
*length4_ptr = strlen((char *)packet4_ptr+5) + 6;
// Populate headers.
// Populate packet length field.
if (big_endian_order == 1)
{
memmove(packet1_ptr+3,length1_ptr,2);
memmove(packet2_ptr+3,length2_ptr,2);
memmove(packet3_ptr+3,length3_ptr,2);
memmove(packet4_ptr+3,length4_ptr,2);
}
else
{
memmove(packet1_ptr+4,length1_ptr,1);
memmove(packet2_ptr+4,length2_ptr,1);
memmove(packet3_ptr+4,length3_ptr,1);
memmove(packet4_ptr+4,length4_ptr,1);
}
// Populate sync field.
sync_pattern[0] = 0xfa;
sync_pattern[1] = 0xfb;
sync_pattern[2] = 0xfc;
memcpy(packet1_ptr,sync_pattern,3);
memcpy(packet2_ptr,sync_pattern,3);
memcpy(packet3_ptr,sync_pattern,3);
memcpy(packet4_ptr,sync_pattern,3);
}
// ////////////////////////////////////////////////////////////////////////////
//
// PrintAvailableIPAddesses uses the PopulateIPAddressStructArray function to
// identify and print the host's available IPv4 and IPv6 addresses.
//
// ////////////////////////////////////////////////////////////////////////////
void PrintAvailableIPAddesses()
{
unsigned int number_of_ip_address_structs;
ip_address_struct_type *ip_address_struct_array_ptr = NULL;
ip_address_struct_type ip_address_struct;
unsigned int i;
PopulateIPAddressStructArray(&number_of_ip_address_structs,
&ip_address_struct_array_ptr);
printf("\nIP Addresses:\n\n");
for (i=0; i<number_of_ip_address_structs; i++)
{
ip_address_struct = ip_address_struct_array_ptr[i];
if (ip_address_struct_array_ptr[i].ip_address_version == IPV4)
{
printf("IPv4 address: %s\n",
ip_address_struct_array_ptr[i].ip_address);
}
else
{
switch (ip_address_struct_array_ptr[i].ip_address_v6_category)
{
printf("IPv6 address: %s\tIPv6 category: GLOBAL_FIXED\n",
ip_address_struct_array_ptr[i].ip_address);
break;
printf("IPv6 address: %s\tIPv6 category: GLOBAL_TEMPORARY\n",
ip_address_struct_array_ptr[i].ip_address);
break;
case LINK_LOCAL:
printf("IPv6 address: %s\tIPv6 category: LINK_LOCAL\n",
ip_address_struct_array_ptr[i].ip_address);
break;
printf("IPv6 address: %s\tIPv6 category: TAREDO_TUNNELING\n",
ip_address_struct_array_ptr[i].ip_address);
break;
printf("IPv6 address: %s\tIPv6 category: IPV6_TO_IPV4_TUNNELING\n",
ip_address_struct_array_ptr[i].ip_address);
break;
case MULTICAST:
printf("IPv6 address: %s\tIPv6 category: MULTICAST\n",
ip_address_struct_array_ptr[i].ip_address);
break;
case OTHER:
printf("IPv6 address: %s\tIPv6 category: OTHER\n",
ip_address_struct_array_ptr[i].ip_address);
break;
default:
printf("IPv6 address: %s\tIPv6 category: UNDEFINED_IP_ADDRESS_V6_CATEGORY\n",
ip_address_struct_array_ptr[i].ip_address);
break;
}
}
}
// NOTE: Problems may develop when freeing the memory associated
// with the ip_address_struct_array_ptr if the DS library's compiler
// or run time environment does not match the application code's
// compiler or run time environment. This method is provided to
// avoid those problems. The code, "free(ip_address_struct_array_ptr)"
// will work if the library and application code's compilers
// are the same.
FreeIPAddressStructArrayMemoryAlloc(ip_address_struct_array_ptr);
}
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program sends and receives four packets via two UDP sockets and logs
// info and error messages. Program uses RegisterPrintMessage,
// StartLoggingMessages, CreateUnicastUDPSocketDevice,
// RegisterReceivePacketFromSocket and SendPacketFromUDPSocketDevice API
// functions.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
char ip_address[50];
unsigned short source_port;
unsigned short receive_port;
unsigned int receive_queue_size;
unsigned int receive_buffer_size;
char source_device_key[50];
char receive_device_key[50];
unsigned char packet1[50];
unsigned char packet2[50];
unsigned char packet3[50];
unsigned char packet4[50];
unsigned short length1;
unsigned short length2;
unsigned short length3;
unsigned short length4;
unsigned int key_buffer_size;
char log_path[256];
char log_filename[256];
// Example code using the PopulateIPAddressStructArray function to print
// the host's available ip addresses.
PrintAvailableIPAddesses();
// Create data packets.
CreateDataPackets(packet1,
packet2,
packet3,
packet4,
&length1,
&length2,
&length3,
&length4);
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during initialization.
RegisterMessage(&PrintTheMessage);
printf("\nNetwork UDP\n\n");
// Create a log file in the home/<username> or Users/<username> directory
// by passing a empty string for directory path. Only log error and
// information messages.
strcpy(log_path,"");
strcpy(log_filename,"network_udp_log_file");
log_filename,
// Create a UDP source socket device.
strcpy(ip_address,"127.0.0.1"); // Use loopback address.
source_port = 20130;
// A receive queue or buffer is not required since this socket is only
// sending packets.
receive_queue_size = 0;
receive_buffer_size = 0;
key_buffer_size = sizeof(source_device_key);
source_port,
receive_queue_size,
receive_buffer_size,
&key_buffer_size,
source_device_key) != SUCCESS)
{
return 0;
}
// Create a UDP receive socket device
receive_port = 20140;
// If the receive socket is receiving hundreds of packets a second and
// receive queue minimizes the chances of packets being dropped.
receive_queue_size = 100;
receive_buffer_size = 100;
key_buffer_size = sizeof(receive_device_key);
receive_port,
receive_queue_size,
receive_buffer_size,
&key_buffer_size,
receive_device_key) != SUCCESS)
{
return 0;
}
// Associate the RecieveDeviceData callback function with the receive
// socket device so all packets received by the device will
// be processed by RecieveDeviceData callback function.
if (RegisterReceivePacketFromSocket (receive_device_key,
&ReceivePacket) != SUCCESS)
{
return 0;
}
// Send the four data packets to the receive socket via the source socket.
// The source socket was not connected to the receive socket so the
// SendPacket function requires a destination IP address and port for each
// transmission.
if (SendPacketFromUDPSocketDevice (source_device_key,
length1,packet1,
ip_address,
receive_port) == SUCCESS)
{
if (SendPacketFromUDPSocketDevice (source_device_key,
length2,
packet2,
ip_address,
receive_port) == SUCCESS)
{
if (SendPacketFromUDPSocketDevice (source_device_key,
length3,
packet3,
ip_address,
receive_port) == SUCCESS)
{
SendPacketFromUDPSocketDevice (source_device_key,
length4,
packet4,
ip_address,
receive_port);
}
}
}
// Allow time to print and log messages.
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
// Close the log file. A timetag is appended to the log file name.
return 0;
}