~ubuntu-branches/ubuntu/trusty/dante/trusty-proposed

« back to all changes in this revision

Viewing changes to dlib/protocol.c

  • Committer: Bazaar Package Importer
  • Author(s): Adrian Bridgett
  • Date: 2002-04-07 12:45:55 UTC
  • Revision ID: james.westby@ubuntu.com-20020407124555-qke8rt2tdor0naz2
Tags: upstream-1.1.11.12p1
Import upstream version 1.1.11.12p1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1997, 1998, 1999, 2000, 2001
 
3
 *      Inferno Nettverk A/S, Norway.  All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
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.
 
18
 *
 
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.
 
29
 *
 
30
 * Inferno Nettverk A/S requests users of this software to return to
 
31
 *
 
32
 *  Software Distribution Coordinator  or  sdc@inet.no
 
33
 *  Inferno Nettverk A/S
 
34
 *  Oslo Research Park
 
35
 *  Gaustadall�en 21
 
36
 *  NO-0349 Oslo
 
37
 *  Norway
 
38
 *
 
39
 * any improvements or extensions that they make and grant Inferno Nettverk A/S
 
40
 * the rights to redistribute these changes.
 
41
 *
 
42
 */
 
43
 
 
44
#include "common.h"
 
45
 
 
46
static const char rcsid[] =
 
47
"$Id: protocol.c,v 1.54 2001/12/12 14:42:13 karls Exp $";
 
48
 
 
49
unsigned char *
 
50
sockshost2mem(host, mem, version)
 
51
        const struct sockshost_t *host;
 
52
        unsigned char *mem;
 
53
        int version;
 
54
{
 
55
 
 
56
        switch (version) {
 
57
                case SOCKS_V4:
 
58
                case SOCKS_V4REPLY_VERSION:
 
59
                        SASSERTX(host->atype == SOCKS_ADDR_IPV4);
 
60
 
 
61
                        /* DSTPORT */
 
62
                        memcpy(mem, &host->port, sizeof(host->port));
 
63
                        mem += sizeof(host->port);
 
64
 
 
65
                        /* DSTIP */
 
66
                        memcpy(mem, &host->addr.ipv4, sizeof(host->addr.ipv4));
 
67
                        mem += sizeof(host->addr.ipv4);
 
68
 
 
69
                        break;
 
70
 
 
71
                case SOCKS_V5:
 
72
                        /* ATYP */
 
73
                        memcpy(mem, &host->atype, sizeof(host->atype));
 
74
                        mem += sizeof(host->atype);
 
75
 
 
76
                        switch (host->atype) {
 
77
                                case SOCKS_ADDR_IPV4:
 
78
                                        memcpy(mem, &host->addr.ipv4.s_addr,
 
79
                                        sizeof(host->addr.ipv4.s_addr));
 
80
                                        mem += sizeof(host->addr.ipv4.s_addr);
 
81
                                        break;
 
82
 
 
83
                                case SOCKS_ADDR_IPV6:
 
84
                                        memcpy(mem, &host->addr.ipv6, sizeof(host->addr.ipv6));
 
85
                                        mem += sizeof(host->addr.ipv6);
 
86
                                        break;
 
87
 
 
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);
 
92
                                        mem += *mem + 1;
 
93
                                        break;
 
94
 
 
95
                                default:
 
96
                                        SERRX(host->atype);
 
97
                        }
 
98
 
 
99
                        /* DST.PORT */
 
100
                        memcpy(mem, &host->port, sizeof(host->port));
 
101
                        mem += sizeof(host->port);
 
102
 
 
103
                        break;
 
104
 
 
105
                default:
 
106
                        SERRX(version);
 
107
        }
 
108
 
 
109
        return mem;
 
110
}
 
111
 
 
112
const unsigned char *
 
113
mem2sockshost(host, mem, len, version)
 
114
        struct sockshost_t *host;
 
115
        const unsigned char *mem;
 
116
        size_t len;
 
117
        int version;
 
118
{
 
119
        const char *function = "mem2sockshost()";
 
120
 
 
121
        switch (version) {
 
122
                case SOCKS_V5:
 
123
                        if (len < sizeof(host->atype))
 
124
                                return NULL;
 
125
                        memcpy(&host->atype, mem, sizeof(host->atype));
 
126
                        mem += sizeof(host->atype);
 
127
                        len -= sizeof(host->atype);
 
128
 
 
129
                        switch (host->atype) {
 
130
                                case SOCKS_ADDR_IPV4:
 
131
                                        if (len < sizeof(host->addr.ipv4))
 
132
                                                return NULL;
 
133
                                        memcpy(&host->addr.ipv4, mem, sizeof(host->addr.ipv4));
 
134
                                        mem += sizeof(host->addr.ipv4);
 
135
                                        len -= sizeof(host->addr.ipv4);
 
136
                                        break;
 
137
 
 
138
                                case SOCKS_ADDR_DOMAIN: {
 
139
                                        size_t domainlen = (size_t)*mem;
 
140
 
 
141
                                        mem += sizeof(*mem);
 
142
 
 
143
                                        OCTETIFY(domainlen);
 
144
 
 
145
                                        if (len < domainlen + 1) /* +1 for NUL to be added. */
 
146
                                                return NULL;
 
147
 
 
148
                                        SASSERTX(domainlen < sizeof(host->addr.domain));
 
149
 
 
150
                                        memcpy(host->addr.domain, mem, domainlen);
 
151
                                        host->addr.domain[domainlen] = NUL;
 
152
                                        mem += domainlen;
 
153
                                        len -= domainlen + 1; /* +1 for added NUL. */
 
154
                                        break;
 
155
                                }
 
156
 
 
157
                                case SOCKS_ADDR_IPV6:
 
158
                                        slog(LOG_INFO, "%s: IPv6 not supported", function);
 
159
                                        return NULL;
 
160
 
 
161
                                default:
 
162
                                        slog(LOG_INFO, "%s: unknown atype field: %d",
 
163
                                        function, host->atype);
 
164
                                        return NULL;
 
165
                        }
 
166
 
 
167
                        if (len < sizeof(host->port))
 
168
                                return NULL;
 
169
                        memcpy(&host->port, mem, sizeof(host->port));
 
170
                        mem += sizeof(host->port);
 
171
                        len -= sizeof(host->port);
 
172
 
 
173
                        break;
 
174
 
 
175
                default:
 
176
                        SERRX(version);
 
177
        }
 
178
 
 
179
        return mem;
 
180
}