Exemplo n.º 1
0
int		main()
{
  pid_t			server_pid;
  struct sigaction	action;

  action.sa_sigaction = receiver;
  action.sa_flags = SA_SIGINFO | SA_ONSTACK;
  g_clients = NULL;
  if (sigaction(SIGUSR1, &action, NULL) == 0 &&
      sigaction(SIGUSR2, &action, NULL) == 0 &&
      signal(SIGINT, exit_signal) == 0)
    {
      server_pid = getpid();
      my_putstr("Server started: ");
      my_put_nbr((int)server_pid);
      my_putstr("\n");
      listen_server();
      return (0);
    }
  else
    my_puterror("Server start failed.\n");
  return (1);
}
Exemplo n.º 2
0
// usage: smp <IP address to connect to>
int main( int argc, char** argv )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        printf( "usage:\n\tsmp <ipaddress>\n\tsmp server\n" );
        return EXIT_FAILURE;
    }
    
    int bServerMode = strstr( "server", argv[ 1 ] ) != 0;

    setup();
    
    unsigned char holder[ BUFFER_SIZE ];
    memset( holder, 0x00, BUFFER_SIZE );
    
    if ( !bServerMode )
    {
        // we are talking to the server at ip address argv[ 1 ]
        char input_string[ 256 ];
        printf( "Enter a shared secret: " );
        readLine( input_string, 256 );
        // TESTCODE: strcpy( input_string, "testme" );
        secret = binEncode( input_string, strlen( input_string ) );
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 1 and send to other side */
        /*****************************************************/
        /*****************************************************/
        int len = step1( holder, BUFFER_SIZE );
        
        int serverfd = connect_to_server( argv[ 1 ] );
        if ( serverfd == 1 )
            return EXIT_FAILURE;
        
        write_to_server( serverfd, holder, len );
        // dumpBuff( holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Get results from other side. */
        /*  Other side performed Step 2. */
        /*****************************************************/
        /*****************************************************/
        memset( holder, 0x00, BUFFER_SIZE );
        len = revc_from_server( serverfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 3 and send to the other side */
        /*****************************************************/
        /*****************************************************/
        step3( holder, BUFFER_SIZE );
        write_to_server( serverfd, holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Get bytes from other side and do Step 5 */
        /*****************************************************/
        /*****************************************************/
        memset( holder, 0x00, BUFFER_SIZE );
        len = revc_from_server( serverfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, len );
        
        step5( holder, len );
        
        disconnect_from_server( serverfd );
    }
    else    // we are in server mode, other side will send us data first
    {
        int listenfd = listen_server();
        /*if ( listenfd == 1 )
            return EXIT_FAILURE;
        TODO: error checking
        */
        
        char input_string[ 256 ];
        printf( "Enter a shared secret: " );
        readLine( input_string, 256 );
        // TESTCODE: strcpy( input_string, "testme" );
        secret = binEncode( input_string, strlen( input_string ) );
        
        int len = revc_from_server( listenfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, BUFFER_SIZE);
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 2 and send to other side */
        /*****************************************************/
        /*****************************************************/
        len = step2( holder, BUFFER_SIZE  );
        write_to_server( listenfd, holder, len );

        len = revc_from_server( listenfd, holder, BUFFER_SIZE );
        // dumpBuff( holder, len );
        
        /*****************************************************/
        /*****************************************************/
        /*  Do Step 4 and send to other side */
        /*****************************************************/
        /*****************************************************/
        len = step4( holder, BUFFER_SIZE );
        write_to_server( listenfd, holder, len );
        
        disconnect_from_server( listenfd );
    }
    
    
    if ( match == 1 )
        printf( "Secrets match\n" );
    else
        printf( "Secrets do not match\n");
    
    cleanup();
    return EXIT_SUCCESS;
}