2
* Copyright (c) 1997, 1998, 1999, 2000, 2001
3
* Inferno Nettverk A/S, Norway. All rights reserved.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. The above copyright notice, this list of conditions and the following
9
* disclaimer must appear in all copies of the software, derivative works
10
* or modified versions, and any portions thereof, aswell as in all
11
* supporting documentation.
12
* 2. All advertising materials mentioning features or use of this software
13
* must display the following acknowledgement:
14
* This product includes software developed by
15
* Inferno Nettverk A/S, Norway.
16
* 3. The name of the author may not be used to endorse or promote products
17
* derived from this software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
* Inferno Nettverk A/S requests users of this software to return to
32
* Software Distribution Coordinator or sdc@inet.no
33
* Inferno Nettverk A/S
39
* any improvements or extensions that they make and grant Inferno Nettverk A/S
40
* the rights to redistribute these changes.
46
static const char rcsid[] =
47
"$Id: protocol.c,v 1.54 2001/12/12 14:42:13 karls Exp $";
50
sockshost2mem(host, mem, version)
51
const struct sockshost_t *host;
58
case SOCKS_V4REPLY_VERSION:
59
SASSERTX(host->atype == SOCKS_ADDR_IPV4);
62
memcpy(mem, &host->port, sizeof(host->port));
63
mem += sizeof(host->port);
66
memcpy(mem, &host->addr.ipv4, sizeof(host->addr.ipv4));
67
mem += sizeof(host->addr.ipv4);
73
memcpy(mem, &host->atype, sizeof(host->atype));
74
mem += sizeof(host->atype);
76
switch (host->atype) {
78
memcpy(mem, &host->addr.ipv4.s_addr,
79
sizeof(host->addr.ipv4.s_addr));
80
mem += sizeof(host->addr.ipv4.s_addr);
84
memcpy(mem, &host->addr.ipv6, sizeof(host->addr.ipv6));
85
mem += sizeof(host->addr.ipv6);
88
case SOCKS_ADDR_DOMAIN:
89
/* first byte gives length of rest. */
90
*mem = (unsigned char)strlen(host->addr.domain);
91
memcpy(mem + 1, host->addr.domain, (size_t)*mem);
100
memcpy(mem, &host->port, sizeof(host->port));
101
mem += sizeof(host->port);
112
const unsigned char *
113
mem2sockshost(host, mem, len, version)
114
struct sockshost_t *host;
115
const unsigned char *mem;
119
const char *function = "mem2sockshost()";
123
if (len < sizeof(host->atype))
125
memcpy(&host->atype, mem, sizeof(host->atype));
126
mem += sizeof(host->atype);
127
len -= sizeof(host->atype);
129
switch (host->atype) {
130
case SOCKS_ADDR_IPV4:
131
if (len < sizeof(host->addr.ipv4))
133
memcpy(&host->addr.ipv4, mem, sizeof(host->addr.ipv4));
134
mem += sizeof(host->addr.ipv4);
135
len -= sizeof(host->addr.ipv4);
138
case SOCKS_ADDR_DOMAIN: {
139
size_t domainlen = (size_t)*mem;
145
if (len < domainlen + 1) /* +1 for NUL to be added. */
148
SASSERTX(domainlen < sizeof(host->addr.domain));
150
memcpy(host->addr.domain, mem, domainlen);
151
host->addr.domain[domainlen] = NUL;
153
len -= domainlen + 1; /* +1 for added NUL. */
157
case SOCKS_ADDR_IPV6:
158
slog(LOG_INFO, "%s: IPv6 not supported", function);
162
slog(LOG_INFO, "%s: unknown atype field: %d",
163
function, host->atype);
167
if (len < sizeof(host->port))
169
memcpy(&host->port, mem, sizeof(host->port));
170
mem += sizeof(host->port);
171
len -= sizeof(host->port);