Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / core / net / ipv6.c
bloba84ea6841c476f67262eed1555aac6db8000e209
1 /*
2 * ZeX/OS
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/>.
20 #include <system.h>
21 #include <string.h>
22 #include <dev.h>
23 #include <net/eth.h>
24 #include <net/net.h>
25 #include <net/if.h>
26 #include <net/packet.h>
27 #include <net/ip.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];
36 buf[0] = swap16 (a);
37 buf[1] = swap16 (b);
38 buf[2] = swap16 (c);
39 buf[3] = swap16 (d);
40 buf[4] = swap16 (e);
41 buf[5] = swap16 (f);
42 buf[6] = swap16 (g);
43 buf[7] = swap16 (h);
45 memcpy (ip, (void *) buf, sizeof (net_ipv6));
48 void net_proto_ipv6_print (net_ipv6 ip)
50 unsigned i = 0;
52 for (i = 0; i < 7; i ++) {
53 if (ip[i])
54 printf ("%04x:", swap16 (ip[i]));
55 else
56 printf ("0:");
59 if (ip[7])
60 printf ("%x", swap16 (ip[7]));
61 else
62 printf ("0");
65 unsigned net_proto_ipv6_cmp (net_ipv6 ip, net_ipv6 ip2)
67 unsigned i;
68 for (i = 0; i < 8; i ++)
69 if (ip[i] != ip2[i])
70 return 0;
72 return 1;
75 unsigned net_proto_ipv6_cmp_prefix (net_ipv6 ip, net_ipv6 ip2, unsigned char prefix)
77 unsigned i;
78 for (i = 0; i < 8; i ++) {
79 if (i >= prefix/16)
80 break;
82 if (ip[i] != ip2[i])
83 return 0;
86 return 1;
89 unsigned net_proto_ipv6_convert (net_ipv6 ipv6, char *ip)
91 unsigned short a;
92 unsigned short b;
93 unsigned short c;
94 unsigned short d;
95 unsigned short e;
96 unsigned short f;
97 unsigned short g;
98 unsigned short h;
100 unsigned j = 0;
101 unsigned i = 0;
102 unsigned y = strlen (ip);
104 if (!y)
105 return 0;
107 unsigned k[8];
109 while (i < y) {
110 if (ip[i] == ':') {
111 ip[i] = '\0';
112 k[j] = i+1;
113 j ++;
116 i ++;
119 if (j != 7)
120 return 0;
122 char *endptr;
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);
135 return 1;
138 /*unsigned net_proto_ip_convert2 (net_ipv4 ip, char *ip_addr)
140 if (!ip_addr)
141 return 0;
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);
150 return 1;
154 unsigned net_proto_ipv6_network (net_ipv6 ip)
156 unsigned short a = (unsigned short) swap16 (ip[0]);
158 if (a == 0xfc00 || a == 0xfc02)
159 return 0;
160 else
161 return 1;
163 return 0;
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)));
170 if (!buf_ip)
171 return 0;
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';
181 /* send packet */
182 unsigned ret = net_packet_send (netif, packet, buf_ip, l+len);
184 kfree (buf_ip);
186 return ret;
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);
198 switch (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);
207 return 0;