Fix a bunch of net issues
This commit is contained in:
parent
ce3a2ec025
commit
04d22dddd9
9 changed files with 181 additions and 196 deletions
|
@ -1,68 +1,92 @@
|
|||
|
||||
#include <grub/dl.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/ethernet.h> /* the L2 protocols */
|
||||
#include <grub/net.h>
|
||||
#include <sys/types.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <grub/term.h>
|
||||
|
||||
static int fd;
|
||||
|
||||
static grub_err_t
|
||||
card_open (struct grub_net_card *dev)
|
||||
{
|
||||
dev->data_num = socket (AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
||||
if (dev->data_num < 0)
|
||||
return grub_error (GRUB_ERR_IO, "couldn't open packet interface");
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
card_close (struct grub_net_card *dev)
|
||||
{
|
||||
close (dev->data_num);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
send_card_buffer (struct grub_net_card *dev, struct grub_net_buff *pack)
|
||||
send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
|
||||
struct grub_net_buff *pack)
|
||||
{
|
||||
ssize_t actual;
|
||||
|
||||
actual = write (dev->data_num, pack->data, pack->tail - pack->data);
|
||||
actual = write (fd, pack->data, pack->tail - pack->data);
|
||||
if (actual < 0)
|
||||
return grub_error (GRUB_ERR_IO, "couldn't send packets");
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
get_card_packet (struct grub_net_card *dev, struct grub_net_buff *pack)
|
||||
static grub_size_t
|
||||
get_card_packet (struct grub_net_card *dev __attribute__ ((unused)),
|
||||
struct grub_net_buff *pack)
|
||||
{
|
||||
ssize_t actual;
|
||||
|
||||
grub_netbuff_clear(pack);
|
||||
actual = read (dev->data_num, pack->data, 1500);
|
||||
actual = read (fd, pack->data, 1500);
|
||||
if (actual < 0)
|
||||
return grub_error (GRUB_ERR_IO, "couldn't receive packets");
|
||||
{
|
||||
grub_error (GRUB_ERR_IO, "couldn't receive packets");
|
||||
return -1;
|
||||
}
|
||||
grub_netbuff_put (pack, actual);
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
return actual;
|
||||
}
|
||||
|
||||
static struct grub_net_card_driver emudriver =
|
||||
{
|
||||
.name = "emu",
|
||||
.init = card_open,
|
||||
.fini = card_close,
|
||||
.send = send_card_buffer,
|
||||
.recv = get_card_packet
|
||||
};
|
||||
|
||||
static struct grub_net_card emucard =
|
||||
{
|
||||
.name = "emu0",
|
||||
.driver = &emudriver,
|
||||
.default_address = {
|
||||
.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
|
||||
{ .mac = { 0, 1, 2, 3, 4, 5} }
|
||||
},
|
||||
.flags = 0
|
||||
};
|
||||
|
||||
GRUB_MOD_INIT(emunet)
|
||||
{
|
||||
grub_net_card_driver_register (&emudriver);
|
||||
struct ifreq ifr;
|
||||
// char fullname[64];
|
||||
fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK);
|
||||
if (fd < 0)
|
||||
return;
|
||||
grub_memset (&ifr, 0, sizeof (ifr));
|
||||
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
||||
if (ioctl (fd, TUNSETIFF, &ifr) < 0)
|
||||
{
|
||||
close (fd);
|
||||
fd = -1;
|
||||
return;
|
||||
}
|
||||
grub_net_card_register (&emucard);
|
||||
}
|
||||
|
||||
GRUB_MODE_FINI(emunet)
|
||||
GRUB_MOD_FINI(emunet)
|
||||
{
|
||||
grub_net_card_driver_unregister (&emudriver);
|
||||
if (fd >= 0)
|
||||
{
|
||||
close (fd);
|
||||
grub_net_card_unregister (&emucard);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue