vaseboot/VasEBoot-core/net/drivers/emu/emunet.c

117 lines
3.0 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2010,2011,2012,2013 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/net/netbuff.h>
#include <VasEBoot/net.h>
#include <VasEBoot/term.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/emu/net.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static VasEBoot_err_t
send_card_buffer (struct VasEBoot_net_card *dev __attribute__ ((unused)),
struct VasEBoot_net_buff *pack);
static struct VasEBoot_net_buff *
get_card_packet (struct VasEBoot_net_card *dev __attribute__ ((unused)));
static struct VasEBoot_net_card_driver emudriver =
{
.name = "emu",
.send = send_card_buffer,
.recv = get_card_packet
};
static struct VasEBoot_net_card emucard =
{
.name = "emu0",
.driver = &emudriver,
.mtu = 1500,
.default_address = {
.type = VAS_EBOOT_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
{.mac = {0, 1, 2, 3, 4, 5}}
},
.flags = 0
};
static VasEBoot_err_t
send_card_buffer (struct VasEBoot_net_card *dev __attribute__ ((unused)),
struct VasEBoot_net_buff *pack)
{
VasEBoot_ssize_t actual;
actual = VasEBoot_emunet_send (pack->data, pack->tail - pack->data);
if (actual < 0)
return VasEBoot_error (VAS_EBOOT_ERR_IO, N_("couldn't send network packet"));
return VAS_EBOOT_ERR_NONE;
}
static struct VasEBoot_net_buff *
get_card_packet (struct VasEBoot_net_card *dev __attribute__ ((unused)))
{
VasEBoot_ssize_t actual;
struct VasEBoot_net_buff *nb;
nb = VasEBoot_netbuff_alloc (emucard.mtu + 36 + 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. */
VasEBoot_netbuff_reserve (nb, 2);
if (!nb)
{
VasEBoot_netbuff_free (nb);
return NULL;
}
actual = VasEBoot_emunet_receive (nb->data, emucard.mtu + 36);
if (actual < 0)
{
VasEBoot_netbuff_free (nb);
return NULL;
}
VasEBoot_netbuff_put (nb, actual);
return nb;
}
static int registered = 0;
VAS_EBOOT_MOD_INIT(emunet)
{
if (!VasEBoot_emunet_create (&emucard.mtu))
{
VasEBoot_net_card_register (&emucard);
registered = 1;
}
}
VAS_EBOOT_MOD_FINI(emunet)
{
if (registered)
{
VasEBoot_emunet_close ();
VasEBoot_net_card_unregister (&emucard);
registered = 0;
}
}