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.
-
+
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
SPDX-License-Identifier: AGPL3.0-or-later
-*/
+ */
/**
* This code provides some support for doing STUN transactions. We
* receive the simplest possible packet as the STUN server and try
#include "gnunet_util_lib.h"
#include "nat_stun.h"
-#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
+#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__)
/**
- * Context for #stun_get_mapped().
+ * Context for #stun_get_mapped().
* Used to store state across processing attributes.
*/
-struct StunState
-{
+struct StunState {
uint16_t attr;
};
* @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)
+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);
+ 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)
+ {
+ 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;
- break;
- case STUN_XOR_MAPPED_ADDRESS:
- break;
- default:
- return GNUNET_NO;
- }
-
- if (ntohs (attr->len) < sizeof (struct stun_addr))
+ }
+
+ 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_port = returned_addr->port ^ htons(ntohl(magic) >> 16);
sa->sin_addr.s_addr = returned_addr->addr ^ magic;
return GNUNET_OK;
}
* #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)
+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;
* 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;
- }
+ {
+ 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);
+ 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_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);
+ 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;
+ LOG(GNUNET_ERROR_TYPE_INFO,
+ "Scrambled STUN packet length (got %d, expecting %d)\n",
+ advertised_message_size,
+ (int)len);
+ return GNUNET_NO;
}
- attr = (const struct stun_attr *) data;
-
- /* compute total attribute length */
- advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr);
+ len = advertised_message_size;
+ memset(&st, 0, sizeof(st));
- /* Check if we still have space in our buffer */
- if (advertised_message_size > len)
+ while (len > 0)
{
- LOG (GNUNET_ERROR_TYPE_INFO,
- "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
- advertised_message_size,
- (int) len);
- break;
+ 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;
}
- if (GNUNET_OK ==
- stun_get_mapped (&st,
- attr,
- hdr->magic,
- arg))
- ret = GNUNET_OK;
- data += advertised_message_size;
- len -= advertised_message_size;
- }
return ret;
}