aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-service-nat_stun.c
blob: 26acf715195142d1f484617597be4720b93e2803 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
     This file is part of GNUnet.
     Copyright (C) 2009, 2015, 2016 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
*/
/**
 * This code provides some support for doing STUN transactions.  We
 * receive the simplest possible packet as the STUN server and try
 * to respond properly.
 *
 * All STUN packets start with a simple header made of a type,
 * length (excluding the header) and a 16-byte random transaction id.
 * Following the header we may have zero or more attributes, each
 * structured as a type, length and a value (whose format depends
 * on the type, but often contains addresses).
 * Of course all fields are in network format.
 *
 * This code was based on ministun.c.
 *
 * @file nat/gnunet-service-nat_stun.c
 * @brief Functions for STUN functionality
 * @author Bruno Souza Cabral
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "nat_stun.h"

#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__)


/**
 * Context for #stun_get_mapped(). 
 * Used to store state across processing attributes.
 */
struct StunState
{
  uint16_t attr;
};


/**
 * Extract the STUN_MAPPED_ADDRESS from the stun response.
 * This is used as a callback for stun_handle_response
 * when called from stun_request.
 *
 * @param[out] st pointer where we will set the type
 * @param attr received stun attribute
 * @param magic Magic cookie
 * @param[out] arg pointer to a sockaddr_in where we will set the reported IP and port
 * @return #GNUNET_OK if @a arg was initialized
 */
static int
stun_get_mapped (struct StunState *st,
                 const struct stun_attr *attr,
		 uint32_t magic,
                 struct sockaddr_in *arg)
{
  const struct stun_addr *returned_addr;
  struct sockaddr_in *sa = (struct sockaddr_in *) arg;
  uint16_t type = ntohs (attr->attr);

  switch (type)
  {
  case STUN_MAPPED_ADDRESS:
    if ( (st->attr == STUN_XOR_MAPPED_ADDRESS) ||
	 (st->attr == STUN_MS_XOR_MAPPED_ADDRESS) )
      return GNUNET_NO;
    magic = 0;
    break;
  case STUN_MS_XOR_MAPPED_ADDRESS:
    if (st->attr == STUN_XOR_MAPPED_ADDRESS)
      return GNUNET_NO;
    break;
  case STUN_XOR_MAPPED_ADDRESS:
    break;
  default:
    return GNUNET_NO;
  }  
  
  if (ntohs (attr->len) < sizeof (struct stun_addr))
    return GNUNET_NO;
  returned_addr = (const struct stun_addr *)(attr + 1);
  if (AF_INET != returned_addr->family)
    return GNUNET_NO;
  st->attr = type;
  sa->sin_family = AF_INET;
  sa->sin_port = returned_addr->port ^ htons (ntohl(magic) >> 16);
  sa->sin_addr.s_addr = returned_addr->addr ^ magic;
  return GNUNET_OK;
}


/**
 * Handle an incoming STUN response.  Do some basic sanity checks on
 * packet size and content, try to extract information.
 * At the moment this only processes BIND requests,
 * and returns the externally visible address of the original
 * request.
 *
 * @param data the packet
 * @param len the length of the packet in @a data
 * @param[out] arg sockaddr_in where we will set our discovered address
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO if the packet is invalid (not a stun packet)
 */
int
GNUNET_NAT_stun_handle_packet_ (const void *data,
				size_t len,
				struct sockaddr_in *arg)
{
  const struct stun_header *hdr;
  const struct stun_attr *attr;
  struct StunState st;
  uint32_t advertised_message_size;
  uint32_t message_magic_cookie;
  int ret = GNUNET_SYSERR;

  /* On entry, 'len' is the length of the UDP payload. After the
   * initial checks it becomes the size of unprocessed options,
   * while 'data' is advanced accordingly.
   */
  if (len < sizeof(struct stun_header))
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Packet too short to be a STUN packet\n");
    return GNUNET_NO;
  }
  hdr = data;
  /* Skip header as it is already in hdr */
  len -= sizeof(struct stun_header);
  data += sizeof(struct stun_header);

  /* len as advertised in the message */
  advertised_message_size = ntohs (hdr->msglen);
  message_magic_cookie = ntohl (hdr->magic);
  /* Compare if the cookie match */
  if (STUN_MAGIC_COOKIE != message_magic_cookie)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Invalid magic cookie for STUN packet\n");
    return GNUNET_NO;
  }

  LOG (GNUNET_ERROR_TYPE_INFO,
       "STUN Packet, msg %s (%04x), length: %d\n",
       stun_msg2str (ntohs (hdr->msgtype)),
       ntohs (hdr->msgtype),
       advertised_message_size);
  if (advertised_message_size > len)
  {
    LOG (GNUNET_ERROR_TYPE_INFO,
         "Scrambled STUN packet length (got %d, expecting %d)\n",
         advertised_message_size,
         (int) len);
    return GNUNET_NO;
  }
  len = advertised_message_size;
  memset (&st, 0, sizeof(st));

  while (len > 0)
  {
    if (len < sizeof (struct stun_attr))
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           "Attribute too short (got %d, expecting %d)\n",
           (int) len,
           (int) sizeof (struct stun_attr));
      break;
    }
    attr = (const struct stun_attr *) data;

    /* compute total attribute length */
    advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr);

    /* Check if we still have space in our buffer */
    if (advertised_message_size > len)
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
           advertised_message_size,
           (int) len);
      break;
    }
    if (GNUNET_OK ==
	stun_get_mapped (&st,
			 attr,
			 hdr->magic,
			 arg))
      ret = GNUNET_OK;
    data += advertised_message_size;
    len -= advertised_message_size;
  }
  return ret;
}

/* end of gnunet-service-nat_stun.c */