Kernel 0.5.8-r8; tcp protocol is finished from base and was merge with sockets, added...
[ZeXOS.git] / kernel / core / net / packet.c
blob376d8537d208db34ecb151bbab4f5414e2f6977c
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 <task.h>
29 extern netif_t netif_list;
30 extern unsigned long timer_ticks;
32 task_t *task_netcore = NULL;
34 char *buf_tx = NULL;
36 unsigned net_packet_send (netif_t *netif, packet_t *packet, char *buf, unsigned len)
38 if (!netif)
39 return 0;
41 memset (buf_tx, 0, 2048);
43 memcpy (buf_tx, (char *) packet, 14);
44 memcpy (buf_tx+14, buf, len);
46 buf_tx[14+len] = '\0';
48 netif->dev->write ((char *) buf_tx, 14+len);
50 /* add transfered bytes to stats */
51 netif->dev->info_tx += 14+len;
53 return 1;
56 /* Here we process a received packets */
57 unsigned net_packet_handler (char *buf, unsigned len)
59 packet_t *packet = (packet_t *) buf;
61 if (!packet)
62 return 0;
64 //printf ("net_packet->type: 0x%04x\n", swap16 (packet->type));
66 switch (packet->type) {
67 case NET_PACKET_TYPE_IPV4:
68 return net_proto_ip_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
69 case NET_PACKET_TYPE_ARP:
70 return net_proto_arp_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
73 return 1;
76 /* Task for checking new incoming packets */
77 unsigned task_net_packet ()
79 /* let's find network device */
80 netif_t *netif = netif_findbyname ("eth0");
82 if (!netif) {
83 task_done (task_netcore);
84 schedule ();
85 return 0;
88 /* alloc 2k packet receive and transfer buffer */
89 char *buf_rx = (char *) kmalloc (2048 * sizeof (char));
90 buf_tx = (char *) kmalloc (2048 * sizeof (char));
92 if (!buf_rx || !buf_tx) {
93 task_done (task_netcore);
94 schedule ();
95 return 0;
98 /* thread loop */
99 while (1) {
100 /* check for incoming data */
101 int ret = netdev_rx (netif->dev, buf_rx, 2048);
103 /* are there some data ? */
104 if (ret) {
105 net_packet_handler (buf_rx, ret);
107 /* clear buffer */
108 memset (buf_rx, 0, 2048);
110 netif->dev->info_rx += ret;
113 schedule ();
116 kfree (buf_tx);
117 kfree (buf_rx);
119 task_done (task_netcore);
120 schedule ();
122 return 1;
126 unsigned init_packet ()
128 if (!init_net_proto_tcp ())
129 return 0;
131 /* start new task/thread for network core */
132 task_netcore = (task_t *) task_create ("netcore", (unsigned) task_net_packet, 255);
134 if (!task_netcore)
135 return 0;
137 return 1;