New revision of the Tunnel6 client with improvements and routed prefix support
[tunnel6.git] / client / src / ipv6.c
blob746baac366039b3f49ce7b0dfda4063eff03a066
1 /*
2 * tunnel6
3 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef __WIN32__
21 # include <arpa/inet.h>
22 # include <string.h>
23 #else
24 # include <windows.h>
25 # include <ws2tcpip.h>
26 #endif
27 #include <stdio.h>
28 #include "ipv6.h"
29 #include "packet.h"
30 #include "defaults.h"
32 static int fd; /* FD for IPv6 packet sender */
33 static char ipv6[64]; /* temporary buffer used by ipv6_print () */
34 #ifdef __WIN32__
35 extern const char *inet_ntop (int af, const void *src, char *dst, socklen_t cnt);
36 #endif
38 /* compare two IPv6 address */
39 unsigned ipv6_cmp (ipv6_addr_t ip, ipv6_addr_t ip2)
41 unsigned i;
42 for (i = 0; i < 8; i ++)
43 if (ip[i] != ip2[i])
44 return 0;
46 return 1;
49 /* compare two IPv6 prefix address */
50 unsigned ipv6_cmp_prefix (ipv6_prefix_t pr, ipv6_prefix_t ip)
52 /* check valid prefix length */
53 if (!pr[15])
54 return 0;
56 unsigned i;
57 for (i = 0; i < (pr[15]/8); i ++)
58 if (pr[i] != ip[i])
59 return 0;
61 return 1;
64 /* print IPv6 address onto console */
65 char *ipv6_print (ipv6_addr_t ip)
67 if (!inet_ntop (AF_INET6, ip, ipv6, 64))
68 return 0;
70 return ipv6;
73 int ipv6_send (char *data, int len)
75 struct sockaddr_in6 sockaddr;
76 struct proto_ipv6_t *ip = (struct proto_ipv6_t *) data;
78 /* prepare data for RAW socket */
79 sockaddr.sin6_family = AF_INET6;
80 sockaddr.sin6_port = 0;
81 memcpy (&sockaddr.sin6_addr.s6_addr, ip->dest, sizeof (ipv6_addr_t));
83 /* send the packet */
84 if (sendto (fd, data, len, 0, (struct sockaddr *) &sockaddr, sizeof (struct sockaddr_in6)) != len) {
85 perror ("sendto()");
86 return -1;
89 return 0;
92 int ipv6_init ()
94 /* initialize raw socket */
95 if ((fd = socket (AF_INET6, SOCK_RAW, IPPROTO_RAW)) == -1) {
96 perror ("socket()");
97 return -1;
100 return 0;