/**
 * Group: Edison Chindrawaly
 *        Sehoon Jung
 *        Sangtae Park
 * Project : #1
 */

#include "header.h"

int main(int argc, char **argv)
{
   int            sock_s1_fd;  // socket of Crpyto erver
   int            sock_s2_fd;  // sokcet of Mary
   char buffer[MAXSIZE+1];

   struct sockaddr_in   servaddr;   // address of crypto server
   struct sockaddr_in   marry_addr; // address of mary

   MESSAGE recv_msg, send_msg; // MESSAGE Structure

   send_msg.key='X';    // encryption key

   /* create sockets */
   sock_s1_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   sock_s2_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

   /* specify address of encryption server */
   bzero(&servaddr, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(SERV_PORT);
   servaddr.sin_addr.s_addr = inet_addr(argv[1]);

   /* specify address of encryption server */
   bzero(&marry_addr, sizeof(marry_addr));
   marry_addr.sin_family = AF_INET;
   marry_addr.sin_port = htons(9999);
   marry_addr.sin_addr.s_addr = inet_addr(argv[2]);

   /* connect to server */
   connect(sock_s1_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
   if(connect(sock_s2_fd, (struct sockaddr
*)&marry_addr,sizeof(marry_addr))<0)
   perror("fail to connect mary");

   for ( ; ; ) {

      // Get input from Pete
      printf("Pete: ");        
      gets(buffer);

      // Send input from pete to Crypto Server to encrpyt
      strcpy(send_msg.msg, buffer);
      send_msg.option=1;
      send(sock_s1_fd,(char*)(&send_msg),sizeof(MESSAGE),0);
     
      // Receive encrypted message from Crypto Server
      recv(sock_s1_fd,(char*)(&recv_msg),sizeof(MESSAGE),0);
      printf("From Server: %s\n",recv_msg.msg);
      // Send encrpyted message to Mary
      send(sock_s2_fd,(char*)(&recv_msg),sizeof(MESSAGE),0);

      /* End condition */
      if (strcasecmp(buffer,"END")==0) {
      close (sock_s1_fd);
      close (sock_s2_fd);
      exit(0);
      }


      // Recieve encrpyted message from Mary
      recv(sock_s2_fd,(char*)(&recv_msg),sizeof(MESSAGE),0);
      printf("From Mary: %s\n",recv_msg.msg);

      // Send message to crypto server to decrypt
      recv_msg.option=2;
      send(sock_s1_fd,(char*)(&recv_msg),sizeof(MESSAGE),0);

      // Recieve decrypted message from Crypto Server
      recv(sock_s1_fd,(char*)(&recv_msg),sizeof(MESSAGE),0);
      printf("From Server: %s\n",recv_msg.msg);
      if(strcasecmp(recv_msg.msg,"END") == 0)
      {
        close(sock_s1_fd);
        close(sock_s2_fd);
        exit(0);
      }
   }
}
 

1