4 Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: tnl.c 1379 2004-03-27 11:59:31Z guus $
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
28 #include "logger/logger.h"
29 #include "support/avl.h"
30 #include "support/sockaddr.h"
31 #include "support/xalloc.h"
34 static const int addressfamily = AF_UNSPEC;
35 static const int socktype = SOCK_STREAM;
36 static const int protocol = IPPROTO_TCP;
38 bool server_recv_meta(struct tnl *tnl, const void *buf, int len) {
42 for(int i = 0; i < len; i++) {
44 out[i] = tolower(in[i]);
45 else if(islower(in[i]))
46 out[i] = toupper(in[i]);
51 tnl->send_meta(tnl, out, len);
56 bool server_accept(struct tnl *tnl) {
57 logger(LOG_INFO, _("Got connection from %s"), tnl->remote.id);
58 tnl->recv_meta = server_recv_meta;
62 void server(char *port) {
63 struct addrinfo *ai, hint = {0};
65 tnl_listen_t *listen = clear(new(listen));
67 hint.ai_family = addressfamily;
68 hint.ai_socktype = socktype;
69 hint.ai_protocol = protocol;
70 hint.ai_flags = AI_PASSIVE;
72 err = getaddrinfo(NULL, port, &hint, &ai);
75 logger(LOG_WARNING, _("Error looking up port %s: %s"), port, gai_strerror(err));
79 if(sizeof listen->local.address < ai->ai_addrlen) {
80 logger(LOG_ERR, "%d < %d!", sizeof listen->local.address, ai->ai_addrlen);
84 memcpy(&listen->local.address, ai->ai_addr, ai->ai_addrlen);
85 listen->local.id = xstrdup("CommonA");
86 listen->type = socktype;
87 listen->protocol = protocol;
88 listen->accept = server_accept;
90 logger(LOG_DEBUG, "Nu ga ik iets doen hoor");
91 if(!tnl_ep_set_x509_credentials(&listen->local, "server_key", "server_cert", "trust", NULL)) {
92 logger(LOG_ERR, "Couldn't set X.509 credentials!");
96 if(!tnl_listen(listen)) {
97 logger(LOG_ERR, _("Could not listen!"));
102 bool client_stdin_handler(fd_t *fd) {
103 tnl_t *tnl = fd->data;
107 len = read(fd->fd, buf, sizeof buf);
110 gnutls_bye(tnl->session, GNUTLS_SHUT_WR);
115 tnl->send_meta(tnl, buf, len);
120 bool client_recv_meta(struct tnl *tnl, const void *buf, int len) {
125 bool client_error(tnl_t *tnl, int err) {
129 bool client_accept(tnl_t *tnl) {
132 logger(LOG_INFO, _("Connected to %s"), tnl->remote.id);
133 tnl->recv_meta = client_recv_meta;
137 fd->read = client_stdin_handler;
144 void client(char *host, char *port) {
145 struct addrinfo *ai, hint = {0};
149 hint.ai_family = addressfamily;
150 hint.ai_socktype = socktype;
152 err = getaddrinfo(host, port, &hint, &ai);
155 logger(LOG_WARNING, _("Error looking up %s port %s: %s"), host, port, gai_strerror(err));
160 memcpy(&tnl->remote.address, ai->ai_addr, ai->ai_addrlen);
161 tnl->local.id = xstrdup("CommonB");
162 tnl->remote.id = xstrdup("CommonA");
163 tnl->type = socktype;
164 tnl->protocol = protocol;
165 tnl->accept = client_accept;
166 tnl->error = client_error;
168 if(!tnl_ep_set_x509_credentials(&tnl->local, "client_key", "client_cert", "trust", NULL)) {
169 logger(LOG_ERR, "Couldn't set credentials!");
173 if(!tnl_connect(tnl)) {
174 logger(LOG_ERR, _("Could not connect to server!"));
179 int main(int argc, char **argv) {
180 gnutls_global_init();
181 gnutls_global_init_extra();
184 logger_init(argv[0], LOGGER_MODE_NULL);
187 client(argv[1], argv[2]);
191 logger(LOG_ERR, "Usage: %s [host] port\n", argv[0]);