TReK ANSI-C  5.3.3
All ANSI-C APIs
network_multicast/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program sends and receives four multicast packets using two UDP
// sockets and logs info and error messages. Program joins two multcast groups
// and uses RegisterPrintMessage, StartLoggingMessages, LogMessage,
// CreateUnicastUDPSocketDevice, CreateIPv4MulticastUDPSocketDevice,
// JoinMulticastGroup, 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);
return;
}
// ////////////////////////////////////////////////////////////////////////////
//
// 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 multicast packets using two UDP
// sockets and logs info and error messages. Program joins two multcast groups
// and uses RegisterPrintMessage, StartLoggingMessages, LogMessage,
// CreateUnicastUDPSocketDevice, CreateIPv4MulticastUDPSocketDevice,
// JoinMulticastGroup, RegisterReceivePacketFromSocket and
// SendPacketFromUDPSocketDevice API functions.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
char ip_address[50];
char multi_ip_address_1[50];
char multi_ip_address_2[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];
boolean_type disable_loopback_flag;
unsigned short multicast_ttl;
// 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 Multicast\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_mulicast_log_file");
log_filename,
// Add a user message to the log file
LogMessage(MSG_CAT_INFO,"Multicast log.");
// Create a UDP source socket device.
strcpy(ip_address,"127.0.0.1"); // use loopback address
source_port = 20135;
// 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);
if (CreateUnicastUDPSocketDevice(ip_address,source_port,
receive_queue_size,
receive_buffer_size,
&key_buffer_size,
source_device_key) != SUCCESS)
{
return 0;
}
// Create a Multicast UDP receive socket device
receive_port = 20145;
// 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(source_device_key);
// Set to false to allow host to receive multicast packets that are
// sent using the same network interface card.
disable_loopback_flag = FALSE_OR_NO;
receive_queue_size,
receive_buffer_size,
disable_loopback_flag,
&key_buffer_size,
receive_device_key) != SUCCESS)
{
return 0;
}
// multicast time-to-live indicates the scope or range in which a
// packet may be forwarded. By convention:
// 0 is restricted to the same host
// 1 is restricted to the same subnet
// 32 is restricted to the same site
// 64 is restricted to the same region
// 128 is restricted to the same continent
// 255 is unrestricted
multicast_ttl = 1;
if (SetMulticastTTL(source_device_key,multicast_ttl) != SUCCESS)
{
return 0;
}
// Join the first multicast group.
// Multicast group IPV4 address range 224.0.0.0 - 239.255.255.255.
strcpy(multi_ip_address_1,"224.0.0.1");
if (JoinMulticastGroup( ip_address,
multi_ip_address_1,
receive_device_key) != SUCCESS)
{
return 0;
}
// Join the second multicast group.
// Multicast group IPV4 address range 224.0.0.0 - 239.255.255.255.
strcpy(multi_ip_address_2,"224.0.0.2");
if (JoinMulticastGroup(ip_address,
multi_ip_address_2,
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
// the RecieveDeviceData callback function.
if (RegisterReceivePacketFromSocket (receive_device_key,
&ReceivePacket) != SUCCESS)
{
return 0;
}
// Send first and second data packets to the first multicast group address.
// 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,
multi_ip_address_1,
receive_port) == SUCCESS)
{
if (SendPacketFromUDPSocketDevice (source_device_key,
length2,packet2,
multi_ip_address_1,
receive_port) == SUCCESS)
{
// Send third and fourth data packets to the second multicast
// group address
if (SendPacketFromUDPSocketDevice (source_device_key,
length3,packet3,
multi_ip_address_2,
receive_port) == SUCCESS)
{
SendPacketFromUDPSocketDevice (source_device_key,
length4,
packet4,
multi_ip_address_2,
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;
}
Message codes and structure definition for the Device Service library.
@ GLOBAL_FIXED
Global fixed.
Definition: ds_shared.h:130
@ MULTICAST
Multicast.
Definition: ds_shared.h:135
@ TAREDO_TUNNELING
Taredo tunneling.
Definition: ds_shared.h:133
@ IPV6_TO_IPV4_TUNNELING
IPv6 to IPv4 tunneling.
Definition: ds_shared.h:134
@ LINK_LOCAL
Link local.
Definition: ds_shared.h:132
@ OTHER
Other.
Definition: ds_shared.h:136
@ GLOBAL_TEMPORARY
GLobal temporary.
Definition: ds_shared.h:131
boolean_type
Boolean enumeration.
Definition: ds_shared.h:104
@ FALSE_OR_NO
False.
Definition: ds_shared.h:105
@ IPV4
IP version 4 address.
Definition: ds_shared.h:112
Structure of parameters needed for IP address identification.
Definition: ds_shared.h:180
Structure of parameters needed for message support.
Definition: trek_toolkit_common_api_ansi_c.h:74
char message[MAX_MESSAGE_SIZE]
Message.
Definition: trek_toolkit_common_api_ansi_c.h:77
enum message_category category
Message category (e.g., MSG_CAT_ERROR, MSG_CAT_ERROR_ALERT, MSG_CAT_WARNING, MSG_CAT_WARNING_ALERT,...
Definition: trek_toolkit_common_api_ansi_c.h:76
An ANSI C Data Service API.
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION CreateUnicastUDPSocketDevice(const char *ip_address, unsigned short port, unsigned int receive_queue_size, unsigned int receive_buffer_size, unsigned int *device_key_buffer_size_ptr, char *device_key)
Creates a UDP socket to send and receive packets at the specified IP address and port....
Definition: toolkit_ds_api_ansi_c.cpp:126
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION DSCleanUp()
Initiates a graceful shutdown of the Device Service library and all supporting device libraries,...
Definition: toolkit_ds_api_ansi_c.cpp:3843
void EXPORT_THIS_TOOLKIT_DS_C_FUNCTION FreeIPAddressStructArrayMemoryAlloc(ip_address_struct_type *ip_address_struct_array_ptr)
Frees the memory associated with the ip_address_struct_array_ptr that was returned by PopulateIPAddre...
Definition: toolkit_ds_api_ansi_c.cpp:3804
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION SendPacketFromUDPSocketDevice(const char *device_key, int packet_length, unsigned char *packet_buffer_ptr, const char *send_to_ip_address, unsigned short send_to_port)
Sends a packet from UDP socket device to the IP address and port number of a corresponding UDP socket...
Definition: toolkit_ds_api_ansi_c.cpp:1921
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION CreateIPv4MulticastUDPSocketDevice(unsigned short port, unsigned int receive_queue_size, unsigned int receive_buffer_size, boolean_type disable_loopback_flag, unsigned int *device_key_buffer_size_ptr, char *device_key)
Creates a UDP socket to send and receive IP version 4 multicast packets at the specified port....
Definition: toolkit_ds_api_ansi_c.cpp:318
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION LogMessage(enum message_category category, const char *message)
Logs a user message in the log file. The maximum null terminated message size is 512 bytes.
Definition: toolkit_ds_api_ansi_c.cpp:3426
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION JoinMulticastGroup(const char *ip_address, const char *multicast_group_address, const char *device_key)
Associates a mulitcast socket with a multicast group address. A multicast socket must join a multicas...
Definition: toolkit_ds_api_ansi_c.cpp:1108
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StopLoggingMessages()
Stops logging messages to a file, closes the log file and renames the log file by concatenating the l...
Definition: toolkit_ds_api_ansi_c.cpp:3479
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterMessage(void(*function_ptr)(message_struct_type *message_struct_ptr))
Register a callback function to receive and process messages issued by the DS library.
Definition: toolkit_ds_api_ansi_c.cpp:3951
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterReceivePacketFromSocket(const char *device_key, void(*function_ptr)(const char *device_key, int packet_length, unsigned char *packet_buffer_ptr, const char *received_from_ip_address, unsigned short received_from_port))
Register a callback function to receive packets from a socket device.
Definition: toolkit_ds_api_ansi_c.cpp:4088
void EXPORT_THIS_TOOLKIT_DS_C_FUNCTION PopulateIPAddressStructArray(unsigned int *number_of_ip_address_structs_ptr, ip_address_struct_type **ip_address_struct_array_ptr)
Populates an array of ip_address_struct with the valid IP addresses of the host platform.
Definition: toolkit_ds_api_ansi_c.cpp:3760
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION SetMulticastTTL(const char *device_key, unsigned short multicast_ttl)
Sets the time to live for a socket that is sending packets to a multicast address.
Definition: toolkit_ds_api_ansi_c.cpp:1206
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StartLoggingMessages(const char *log_file_path, const char *log_filename, boolean_type log_debug_messages)
Starts logging messages to a file.
Definition: toolkit_ds_api_ansi_c.cpp:3339
Error codes for the DS API (starts at 50001)
Command codes for TReK.
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8
The commonly shared macros, structures and functions.
@ MSG_CAT_INFO_ALERT
Information alert message (supports information alert pop up dialog box)
Definition: trek_toolkit_common_api_ansi_c.h:61
@ MSG_CAT_INFO
Information message.
Definition: trek_toolkit_common_api_ansi_c.h:60
@ MSG_CAT_ERROR
Error message.
Definition: trek_toolkit_common_api_ansi_c.h:56
@ MSG_CAT_ERROR_ALERT
Error alert message (supports error alert pop up dialog box)
Definition: trek_toolkit_common_api_ansi_c.h:57
const char EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION * GetMessageCategoryAsString(enum message_category input)
Converts an enumerated message category value into its equivalent character string.
Definition: trek_toolkit_common_api_ansi_c.cpp:45