1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2008, 2009, 2011, 2012
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: udp_util.c,v 1.68 2013/01/02 13:22:39 karls Exp $";
48 
49 void *
udpheader_add(host,msg,len,msgsize)50 udpheader_add(host, msg, len, msgsize)
51    const sockshost_t *host;
52    void *msg;
53    size_t *len;
54    const size_t msgsize;
55 {
56    const char *function = "udpheader_add()";
57    udpheader_t header;
58    unsigned char *offset;
59 
60    bzero(&header, sizeof(header));
61    header.host = *host;
62 
63    if (msgsize < (size_t)(*len + HEADERSIZE_UDP(&header))) {
64       swarnx("%s: could not prefix socks udp header of size %lu to udp "
65              "payload of length %lu: msgsize (%lu) is too short",
66              function,
67              (unsigned long)HEADERSIZE_UDP(&header),
68              (unsigned long)*len,
69              (unsigned long)msgsize);
70 
71       errno = EMSGSIZE;
72       return NULL;
73    }
74 
75    slog(LOG_DEBUG,
76         "%s: prefixing udp header with addr %s to buffer of len %lu, size %lu",
77         function,
78         sockshost2string(&header.host, NULL, 0),
79         (unsigned long)*len,
80         (unsigned long)msgsize);
81 
82    /* offset old contents by size of header we are about to prefix. */
83    memmove((char *)msg + HEADERSIZE_UDP(&header), msg, *len);
84    offset = msg;
85 
86    memcpy(offset, &header.flag, sizeof(header.flag));
87    offset += sizeof(header.flag);
88 
89    memcpy(offset, &header.frag, sizeof(header.frag));
90    offset += sizeof(header.frag);
91 
92    offset = sockshost2mem(&header.host, offset, PROXY_SOCKS_V5);
93    offset += *len; /* len bytes copied above. */
94 
95    *len = (char *)offset - (char *)msg;
96 
97    return msg;
98 }
99