4 * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5 * featured implementation uAnytun has no support for multiple connections
6 * or synchronisation. It is a small single threaded implementation intended
7 * to act as a client on small platforms.
8 * The secure anycast tunneling protocol (satp) defines a protocol used
9 * for communication between any combination of unicast and anycast
10 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
11 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
12 * ethernet, ip, arp ...). satp directly includes cryptography and
13 * message authentication based on the methodes used by SRTP. It is
14 * intended to deliver a generic, scaleable and secure solution for
15 * tunneling and relaying of packets of any protocol.
18 * Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
22 * uAnytun is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
27 * uAnytun is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
36 #include "datatypes.h"
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <arpa/inet.h>
50 #include <netinet/in.h>
52 int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resolv_addr_type_t resolv_type)
58 memset(&(sock->local_end_), 0, sizeof(sock->local_end_));
59 memset(&(sock->remote_end_), 0, sizeof(sock->local_end_));
60 sock->remote_end_set_ = 0;
62 struct addrinfo hints, *res;
65 memset (&hints, 0, sizeof (hints));
66 hints.ai_socktype = SOCK_DGRAM;
67 hints.ai_flags |= AI_PASSIVE;
70 case IPV4_ONLY: hints.ai_family = PF_INET; break;
71 case IPV6_ONLY: hints.ai_family = PF_INET6; break;
72 default: hints.ai_family = PF_UNSPEC; break;
75 int errcode = getaddrinfo(local_addr, port, &hints, &res);
77 log_printf(ERROR, "Error resolving local address (%s:%s): %s", (local_addr) ? local_addr : "*", port, gai_strerror(errcode));
84 log_printf(ERROR, "getaddrinfo returned no address for %s:%s", local_addr, port);
88 memcpy(&(sock->local_end_), res->ai_addr, res->ai_addrlen);
90 sock->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
92 log_printf(ERROR, "Error on opening udp socket: %s", strerror(errno));
98 errcode = bind(sock->fd_, res->ai_addr, res->ai_addrlen);
100 log_printf(ERROR, "Error on binding udp socket: %s", strerror(errno));
106 /* this doesn't work on linux ?? */
107 /* #ifdef NO_V4MAPPED */
108 /* if(res->ai_family == AF_INET6) { */
109 /* log_printf(NOTICE, "disabling V4-Mapped addresses"); */
111 /* if(setsockopt(sock->fd_, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on))) */
112 /* log_printf(ERROR, "Error on setting IPV6_V6ONLY socket option: %s", strerror(errno)); */
120 int udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port, resolv_addr_type_t resolv_type)
122 if(!sock || !remote_addr || !port)
125 struct addrinfo hints, *res;
128 memset (&hints, 0, sizeof (hints));
129 hints.ai_socktype = SOCK_DGRAM;
131 switch(resolv_type) {
132 case IPV4_ONLY: hints.ai_family = PF_INET; break;
133 case IPV6_ONLY: hints.ai_family = PF_INET6; break;
134 default: hints.ai_family = PF_UNSPEC; break;
137 int errcode = getaddrinfo(remote_addr, port, &hints, &res);
139 log_printf(ERROR, "Error resolving remote address (%s:%s): %s", (remote_addr) ? remote_addr : "*", port, gai_strerror(errcode));
143 log_printf(ERROR, "getaddrinfo returned no address for %s:%s", remote_addr, port);
146 memcpy(&(sock->remote_end_), res->ai_addr, res->ai_addrlen);
147 sock->remote_end_set_ = 1;
153 void udp_close(udp_socket_t* sock)
162 char* udp_endpoint_to_string(udp_endpoint_t e)
166 size_t addrstr_len = 0;
168 char addrport_sep = ':';
170 switch (((struct sockaddr *)&e)->sa_family)
173 ptr = &((struct sockaddr_in *)&e)->sin_addr;
174 port = ntohs(((struct sockaddr_in *)&e)->sin_port);
175 addrstr_len = INET_ADDRSTRLEN + 1;
179 ptr = &((struct sockaddr_in6 *)&e)->sin6_addr;
180 port = ntohs(((struct sockaddr_in6 *)&e)->sin6_port);
181 addrstr_len = INET6_ADDRSTRLEN + 1;
185 asprintf(&ret, "unknown address type");
188 addrstr = malloc(addrstr_len);
191 inet_ntop (((struct sockaddr *)&e)->sa_family, ptr, addrstr, addrstr_len);
192 asprintf(&ret, "%s%c%d", addrstr, addrport_sep ,port);
197 char* udp_get_local_end_string(udp_socket_t* sock)
202 return udp_endpoint_to_string(sock->local_end_);
205 char* udp_get_remote_end_string(udp_socket_t* sock)
207 if(!sock || !sock->remote_end_set_)
210 return udp_endpoint_to_string(sock->remote_end_);
213 int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end)
215 if(!sock || !remote_end)
218 socklen_t socklen = sizeof(*remote_end);
219 return recvfrom(sock->fd_, buf, len, 0, (struct sockaddr *)remote_end, &socklen);
222 int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len)
224 if(!sock || !sock->remote_end_set_)
227 socklen_t socklen = sizeof(sock->remote_end_);
229 if((((struct sockaddr *)&sock->local_end_)->sa_family) == AF_INET)
230 socklen = sizeof(struct sockaddr_in);
231 else if ((((struct sockaddr *)&sock->local_end_)->sa_family) == AF_INET6)
232 socklen = sizeof(struct sockaddr_in6);
234 return sendto(sock->fd_, buf, len, 0, (struct sockaddr *)&(sock->remote_end_), socklen);;