source: grub-pc/trunk/fuentes/grub-core/net/netbuff.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 3.2 KB
Line 
1/*
2 *  GRUB  --  GRand Unified Bootloader
3 *  Copyright (C) 2010 Free Software Foundation, Inc.
4 *
5 *  GRUB 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.
9 *
10 *  GRUB 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.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <grub/err.h>
20#include <grub/misc.h>
21#include <grub/mm.h>
22#include <grub/net/netbuff.h>
23
24grub_err_t
25grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
26{
27  nb->tail += len;
28  if (nb->tail > nb->end)
29    return grub_error (GRUB_ERR_BUG, "put out of the packet range.");
30  return GRUB_ERR_NONE;
31}
32
33grub_err_t
34grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
35{
36  nb->tail -= len;
37  if (nb->tail < nb->head)
38    return grub_error (GRUB_ERR_BUG,
39                       "unput out of the packet range.");
40  return GRUB_ERR_NONE;
41}
42
43grub_err_t
44grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
45{
46  nb->data -= len;
47  if (nb->data < nb->head)
48    return grub_error (GRUB_ERR_BUG,
49                       "push out of the packet range.");
50  return GRUB_ERR_NONE;
51}
52
53grub_err_t
54grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
55{
56  nb->data += len;
57  if (nb->data > nb->end)
58    return grub_error (GRUB_ERR_BUG,
59                       "pull out of the packet range.");
60  return GRUB_ERR_NONE;
61}
62
63grub_err_t
64grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
65{
66  nb->data += len;
67  nb->tail += len;
68  if ((nb->tail > nb->end) || (nb->data > nb->end))
69    return grub_error (GRUB_ERR_BUG,
70                       "reserve out of the packet range.");
71  return GRUB_ERR_NONE;
72}
73
74struct grub_net_buff *
75grub_netbuff_alloc (grub_size_t len)
76{
77  struct grub_net_buff *nb;
78  void *data;
79
80  COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (grub_properly_aligned_t) == 0);
81
82  if (len < NETBUFFMINLEN)
83    len = NETBUFFMINLEN;
84
85  len = ALIGN_UP (len, NETBUFF_ALIGN);
86#ifdef GRUB_MACHINE_EMU
87  data = grub_malloc (len + sizeof (*nb));
88#else
89  data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
90#endif
91  if (!data)
92    return NULL;
93  nb = (struct grub_net_buff *) ((grub_properly_aligned_t *) data
94                                 + len / sizeof (grub_properly_aligned_t));
95  nb->head = nb->data = nb->tail = data;
96  nb->end = (grub_uint8_t *) nb;
97  return nb;
98}
99
100struct grub_net_buff *
101grub_netbuff_make_pkt (grub_size_t len)
102{
103  struct grub_net_buff *nb;
104  grub_err_t err;
105  nb = grub_netbuff_alloc (len + 512);
106  if (!nb)
107    return NULL;
108  err = grub_netbuff_reserve (nb, len + 512);
109  if (err)
110    goto fail;
111  err = grub_netbuff_push (nb, len);
112  if (err)
113    goto fail;
114  return nb;
115 fail:
116  grub_netbuff_free (nb);
117  return NULL;
118}
119
120void
121grub_netbuff_free (struct grub_net_buff *nb)
122{
123  if (!nb)
124    return;
125  grub_free (nb->head);
126}
127
128grub_err_t
129grub_netbuff_clear (struct grub_net_buff *nb)
130{
131  nb->data = nb->tail = nb->head;
132  return GRUB_ERR_NONE;
133}
Note: See TracBrowser for help on using the repository browser.