Split emunet into platform-dependent and GRUB-binding parts. Keep

platform-dependent part in kernel for easy access to OS functions.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-15 11:55:20 +02:00
parent 25ac643a54
commit 70671037c8
9 changed files with 220 additions and 92 deletions

View file

@ -16,107 +16,59 @@
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/net/netbuff.h>
#include <config.h>
#include <config-util.h>
#include <sys/socket.h>
#include <grub/net.h>
#include <sys/types.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <grub/term.h>
#include <grub/i18n.h>
#include <string.h>
GRUB_MOD_LICENSE ("GPLv3+");
#include <grub/emu/net.h>
static int fd;
static grub_err_t
send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
struct grub_net_buff *pack)
grub_ssize_t
grub_emunet_send (const void *packet, grub_size_t sz)
{
ssize_t actual;
actual = write (fd, pack->data, pack->tail - pack->data);
if (actual < 0)
return grub_error (GRUB_ERR_IO, N_("couldn't send network packet"));
return GRUB_ERR_NONE;
return write (fd, packet, sz);
}
static struct grub_net_buff *
get_card_packet (struct grub_net_card *dev __attribute__ ((unused)))
grub_ssize_t
grub_emunet_receive (void *packet, grub_size_t sz)
{
ssize_t actual;
struct grub_net_buff *nb;
nb = grub_netbuff_alloc (1536 + 2);
if (!nb)
return NULL;
/* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
by 4. So that IP header is aligned on 4 bytes. */
grub_netbuff_reserve (nb, 2);
if (!nb)
{
grub_netbuff_free (nb);
return NULL;
}
actual = read (fd, nb->data, 1536);
if (actual < 0)
{
grub_netbuff_free (nb);
return NULL;
}
grub_netbuff_put (nb, actual);
return nb;
return read (fd, packet, sz);
}
static struct grub_net_card_driver emudriver =
{
.name = "emu",
.send = send_card_buffer,
.recv = get_card_packet
};
static struct grub_net_card emucard =
{
.name = "emu0",
.driver = &emudriver,
.mtu = 1500,
.default_address = {
.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
{.mac = {0, 1, 2, 3, 4, 5}}
},
.flags = 0
};
GRUB_MOD_INIT(emunet)
int
grub_emunet_create (grub_size_t *mtu)
{
struct ifreq ifr;
*mtu = 1500;
fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK);
if (fd < 0)
return;
grub_memset (&ifr, 0, sizeof (ifr));
return -1;
memset (&ifr, 0, sizeof (ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (ioctl (fd, TUNSETIFF, &ifr) < 0)
{
close (fd);
fd = -1;
return;
return -1;
}
grub_net_card_register (&emucard);
return 0;
}
GRUB_MOD_FINI(emunet)
void
grub_emunet_close (void)
{
if (fd >= 0)
{
close (fd);
grub_net_card_unregister (&emucard);
}
if (fd < 0)
return;
close (fd);
fd = -1;
}