Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / core / net / ip.c
blobed3c3f6501116a622722302bb9afbadc695b694b
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 <config.h>
21 #include <system.h>
22 #include <string.h>
23 #include <dev.h>
24 #include <net/eth.h>
25 #include <net/net.h>
26 #include <net/if.h>
27 #include <net/packet.h>
28 #include <net/ip.h>
29 #include <net/checksum.h>
32 void net_proto_ip_print (net_ipv4 ip)
34 unsigned char a = (unsigned char) ip;
35 unsigned char b = (unsigned char) (ip >> 8);
36 unsigned char c = (unsigned char) (ip >> 16);
37 unsigned char d = (unsigned char) (ip >> 24);
39 printf ("%d.%d.%d.%d", a, b, c, d);
42 net_ipv4 net_proto_ip_convert (char *ip)
44 if (!ip)
45 return 0;
47 unsigned char a = 0;
48 unsigned char b = 0;
49 unsigned char c = 0;
50 unsigned char d = 0;
52 unsigned g = 0;
53 unsigned i = 0;
54 unsigned y = strlen (ip);
56 if (!y)
57 return 0;
59 char *str = (char *) kmalloc (sizeof (char) * (y + 1));
61 if (!str)
62 return 0;
64 memcpy (str, ip, y);
65 str[y] = '\0';
67 unsigned h[4];
69 while (i < y) {
70 if (str[i] == '.') {
71 str[i] = '\0';
72 h[g] = i+1;
73 g ++;
76 i ++;
79 if (g != 3) {
80 kfree (str);
81 return 0;
84 a = atoi (str);
85 b = atoi (str+h[0]);
86 c = atoi (str+h[1]);
87 d = atoi (str+h[2]);
89 kfree (str);
91 return NET_IPV4_TO_ADDR (a, b, c, d);
94 unsigned net_proto_ip_convert2 (net_ipv4 ip, char *ip_addr)
96 if (!ip_addr)
97 return 0;
99 unsigned char a = (unsigned char) ip;
100 unsigned char b = (unsigned char) (ip >> 8);
101 unsigned char c = (unsigned char) (ip >> 16);
102 unsigned char d = (unsigned char) (ip >> 24);
104 sprintf (ip_addr, "%d.%d.%d.%d", a, b, c, d);
106 return 1;
109 unsigned net_proto_ip_network (net_ipv4 ip)
111 unsigned char a = (unsigned char) ip;
113 if (a == 192 || a == 10 || a == 127 || a == 255) /* TODO: Dynamic */
114 return 0;
115 else
116 return 1;
118 return 0;
121 unsigned net_proto_ip_send (netif_t *netif, packet_t *packet, proto_ip_t *ip, char *buf, unsigned len)
123 char *buf_ip = (char *) kmalloc ((len + 1 + sizeof (proto_ip_t)));
125 if (!buf_ip)
126 return 0;
128 unsigned l = sizeof (proto_ip_t);
130 /* 16bit IP header checksum */
131 ip->checksum = checksum16 (ip, l);
133 /* put ip header and buf data to one buffer */
134 memcpy (buf_ip, (char *) ip, l);
135 memcpy (buf_ip+l, buf, len);
137 buf_ip[l+len] = '\0';
139 /* send packet */
140 unsigned ret = net_packet_send (netif, packet, buf_ip, l+len);
142 kfree (buf_ip);
144 return ret;
147 unsigned net_proto_ip_handler (packet_t *packet, char *buf, unsigned len)
149 proto_ip_t *ip = (proto_ip_t *) buf;
151 /* calculate real length of header */
152 unsigned head_len = ip->head_len * 4;
154 switch (ip->proto) {
155 case NET_PROTO_IP_TYPE_TCP:
156 return net_proto_tcp_handler (packet, ip, buf+head_len, len-head_len);
157 case NET_PROTO_IP_TYPE_UDP:
158 return net_proto_udp_handler (packet, ip, buf+head_len, len-head_len);
159 case NET_PROTO_IP_TYPE_ICMP:
160 return net_proto_icmp_handler (packet, ip, buf+head_len, len-head_len);
161 #ifdef CONFIG_PROTO_TUN6
162 case NET_PROTO_IP_TYPE_TUN6:
163 return net_proto_tun6_handler (packet, ip, buf+head_len, len-head_len);
164 #endif
167 return 0;