-
Notifications
You must be signed in to change notification settings - Fork 12
/
direct6.c
113 lines (96 loc) · 3.27 KB
/
direct6.c
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
/*
* Copyright (C) 2002-2006 Josh A. Beam
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* connect directly with ipv6 */
#ifdef IPV6
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "prtunnel.h"
extern char *proxyhost;
extern unsigned short proxyport;
extern int establish_connection6(char *, unsigned short);
/* connect to hostname:port directly */
static int
direct6_connect_to(struct prt_context *context,
char *hostname, unsigned short port,
char *username, char *password, int server_timeout)
{
int fd;
struct hostent *host;
host = gethostbyname2(hostname, AF_INET6);
if(!host)
return -1;
fd = establish_connection6(host->h_addr, port);
if(fd == -1)
return -1;
/* set remote server socket timeout if necessary */
if(server_timeout) {
struct timeval timeout_val;
timeout_val.tv_sec = server_timeout;
timeout_val.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout_val, sizeof(timeout_val));
}
return fd;
}
static void
direct6_disconnect(struct prt_context *context)
{
shutdown(context->localfd, SHUT_RDWR);
shutdown(context->remotefd, SHUT_RDWR);
close(context->localfd);
close(context->remotefd);
}
static int
direct6_local_read(struct prt_context *context, char *buf, int size)
{
return recv(context->localfd, buf, size, 0);
}
static int
direct6_remote_read(struct prt_context *context, char *buf, int size)
{
return recv(context->remotefd, buf, size, 0);
}
static int
direct6_local_send(struct prt_context *context, char *buf, int size)
{
return send(context->localfd, buf, size, 0);
}
static int
direct6_remote_send(struct prt_context *context, char *buf, int size)
{
return send(context->remotefd, buf, size, 0);
}
void
direct6_set_context(struct prt_context *context)
{
context->connect = direct6_connect_to;
context->disconnect = direct6_disconnect;
context->local_read = direct6_local_read;
context->local_send = direct6_local_send;
context->remote_read = direct6_remote_read;
context->remote_send = direct6_remote_send;
context->data = NULL;
}
#endif /* IPV6 */