/*
 * C code for sending a simple fixed-length header
 * with a data buffer following the header.
 *
 * DOES NOT COMPILE.  illustrative only.
 *
 * This is a send-side illustration.  A recv-side illustration
 * should be derivable by the student.  However as a security
 * suggestion, the recv-side buffer should probably best NOT
 * be on the stack as a simple measure against buffer overflow.
 * It could be declared as static storage.
 *
 * static char packet[];   /* recv-side buffer for recvfrom(2) */
 *
 * int
 * recvMsg( ...)
 */

struct udp_fixed_header {
	unsigned long f1;	/* field 1 */
	unsigned long f2;	/* field 2 */
} fh;

/* not really good in terms of reasonable MTU assumptions
 * here but never mind
 */
#define MAXPACKET	1500
#define PACKETBUFFER    (MAXPACKET + sizeof (struct udp_fixed_header))

int
sendUdpMessage(char *data, int datasize)
{
	struct udp_fixed_header *h;
	char packet[PACKETBUFFER];
	int cc = 0;  /* offset into buffer */
	char *destination;

	h = (struct udp_fixed_header *) &packet[0];
	h->f1 = htonl(SOMEF1VALUE);
	cc += sizeof (unsigned long);
	h->f2 = htonl(SOMEF2VALUE);
	cc += sizeof (unsigned long);

	/* form offset for data buffer */
	destination = ((char *) (h)) + cc;

	/* sanity size check.  error if fails
	*/
	if (datasize > (MAXPACKET))
		return(-1);
	memcpy(destination, data, datasize);
	cc += datasize;
	
	sendto ...  destsocket,  destination, cc 

	return(0);
}
