Kernel 0.5.8-r8; tcp protocol is finished from base and was merge with sockets, added...
[ZeXOS.git] / kernel / core / net / ip.c
blob4020fdb7fe3a04c36304fb3490d93117bc47e53f
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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_proto_ip_print (net_ipv4 ip)
33 unsigned char a = (unsigned char) ip;
34 unsigned char b = (unsigned char) (ip >> 8);
35 unsigned char c = (unsigned char) (ip >> 16);
36 unsigned char d = (unsigned char) (ip >> 24);
38 printf ("%d.%d.%d.%d", a, b, c, d);
41 net_ipv4 net_proto_ip_convert (char *ip)
43 unsigned char a = 0;
44 unsigned char b = 0;
45 unsigned char c = 0;
46 unsigned char d = 0;
48 unsigned g = 0;
49 unsigned i = 0;
50 unsigned y = strlen (ip);
51 ip[y-1] = '\0';
53 unsigned h[4];
55 while (i < y) {
56 if (ip[i] == '.') {
57 ip[i] = '\0';
58 h[g] = i+1;
59 g ++;
62 i ++;
65 if (g != 3)
66 return 0;
68 a = atoi (ip);
69 b = atoi (ip+h[0]);
70 c = atoi (ip+h[1]);
71 d = atoi (ip+h[2]);
73 return NET_IPV4_TO_ADDR (a, b, c, d);
76 unsigned net_proto_ip_network (net_ipv4 ip)
78 unsigned char a = (unsigned char) ip;
80 if (a == 192 || a == 10 || a == 127)
81 return 0;
82 else
83 return 1;
85 return 0;
88 unsigned net_proto_ip_send (netif_t *netif, packet_t *packet, proto_ip_t *ip, char *buf, unsigned len)
90 char *buf_ip = (char *) kmalloc ((len+sizeof (proto_ip_t)) * sizeof (char));
92 if (!buf_ip)
93 return 0;
95 unsigned l = sizeof (proto_ip_t);
97 /* 16bit IP header checksum */
98 ip->checksum = checksum16 (ip, l);
100 /* put ip header and buf data to one buffer */
101 memcpy (buf_ip, (char *) ip, l);
102 memcpy (buf_ip+l, buf, len);
104 buf_ip[l+len] = '\0';
106 /* send packet */
107 unsigned ret = net_packet_send (netif, packet, buf_ip, l+len);
109 kfree (buf_ip);
111 return ret;
114 unsigned net_proto_ip_handler (packet_t *packet, char *buf, unsigned len)
116 proto_ip_t *ip = (proto_ip_t *) buf;
118 /* calculate real length of header */
119 unsigned head_len = ip->head_len * 4;
121 switch (ip->proto) {
122 case NET_PROTO_IP_TYPE_TCP:
123 return net_proto_tcp_handler (packet, ip, buf+head_len, len-head_len);
124 case NET_PROTO_IP_TYPE_ICMP:
125 return net_proto_icmp_handler (packet, ip, buf+head_len, len-head_len);
128 return 0;