Back to the Résumé

/* test.c: Jay Miller, 11-2001

 * A simple program to demonstrate the basic use of the p2p library by

 * sending (encrypted) strings between two hosts running this program.

 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include "p2p_public.h"

/* Demonstrates the use of the p2p datagram-receiving callback.  As per

 * the definition in p2p_public.h..

 *    dg     : the incoming datagram.

 *    dg_len : the size in bytes of argument 1.

 *    sa     : a sockaddr structure describing the sender.

 *    sa_len : the size in bytes of argument 3.

 *

 * Always returns 0.

 */
int p2p_recv_handler(void *dg, size_t dg_len, struct sockaddr_in *sa,
      socklen_t sa_len)
{
   char addr[24];
   struct hostent *hptr;

   inet_ntop(sa->sin_family, &sa->sin_addr, addr, 23);
   hptr = gethostbyaddr((char *)&sa->sin_addr, 4, sa->sin_family);

   printf("\n\n*** Received %d bytes from %s (%s), port %d:\n%s\n\n",
         dg_len, hptr->h_name, addr, sa->sin_port, (char *)dg);
   return 0;
}

/* A program utilizing the p2p library to send simple text strings

 * between hosts.

 */
int main(void)
{
   char dest[80], s[80];
   crypt_t key = 0xd34db33f;

   /* Initialize the p2p library with a key to enable encryption. */
   if (p2p_init(key) == -1)
      return -1;
   
   while (1) {
      /* gets(): for demonstration purposes only! */
      printf("Destination (or 'quit'): "); gets(dest);
      if (!strcmp(dest, "quit"))
         break;
      printf("Message: "); gets(s);

      /* simply sends the string s to host dest */
      if (p2p_send(dest, s, strlen(s)) == -1)
         break;
   }

   /* Shutdown the p2p library, releasing the thread. */
   p2p_stop();

   return 0;
}


Back to the Résumé