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 version 3 as
24 * published by the Free Software Foundation.
26 * uAnytun is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
35 #include "datatypes.h"
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <arpa/inet.h>
49 #include <netinet/in.h>
51 int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resolv_addr_type_t resolv_type)
57 memset(&(sock->local_end_), 0, sizeof(sock->local_end_));
58 memset(&(sock->remote_end_), 0, sizeof(sock->local_end_));
59 sock->remote_end_set_ = 0;
61 struct addrinfo hints, *res;
64 memset (&hints, 0, sizeof (hints));
65 hints.ai_socktype = SOCK_DGRAM;
66 hints.ai_flags |= AI_PASSIVE;
69 case IPV4_ONLY: hints.ai_family = PF_INET; break;
70 case IPV6_ONLY: hints.ai_family = PF_INET6; break;
71 default: hints.ai_family = PF_UNSPEC; break;
74 int errcode = getaddrinfo(local_addr, port, &hints, &res);
76 log_printf(ERROR, "Error resolving local address (%s:%s): %s", (local_addr) ? local_addr : "*", port, gai_strerror(errcode));
83 log_printf(ERROR, "getaddrinfo returned no address for %s:%s", local_addr, port);
87 memcpy(&(sock->local_end_), res->ai_addr, res->ai_addrlen);
89 sock->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
91 log_printf(ERROR, "Error on opening udp socket: %s", strerror(errno));
97 errcode = bind(sock->fd_, res->ai_addr, res->ai_addrlen);
99 log_printf(ERROR, "Error on binding udp socket: %s", strerror(errno));
105 /* this doesn't work on linux ?? */
106 /* #ifdef NO_V4MAPPED */
107 /* if(res->ai_family == AF_INET6) { */
108 /* log_printf(NOTICE, "disabling V4-Mapped addresses"); */
110 /* if(setsockopt(sock->fd_, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on))) */
111 /* log_printf(ERROR, "Error on setting IPV6_V6ONLY socket option: %s", strerror(errno)); */
119 int udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port, resolv_addr_type_t resolv_type)
121 if(!sock || !remote_addr || !port)
124 struct addrinfo hints, *res;
127 memset (&hints, 0, sizeof (hints));
128 hints.ai_socktype = SOCK_DGRAM;
130 switch(resolv_type) {
131 case IPV4_ONLY: hints.ai_family = PF_INET; break;
132 case IPV6_ONLY: hints.ai_family = PF_INET6; break;
133 default: hints.ai_family = PF_UNSPEC; break;
136 int errcode = getaddrinfo(remote_addr, port, &hints, &res);
138 log_printf(ERROR, "Error resolving remote address (%s:%s): %s", (remote_addr) ? remote_addr : "*", port, gai_strerror(errcode));
142 log_printf(ERROR, "getaddrinfo returned no address for %s:%s", remote_addr, port);
145 memcpy(&(sock->remote_end_), res->ai_addr, res->ai_addrlen);
146 sock->remote_end_set_ = 1;
152 void udp_close(udp_socket_t* sock)
161 char* udp_endpoint_to_string(udp_endpoint_t e)
165 size_t addrstr_len = 0;
167 char addrport_sep = ':';
169 switch (((struct sockaddr *)&e)->sa_family)
172 ptr = &((struct sockaddr_in *)&e)->sin_addr;
173 port = ntohs(((struct sockaddr_in *)&e)->sin_port);
174 addrstr_len = INET_ADDRSTRLEN + 1;
178 ptr = &((struct sockaddr_in6 *)&e)->sin6_addr;
179 port = ntohs(((struct sockaddr_in6 *)&e)->sin6_port);
180 addrstr_len = INET6_ADDRSTRLEN + 1;
184 asprintf(&ret, "unknown address type");
187 addrstr = malloc(addrstr_len);
190 inet_ntop (((struct sockaddr *)&e)->sa_family, ptr, addrstr, addrstr_len);
191 asprintf(&ret, "%s%c%d", addrstr, addrport_sep ,port);
196 char* udp_get_local_end_string(udp_socket_t* sock)
201 return udp_endpoint_to_string(sock->local_end_);
204 char* udp_get_remote_end_string(udp_socket_t* sock)
206 if(!sock || !sock->remote_end_set_)
209 return udp_endpoint_to_string(sock->remote_end_);
212 int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end)
214 if(!sock || !remote_end)
217 socklen_t socklen = sizeof(*remote_end);
218 return recvfrom(sock->fd_, buf, len, 0, (struct sockaddr *)remote_end, &socklen);
221 int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len)
223 if(!sock || !sock->remote_end_set_)
226 socklen_t socklen = sizeof(sock->remote_end_);
228 if((((struct sockaddr *)&sock->local_end_)->sa_family) == AF_INET)
229 socklen = sizeof(struct sockaddr_in);
230 else if ((((struct sockaddr *)&sock->local_end_)->sa_family) == AF_INET6)
231 socklen = sizeof(struct sockaddr_in6);
233 return sendto(sock->fd_, buf, len, 0, (struct sockaddr *)&(sock->remote_end_), socklen);;