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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <canl.h>
#include <canl_ssl.h>
#define BUF_LEN 1000
#define BACKLOG 10
#define DEF_PORT 4321
#define DEF_TIMEOUT 150
int main(int argc, char *argv[])
{
canl_ctx my_ctx;
canl_io_handler my_io_h = NULL;
int err = 0;
int opt, port = DEF_PORT;
char *serv_cert = NULL;
char *serv_key = NULL;
char *ca_dir = NULL;
char buf[BUF_LEN];
int buf_len = 0;
struct timeval timeout;
canl_principal princ = NULL;
int get_peer_princ = 0;
int ocsp_on = 0;
char *name = NULL;
timeout.tv_sec = DEF_TIMEOUT;
timeout.tv_usec = 0;
while ((opt = getopt(argc, argv, "nhop:c:k:d:t:")) != -1) {
switch (opt) {
case 'h':
fprintf(stderr, "Usage: %s [-p port] [-c certificate]"
" [-k private key] [-d ca_dir] [-h] "
"[-t timeout] [-n {print peer's princ name}] "
" [-o {turn OCSP on}] "
" \n", argv[0]);
exit(0);
case 'p':
port = atoi(optarg);
break;
case 'c':
serv_cert = optarg;
break;
case 'k':
serv_key = optarg;
break;
case 'd':
ca_dir = optarg;
break;
case 't':
timeout.tv_sec = atoi(optarg);
break;
case 'n':
get_peer_princ = 1;
break;
case 'o':
ocsp_on = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-p port] [-c certificate]"
" [-k private key] [-d ca_dir] [-h] "
"[-t timeout] [-n {print peer's princ name}] "
" [-o {turn OCSP on}] "
" \n", argv[0]);
exit(-1);
}
}
my_ctx = canl_create_ctx();
if (!my_ctx){
printf("[SERVER] canl context cannot be created\n");
return -1;
}
err = canl_create_io_handler(my_ctx, &my_io_h);
if (err) {
printf("[SERVER] io handler cannot be created:\n[CANL] %s\n",
canl_get_error_message(my_ctx));
goto end;
}
if (serv_cert || serv_key){
err = canl_ctx_set_ssl_cred(my_ctx, serv_cert, serv_key, NULL,
NULL, NULL);
if (err) {
printf("[SERVER] cannot set certificate or key to"
" context:\n[CANL] %s\n",
canl_get_error_message(my_ctx));
goto end;
}
}
/* ACCEPT from canl_io_accept*/
int sockfd = 0, new_fd = 0;
char str_port[8];
struct addrinfo hints, *servinfo, *p;
struct sockaddr s_addr;
socklen_t sin_size;
int yes=1;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if (snprintf(str_port, 8, "%d", port) < 0) {
printf ("[SERVER] Wrong port request");
return 1;
}
/* XXX timeouts - use c-ares, too */
if ((err = getaddrinfo(NULL, str_port, &hints, &servinfo)) != 0) {
printf("[SERVER] getaddrinfo: %s\n", gai_strerror(err));
return 1;
}
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
err = errno;
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
err = errno;
continue;
}
if ((err = bind(sockfd, p->ai_addr, p->ai_addrlen))) {
err = errno;
close(sockfd);
continue;
}
if ((err = listen(sockfd, BACKLOG))) {
close(sockfd);
err = errno;
continue;
}
break;
}
freeaddrinfo(servinfo); // all done with this structure
if (p == NULL) {
/* Beware that only the last error is displayed here ... */
printf("Failed to acquire a server socket: %s\n",
strerror(err));
return 1;
}
printf("server: waiting for connections...\n");
sin_size = sizeof(s_addr);
if (ocsp_on)
canl_ctx_set_ssl_flags(my_ctx, CANL_SSL_OCSP_VERIFY_ALL);
new_fd = accept(sockfd, &s_addr, &sin_size);
if (new_fd == -1){
printf("Failed to accept network connection: %s", strerror(errno));
}
/* canl_create_io_handler has to be called for my_io_h*/
/* TODO timeout in this function? and select around it*/
if (get_peer_princ) {
err = canl_io_accept(my_ctx, my_io_h, new_fd, s_addr,
0, &princ, &timeout);
if (err) {
printf("[SERVER] connection cannot be established:\n[CANL] %s\n",
canl_get_error_message(my_ctx));
goto end;
}
err = canl_princ_name(my_ctx, princ, &name);
printf("[SERVER] connection established with %s\n", name);
free(name);
canl_princ_free(my_ctx, princ);
}
else{
err = canl_io_accept(my_ctx, my_io_h, new_fd, s_addr,
0, NULL, &timeout);
if (err) {
printf("[SERVER] connection cannot be established:\n[CANL] %s\n",
canl_get_error_message(my_ctx));
goto end;
}
printf("[SERVER] connection established\n");
}
strncpy(buf, "This is a testing message to send", sizeof(buf));
buf_len = strlen(buf) + 1;
printf("[SERVER] Trying to send sth to the client\n");
err = canl_io_write (my_ctx, my_io_h, buf, buf_len, &timeout);
if (err <= 0) {
printf("[SERVER] cannot send message to the client:\n[CANL] %s\n",
canl_get_error_message(my_ctx));
goto end;
}
else {
buf[err] = '\0';
printf("[SERVER] message \"%s\" sent successfully\n", buf);
}
buf[0] = '\0';
err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, &timeout);
if (err <= 0) {
printf("[SERVER] Failed to receive reply from client:\n[CANL] %s\n",
canl_get_error_message(my_ctx));
goto end;
}
buf[err] = '\0';
printf ("[SERVER] received: %s\n", buf);
err = 0;
end:
if (my_io_h)
canl_io_destroy(my_ctx, my_io_h);
canl_free_ctx(my_ctx);
return err;
}
|