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/>.
26 #include <net/packet.h>
29 extern netif_t netif_list
;
30 extern unsigned long timer_ticks
;
32 task_t
*task_netcore
= NULL
;
36 unsigned net_packet_send (netif_t
*netif
, packet_t
*packet
, char *buf
, unsigned len
)
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
;
56 /* Here we process a received packets */
57 unsigned net_packet_handler (char *buf
, unsigned len
)
59 packet_t
*packet
= (packet_t
*) buf
;
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
));
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");
83 task_done (task_netcore
);
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
);
100 /* check for incoming data */
101 int ret
= netdev_rx (netif
->dev
, buf_rx
, 2048);
103 /* are there some data ? */
105 net_packet_handler (buf_rx
, ret
);
108 memset (buf_rx
, 0, 2048);
110 netif
->dev
->info_rx
+= ret
;
119 task_done (task_netcore
);
126 unsigned init_packet ()
128 if (!init_net_proto_tcp ())
131 /* start new task/thread for network core */
132 task_netcore
= (task_t
*) task_create ("netcore", (unsigned) task_net_packet
, 255);