3 * Copyright (C) 2008 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/>.
26 #include <net/packet.h>
28 #include <net/checksum.h>
31 void NET_IPV6_TO_ADDR (net_ipv6 ip
, unsigned short a
, unsigned short b
, unsigned short c
, unsigned short d
,
32 unsigned short e
, unsigned short f
, unsigned short g
, unsigned short h
) {
34 unsigned short buf
[8];
45 memcpy (ip
, (void *) buf
, sizeof (net_ipv6
));
48 void net_proto_ipv6_print (net_ipv6 ip
)
52 for (i
= 0; i
< 7; i
++) {
54 printf ("%04x:", swap16 (ip
[i
]));
60 printf ("%x", swap16 (ip
[7]));
65 unsigned net_proto_ipv6_cmp (net_ipv6 ip
, net_ipv6 ip2
)
68 for (i
= 0; i
< 8; i
++)
75 unsigned net_proto_ipv6_cmp_prefix (net_ipv6 ip
, net_ipv6 ip2
, unsigned char prefix
)
78 for (i
= 0; i
< 8; i
++) {
89 unsigned net_proto_ipv6_convert (net_ipv6 ipv6
, char *ip
)
102 unsigned y
= strlen (ip
);
124 a
= strtol (ip
, &endptr
, 16);
125 b
= strtol (ip
+k
[0], &endptr
, 16);
126 c
= strtol (ip
+k
[1], &endptr
, 16);
127 d
= strtol (ip
+k
[2], &endptr
, 16);
128 e
= strtol (ip
+k
[3], &endptr
, 16);
129 f
= strtol (ip
+k
[4], &endptr
, 16);
130 g
= strtol (ip
+k
[5], &endptr
, 16);
131 h
= strtol (ip
+k
[6], &endptr
, 16);
133 NET_IPV6_TO_ADDR (ipv6
, a
, b
, c
, d
, e
, f
, g
, h
);
138 /*unsigned net_proto_ip_convert2 (net_ipv4 ip, char *ip_addr)
143 unsigned char a = (unsigned char) ip;
144 unsigned char b = (unsigned char) (ip >> 8);
145 unsigned char c = (unsigned char) (ip >> 16);
146 unsigned char d = (unsigned char) (ip >> 24);
148 sprintf (ip_addr, "%d.%d.%d.%d", a, b, c, d);
154 unsigned net_proto_ipv6_network (net_ipv6 ip
)
156 unsigned short a
= (unsigned short) swap16 (ip
[0]);
158 if (a
== 0xfc00 || a
== 0xfc02)
166 unsigned net_proto_ipv6_send (netif_t
*netif
, packet_t
*packet
, proto_ipv6_t
*ip
, char *buf
, unsigned len
)
168 char *buf_ip
= (char *) kmalloc ((len
+ 1 + sizeof (proto_ipv6_t
)));
173 unsigned l
= sizeof (proto_ipv6_t
);
175 /* put ip header and buf data to one buffer */
176 memcpy (buf_ip
, (char *) ip
, l
);
177 memcpy (buf_ip
+l
, buf
, len
);
179 buf_ip
[l
+len
] = '\0';
182 unsigned ret
= net_packet_send (netif
, packet
, buf_ip
, l
+len
);
189 unsigned net_proto_ipv6_handler (packet_t
*packet
, char *buf
, unsigned len
)
191 proto_ipv6_t
*ip
= (proto_ipv6_t
*) buf
;
193 /* ipv6 provide directly length of data - payload length */
194 unsigned data_len
= swap16 (ip
->pl_len
);
196 //printf ("ipv6 -> 0x%x\n", ip->nhead);
199 case NET_PROTO_IP_TYPE_TCP6
:
200 return net_proto_tcp6_handler (packet
, ip
, buf
+sizeof (proto_ipv6_t
), data_len
);
201 case NET_PROTO_IP_TYPE_UDP6
:
202 return net_proto_udp6_handler (packet
, ip
, buf
+sizeof (proto_ipv6_t
), data_len
);
203 case NET_PROTO_IP_TYPE_ICMP6
:
204 return net_proto_icmp6_handler (packet
, ip
, buf
+sizeof (proto_ipv6_t
), data_len
);