Fixed buffer overflow in mserver; fixed type of checkinfo () 2nd parameter; memory
[ZeXOS.git] / kernel / core / net / ip.c
blobc06e3560a6cff26f4bbb70ba3fb37a61ed4074fa
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>
31 static char buf_ip_prealloc[NET_PACKET_MTU + sizeof (net_ipv4) + 1];
33 void net_proto_ip_print (net_ipv4 ip)
35 unsigned char a = (unsigned char) ip;
36 unsigned char b = (unsigned char) (ip >> 8);
37 unsigned char c = (unsigned char) (ip >> 16);
38 unsigned char d = (unsigned char) (ip >> 24);
40 printf ("%d.%d.%d.%d", a, b, c, d);
43 net_ipv4 net_proto_ip_convert (char *ip)
45 if (!ip)
46 return 0;
48 unsigned char a = 0;
49 unsigned char b = 0;
50 unsigned char c = 0;
51 unsigned char d = 0;
53 unsigned g = 0;
54 unsigned i = 0;
55 unsigned y = strlen (ip);
57 if (!y)
58 return 0;
60 char *str = (char *) kmalloc (sizeof (char) * (y + 1));
62 if (!str)
63 return 0;
65 memcpy (str, ip, y);
66 str[y] = '\0';
68 unsigned h[4];
70 while (i < y) {
71 if (str[i] == '.') {
72 str[i] = '\0';
73 h[g] = i+1;
74 g ++;
77 i ++;
80 if (g != 3) {
81 kfree (str);
82 return 0;
85 a = atoi (str);
86 b = atoi (str+h[0]);
87 c = atoi (str+h[1]);
88 d = atoi (str+h[2]);
90 kfree (str);
92 return NET_IPV4_TO_ADDR (a, b, c, d);
95 unsigned net_proto_ip_convert2 (net_ipv4 ip, char *ip_addr)
97 if (!ip_addr)
98 return 0;
100 unsigned char a = (unsigned char) ip;
101 unsigned char b = (unsigned char) (ip >> 8);
102 unsigned char c = (unsigned char) (ip >> 16);
103 unsigned char d = (unsigned char) (ip >> 24);
105 sprintf (ip_addr, "%d.%d.%d.%d", a, b, c, d);
107 return 1;
110 unsigned net_proto_ip_network (net_ipv4 ip)
112 unsigned char a = (unsigned char) ip;
114 if (a == 192 || a == 10 || a == 127 || a == 255) /* TODO: Dynamic */
115 return 0;
116 else
117 return 1;
119 return 0;
122 unsigned net_proto_ip_send (netif_t *netif, packet_t *packet, proto_ip_t *ip, char *buf, unsigned len)
124 if (len +1 + sizeof (proto_ip_t) > NET_PACKET_MTU + sizeof (net_ipv4)) {
125 kprintf ("ERROR -> IP packet is too long (%d/%d Bytes)\n", len + sizeof (proto_ip_t), NET_PACKET_MTU + sizeof (net_ipv4));
126 return 0;
129 char *buf_ip = (char *) &buf_ip_prealloc; //(char *) kmalloc ((len + 1 + sizeof (proto_ip_t)));
131 if (!buf_ip)
132 return 0;
134 unsigned l = sizeof (proto_ip_t);
136 /* 16bit IP header checksum */
137 ip->checksum = checksum16 (ip, l);
139 /* put ip header and buf data to one buffer */
140 memcpy (buf_ip, (char *) ip, l);
141 memcpy (buf_ip+l, buf, len);
143 /* send packet */
144 unsigned ret = net_packet_send (netif, packet, buf_ip, l+len);
146 //kfree (buf_ip);
148 return ret;
151 unsigned net_proto_ip_handler (packet_t *packet, char *buf, unsigned len)
153 proto_ip_t *ip = (proto_ip_t *) buf;
155 /* calculate real length of header */
156 unsigned head_len = ip->head_len * 4;
158 switch (ip->proto) {
159 case NET_PROTO_IP_TYPE_TCP:
160 return net_proto_tcp_handler (packet, ip, buf+head_len, len-head_len);
161 case NET_PROTO_IP_TYPE_UDP:
162 return net_proto_udp_handler (packet, ip, buf+head_len, len-head_len);
163 case NET_PROTO_IP_TYPE_ICMP:
164 return net_proto_icmp_handler (packet, ip, buf+head_len, len-head_len);
165 #ifdef CONFIG_PROTO_TUN6
166 case NET_PROTO_IP_TYPE_TUN6:
167 return net_proto_tun6_handler (packet, ip, buf+head_len, len-head_len);
168 #endif
171 return 0;