summaryrefslogtreecommitdiff
path: root/src/serv.c
blob: 521c4dd18dbca7472e9ba0cf5a23926c9719f5f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *      Copyright (C) 2000 Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * GNUTLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUTLS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include "../lib/gnutls.h"
#include <port.h>

#define SA struct sockaddr
#define ERR(err,s) if(err==-1) {perror(s);return(1);}
#define MAX_BUF 100

int main()
{
    int err, listen_sd;
    int sd, ret;
    struct sockaddr_in sa_serv;
    struct sockaddr_in sa_cli;
    int client_len;
    char topbuf[512];
    GNUTLS_STATE state;
    char buffer[MAX_BUF+1];
    int optval = 1;

    listen_sd = socket(AF_INET, SOCK_STREAM, 0);
    ERR(listen_sd, "socket");

    memset(&sa_serv, '\0', sizeof(sa_serv));
    sa_serv.sin_family = AF_INET;
    sa_serv.sin_addr.s_addr = INADDR_ANY;
    sa_serv.sin_port = htons(PORT);	/* Server Port number */

    setsockopt( listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
    err = bind(listen_sd, (SA *) & sa_serv, sizeof(sa_serv));
    ERR(err, "bind");
    err = listen(listen_sd, 1024);
    ERR(err, "listen");



    client_len = sizeof(sa_cli);
    for (;;) {
	gnutls_init(&state, GNUTLS_SERVER);
	gnutls_set_db_name(state, "/tmp/gdb");
	gnutls_set_cipher_priority( state, 4, GNUTLS_TWOFISH, GNUTLS_RIJNDAEL, GNUTLS_3DES, GNUTLS_ARCFOUR);
	gnutls_set_compression_priority( state, 2, GNUTLS_ZLIB, GNUTLS_NULL_COMPRESSION);
	gnutls_set_kx_priority( state, 1, GNUTLS_KX_ANON_DH);
	gnutls_set_mac_priority( state, 2, GNUTLS_MAC_SHA, GNUTLS_MAC_MD5);
	sd = accept(listen_sd, (SA *) & sa_cli, &client_len);


	fprintf(stderr, "connection from %s, port %d\n",
		inet_ntop(AF_INET, &sa_cli.sin_addr, topbuf,
			  sizeof(topbuf)), ntohs(sa_cli.sin_port));



	ret = gnutls_handshake(sd, state);
	if (ret < 0) {
	    close(sd);
	    gnutls_deinit(&state);
	    fprintf(stderr, "Handshake has failed (%d)\n", ret);
	    gnutls_perror(ret);
	    continue;
	}
	fprintf(stderr, "Handshake was completed\n");
	fprintf(stderr, "Acting as echo server...\n");
/*	ret =
	    gnutls_write(sd, state, "hello client",
			sizeof("hello client"));
	if (ret < 0) {
	    close(sd);
	    gnutls_deinit(&state);
	    gnutls_perror(ret);
	    continue;
	}
*/
	for (;;) {
	    bzero( buffer, MAX_BUF+1);
	    ret = gnutls_read(sd, state, buffer, MAX_BUF);
	    if (gnutls_is_fatal_error(ret) == 1) {
		if (ret == GNUTLS_E_CLOSURE_ALERT_RECEIVED) {
		    fprintf(stderr,
			    "\nPeer has closed the GNUTLS connection\n");
		    break;
		} else {
		    fprintf(stderr, "\nReceived corrupted data(%d). Closing the connection.\n", ret);
		    break;
		}

	    }
	    gnutls_write(sd, state, buffer, strlen(buffer));
	}
	fprintf(stderr, "\n");
	gnutls_close(sd, state);
	close(sd);
	gnutls_deinit(&state);
    }
    close(listen_sd);
    return 0;

}