New revision of the Tunnel6 client with improvements and routed prefix support
[tunnel6.git] / client / src / tunnel.c
blobac9397a6f244069521ef3fbf02a521d5d50fceb1
1 /*
2 * tunnel6
3 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef __WIN32__
21 # include <sys/socket.h>
22 # include <sys/types.h>
23 # include <netinet/in.h>
24 # include <arpa/inet.h>
25 # include <fcntl.h>
26 # include <unistd.h>
27 # include <string.h>
28 # include <stdlib.h>
29 # include <netdb.h>
30 #else
31 # include <windows.h>
32 #endif
33 #include <stdio.h>
34 #include "ipv6.h"
35 #include "proto.h"
36 #include "packet.h"
37 #include "defaults.h"
39 #ifndef __WIN32__
40 static int fd; /* File descriptor of the current session */
41 extern int fd_udp; /* Polling socket */
42 #else
43 static SOCKET fd; /* File descriptor of the current session */
44 extern SOCKET fd_udp; /* Polling socket */
45 #endif
46 static struct sockaddr_in sockname; /* Session information */
47 static char data[DEFAULT_MTU+1]; /* Receiver data buffer */
49 /* receive data from tunnel server */
50 int tunnel_recv ()
52 int ret = recv (fd, data, DEFAULT_MTU, 0);
54 if (ret < 1) {
55 //printf ("ERROR -> recvfrom ()\n");
56 return 0;
59 data[ret] = '\0';
61 return ret;
64 /* check for new data from tunnel server */
65 int tunnel_poll ()
67 int ret = tunnel_recv ();
69 if (!ret)
70 return 0;
72 /* parse received data */
73 return proto_parse (data, ret);
76 /* send data to tunnel server */
77 int tunnel_send (char *packet, unsigned len)
79 if (send (fd, packet, len, 0) == -1) {
80 printf ("ERROR -> send ()\n");
81 return -1;
84 return 0;
87 /* try to connect to tunnel server and log in */
88 int tunnel_init (char *addr, unsigned short port, char *name, char *pwd)
90 struct hostent *host;
91 #ifdef __WIN32__
92 /* initialize WinSock
93 version of WinSock API (should be supported from W. XP) */
94 WORD wVersionRequested = MAKEWORD (2, 2);
95 WSADATA wsadata;
97 if (WSAStartup (wVersionRequested, &wsadata)) {
98 return -1;
100 #endif
101 /* create UDP socket */
102 if ((fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
103 printf ("ERROR -> socket ()\n");
104 goto err;
107 /* check IPv4 from given addr */
108 if (!(host = gethostbyname (addr))) {
109 printf ("ERROR -> Address '%s' does'nt exist\n", addr);
110 goto err;
113 /* setup connection */
114 sockname.sin_family = AF_INET;
115 sockname.sin_port = htons (port);
116 memcpy (&sockname.sin_addr, host->h_addr, host->h_length);
118 /* connect to tunnel server */
119 if (connect (fd, (struct sockaddr *) &sockname, sizeof (sockname)) == -1) {
120 printf ("ERROR -> Unable to connect to '%s'\n", addr);
122 goto err;
125 /* send login request to server */
126 if (proto_login (data, name, pwd) == -1)
127 goto err;
129 /* save polling socket */
130 fd_udp = fd;
132 return 0;
134 err: /* error */
135 #ifdef __WIN32__
136 WSACleanup();
137 #endif
139 return -1;