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/>.
21 # include <sys/socket.h>
22 # include <sys/types.h>
23 # include <netinet/in.h>
24 # include <arpa/inet.h>
40 static int fd
; /* File descriptor of the current session */
41 extern int fd_udp
; /* Polling socket */
43 static SOCKET fd
; /* File descriptor of the current session */
44 extern SOCKET fd_udp
; /* Polling socket */
46 static struct sockaddr_in sockname
; /* Session information */
47 static char data
[DEFAULT_MTU
+1]; /* Receiver data buffer */
49 /* receive data from tunnel server */
52 int ret
= recv (fd
, data
, DEFAULT_MTU
, 0);
55 //printf ("ERROR -> recvfrom ()\n");
64 /* check for new data from tunnel server */
67 int ret
= tunnel_recv ();
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");
87 /* try to connect to tunnel server and log in */
88 int tunnel_init (char *addr
, unsigned short port
, char *name
, char *pwd
)
93 version of WinSock API (should be supported from W. XP) */
94 WORD wVersionRequested
= MAKEWORD (2, 2);
97 if (WSAStartup (wVersionRequested
, &wsadata
)) {
101 /* create UDP socket */
102 if ((fd
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) == -1) {
103 printf ("ERROR -> socket ()\n");
107 /* check IPv4 from given addr */
108 if (!(host
= gethostbyname (addr
))) {
109 printf ("ERROR -> Address '%s' does'nt exist\n", addr
);
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
);
125 /* send login request to server */
126 if (proto_login (data
, name
, pwd
) == -1)
129 /* save polling socket */