source: trunk/CrypPlugins/KeySearcher/ExternalClient/Main.cpp

Last change on this file was 8983, checked in by kopal, 4 months ago

Complete CrypTool 2 project

  • renamed "Cryptool" namespace to "CrypTool" namespace
File size: 2.1 KB
Line 
1#include <ctype.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <netdb.h>
8#include <string.h>
9#include <netinet/in.h>
10#include <arpa/inet.h>
11#include <signal.h>
12
13#include "Network.h"
14
15#define RECONNECT_TIME  5
16
17void usage(char **argv)
18{
19    printf("Usage: %s $HOST $PORT [$PASSWORD]\n\n", argv[0]);
20    printf("    HOST       - PC running cryptool\n");
21    printf("    PORT       - CrypTool's listening port\n");
22    printf("    [PASSWORD] - Password used to auth with server\n");
23}
24
25bool lookup(const char* host, sockaddr_in & serv_addr)
26{
27    hostent *server = gethostbyname(host);
28
29    if(!server)
30        return false;
31
32    serv_addr.sin_family = AF_INET;
33    bcopy((char *)server->h_addr, 
34            (char *)&serv_addr.sin_addr.s_addr,
35            server->h_length);
36
37    return true;
38}
39
40bool receivedSigInt = false;
41
42void terminate(int parm)
43{
44    if(receivedSigInt)
45    {
46        printf("Force quitting...\n");
47        exit(1);
48    }
49    printf("Received SIGINT, will exit after current chunk\n");
50    printf("Press again to force quit.\n");
51    receivedSigInt = true;
52}
53
54int main (int argc, char **argv)
55{
56    if(argc != 3 && argc != 4)
57    {
58        usage(argv);
59        return 1;
60    }
61
62    int pwlen = (argc==4?strlen(argv[3]):0) +1;
63    char password[pwlen];
64    bzero(password, pwlen);
65    if(argc == 4)
66    {
67        strcpy(password, argv[3]);
68        bzero(argv[3], pwlen);
69    }
70
71    int port = atoi(argv[2]);
72
73    if(port == 0 || port <= 0 || port > 0xFFFF)
74    {
75        printf("Invalid port.\n");
76        return 1;
77    }
78
79    // CTRL+C
80    signal (SIGINT, terminate);
81
82    sockaddr_in serv_addr;
83    bzero((char *) &serv_addr, sizeof(serv_addr));
84    serv_addr.sin_port = htons(port);
85
86    CrypTool cryptool;
87
88    while(!receivedSigInt)
89    {
90        if(!lookup(argv[1], serv_addr))
91        {
92            printf("failed to lookup %s..\n", argv[1]);
93            continue;
94        }
95        networkThread(serv_addr, port, password, &cryptool, &receivedSigInt);
96        printf("Reconnecting in %u seconds\n", RECONNECT_TIME);
97        sleep(RECONNECT_TIME);
98    }
99
100}
Note: See TracBrowser for help on using the repository browser.