/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2010 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 . */ #include #include #include #include VasEBoot_err_t VasEBoot_netbuff_put (struct VasEBoot_net_buff *nb, VasEBoot_size_t len) { nb->tail += len; if (nb->tail > nb->end) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "put out of the packet range."); return VAS_EBOOT_ERR_NONE; } VasEBoot_err_t VasEBoot_netbuff_unput (struct VasEBoot_net_buff *nb, VasEBoot_size_t len) { nb->tail -= len; if (nb->tail < nb->head) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "unput out of the packet range."); return VAS_EBOOT_ERR_NONE; } VasEBoot_err_t VasEBoot_netbuff_push (struct VasEBoot_net_buff *nb, VasEBoot_size_t len) { nb->data -= len; if (nb->data < nb->head) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "push out of the packet range."); return VAS_EBOOT_ERR_NONE; } VasEBoot_err_t VasEBoot_netbuff_pull (struct VasEBoot_net_buff *nb, VasEBoot_size_t len) { nb->data += len; if (nb->data > nb->end || nb->data > nb->tail) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "pull out of the packet range."); return VAS_EBOOT_ERR_NONE; } VasEBoot_err_t VasEBoot_netbuff_reserve (struct VasEBoot_net_buff *nb, VasEBoot_size_t len) { nb->data += len; nb->tail += len; if ((nb->tail > nb->end) || (nb->data > nb->end)) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "reserve out of the packet range."); return VAS_EBOOT_ERR_NONE; } struct VasEBoot_net_buff * VasEBoot_netbuff_alloc (VasEBoot_size_t len) { struct VasEBoot_net_buff *nb; void *data; COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (VasEBoot_properly_aligned_t) == 0); /* * The largest size of a TCP packet is 64 KiB, and everything else * should be a lot smaller - most MTUs are 1500 or less. Cap data * size at 64 KiB + a buffer. */ if (len > 0xffffUL + 0x1000UL) { VasEBoot_error (VAS_EBOOT_ERR_BUG, "attempted to allocate a packet that is too big"); return NULL; } if (len < NETBUFFMINLEN) len = NETBUFFMINLEN; len = ALIGN_UP (len, NETBUFF_ALIGN); #ifdef VAS_EBOOT_MACHINE_EMU data = VasEBoot_malloc (len + sizeof (*nb)); #else data = VasEBoot_memalign (NETBUFF_ALIGN, len + sizeof (*nb)); #endif if (!data) return NULL; nb = (struct VasEBoot_net_buff *) ((VasEBoot_properly_aligned_t *) data + len / sizeof (VasEBoot_properly_aligned_t)); nb->head = nb->data = nb->tail = data; nb->end = (VasEBoot_uint8_t *) nb; return nb; } struct VasEBoot_net_buff * VasEBoot_netbuff_make_pkt (VasEBoot_size_t len) { struct VasEBoot_net_buff *nb; VasEBoot_err_t err; nb = VasEBoot_netbuff_alloc (len + 512); if (!nb) return NULL; err = VasEBoot_netbuff_reserve (nb, len + 512); if (err) goto fail; err = VasEBoot_netbuff_push (nb, len); if (err) goto fail; return nb; fail: VasEBoot_netbuff_free (nb); return NULL; } void VasEBoot_netbuff_free (struct VasEBoot_net_buff *nb) { if (!nb) return; VasEBoot_free (nb->head); } VasEBoot_err_t VasEBoot_netbuff_clear (struct VasEBoot_net_buff *nb) { nb->data = nb->tail = nb->head; return VAS_EBOOT_ERR_NONE; }