1 | |
2 | |
3 | #ifdef HAVE_CONFIG_H1 |
4 | #include <config.h> |
5 | #endif |
6 | |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <errno(*__errno_location ()).h> |
10 | #include <sys/types.h> |
11 | #include <sys/socket.h> |
12 | #include <arpa/inet.h> |
13 | #include <netinet/in.h> |
14 | #include <string.h> |
15 | #include <unistd.h> |
16 | #include <gnutls/gnutls.h> |
17 | #include <gnutls/openpgp.h> |
18 | |
19 | #define KEYFILE"secret.asc" "secret.asc" |
20 | #define CERTFILE"public.asc" "public.asc" |
21 | #define RINGFILE"ring.gpg" "ring.gpg" |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | #define SAstruct sockaddr struct sockaddr |
28 | #define SOCKET_ERR(err,s)if(err==-1) {perror(s);return(1);} if(err==-1) {perror(s);return(1);} |
29 | #define MAX_BUF1024 1024 |
30 | #define PORT5556 5556 /* listen to 5556 port */ |
31 | #define DH_BITS1024 1024 |
32 | |
33 | |
34 | gnutls_certificate_credentials_t cred; |
35 | gnutls_dh_params_t dh_params; |
36 | |
37 | static int |
38 | generate_dh_params (void) |
39 | { |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | gnutls_dh_params_init (&dh_params); |
47 | gnutls_dh_params_generate2 (dh_params, DH_BITS1024); |
48 | |
49 | return 0; |
50 | } |
51 | |
52 | static gnutls_session_t |
53 | initialize_tls_session (void) |
54 | { |
55 | gnutls_session_t session; |
56 | |
57 | gnutls_init (&session, GNUTLS_SERVER1); |
58 | |
59 | gnutls_priority_set_direct (session, "NORMAL:+CTYPE-OPENPGP", NULL((void*)0)); |
60 | |
61 | |
62 | |
63 | gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST); |
64 | |
65 | gnutls_dh_set_prime_bits (session, DH_BITS1024); |
66 | |
67 | return session; |
68 | } |
69 | |
70 | int |
71 | main (void) |
72 | { |
73 | int err, listen_sd; |
74 | int sd, ret; |
75 | struct sockaddr_in sa_serv; |
76 | struct sockaddr_in sa_cli; |
77 | int client_len; |
78 | char topbuf[512]; |
79 | gnutls_session_t session; |
80 | char buffer[MAX_BUF1024 + 1]; |
81 | int optval = 1; |
82 | char name[256]; |
83 | |
84 | strcpy (name, "Echo Server"); |
85 | |
86 | |
87 | |
88 | gnutls_global_init (); |
89 | |
90 | gnutls_certificate_allocate_credentials (&cred); |
91 | gnutls_certificate_set_openpgp_keyring_file (cred, RINGFILE"ring.gpg", |
92 | GNUTLS_OPENPGP_FMT_BASE64); |
93 | |
94 | gnutls_certificate_set_openpgp_key_file (cred, CERTFILE"public.asc", KEYFILE"secret.asc", |
95 | GNUTLS_OPENPGP_FMT_BASE64); |
96 | |
97 | generate_dh_params (); |
98 | |
99 | gnutls_certificate_set_dh_params (cred, dh_params); |
100 | |
101 | |
102 | |
103 | listen_sd = socket (AF_INET2, SOCK_STREAMSOCK_STREAM, 0); |
104 | SOCKET_ERR (listen_sd, "socket")if(listen_sd==-1) {perror("socket");return(1);}; |
105 | |
106 | memset (&sa_serv, '\0', sizeof (sa_serv)); |
107 | sa_serv.sin_family = AF_INET2; |
108 | sa_serv.sin_addr.s_addr = INADDR_ANY((in_addr_t) 0x00000000); |
109 | sa_serv.sin_port = htons (PORT5556); |
110 | |
111 | setsockopt (listen_sd, SOL_SOCKET1, SO_REUSEADDR2, (void *) &optval, |
112 | sizeof (int)); |
113 | |
114 | err = bind (listen_sd, (SAstruct sockaddr *) & sa_serv, sizeof (sa_serv)); |
115 | SOCKET_ERR (err, "bind")if(err==-1) {perror("bind");return(1);}; |
116 | err = listen (listen_sd, 1024); |
117 | SOCKET_ERR (err, "listen")if(err==-1) {perror("listen");return(1);}; |
118 | |
119 | printf ("%s ready. Listening to port '%d'.\n\n", name, PORT5556); |
120 | |
121 | client_len = sizeof (sa_cli); |
122 | for (;;) |
| 1 | Loop condition is true. Entering loop body |
|
123 | { |
124 | session = initialize_tls_session (); |
125 | |
126 | sd = accept (listen_sd, (SAstruct sockaddr *) & sa_cli, &client_len); |
127 | |
128 | printf ("- connection from %s, port %d\n", |
129 | inet_ntop (AF_INET2, &sa_cli.sin_addr, topbuf, |
130 | sizeof (topbuf)), ntohs (sa_cli.sin_port)); |
| 2 | Pass-by-value argument in function call is undefined |
|
131 | |
132 | gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd); |
133 | ret = gnutls_handshake (session); |
134 | if (ret < 0) |
135 | { |
136 | close (sd); |
137 | gnutls_deinit (session); |
138 | fprintf (stderrstderr, "*** Handshake has failed (%s)\n\n", |
139 | gnutls_strerror (ret)); |
140 | continue; |
141 | } |
142 | printf ("- Handshake was completed\n"); |
143 | |
144 | |
145 | |
146 | |
147 | for (;;) |
148 | { |
149 | memset (buffer, 0, MAX_BUF1024 + 1); |
150 | ret = gnutls_record_recv (session, buffer, MAX_BUF1024); |
151 | |
152 | if (ret == 0) |
153 | { |
154 | printf ("\n- Peer has closed the GnuTLS connection\n"); |
155 | break; |
156 | } |
157 | else if (ret < 0) |
158 | { |
159 | fprintf (stderrstderr, "\n*** Received corrupted " |
160 | "data(%d). Closing the connection.\n\n", ret); |
161 | break; |
162 | } |
163 | else if (ret > 0) |
164 | { |
165 | |
166 | |
167 | gnutls_record_send (session, buffer, strlen (buffer)); |
168 | } |
169 | } |
170 | printf ("\n"); |
171 | |
172 | |
173 | gnutls_bye (session, GNUTLS_SHUT_WR); |
174 | |
175 | close (sd); |
176 | gnutls_deinit (session); |
177 | |
178 | } |
179 | close (listen_sd); |
180 | |
181 | gnutls_certificate_free_credentials (cred); |
182 | |
183 | gnutls_global_deinit (); |
184 | |
185 | return 0; |
186 | |
187 | } |