Fix typos.
[glibc/history.git] / inet / inet6_opt.c
blobbddb85182b3de154f99e6debae1b2149c12b4bb2
1 /* Copyright (C) 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <string.h>
21 #include <netinet/in.h>
22 #include <netinet/ip6.h>
25 /* RFC 3542, 10.1
27 This function returns the number of bytes needed for the empty
28 extension header i.e., without any options. If EXTBUF is not NULL it
29 also initializes the extension header to have the correct length
30 field. In that case if the EXTLEN value is not a positive (i.e.,
31 non-zero) multiple of 8 the function fails and returns -1. */
32 int
33 inet6_opt_init (void *extbuf, socklen_t extlen)
35 if (extbuf != NULL)
37 if (extlen <= 0 || (extlen % 8) != 0)
38 return -1;
40 /* Fill in the length in units of 8 octets. */
41 struct ip6_hbh *extp = (struct ip6_hbh *) extbuf;
42 extp->ip6h_len = extlen / 8;
45 return sizeof (struct ip6_hbh);
49 static void
50 add_padding (uint8_t *extbuf, int offset, int npad)
52 if (npad == 1)
53 extbuf[offset] = IP6OPT_PAD1;
54 else
56 struct ip6_opt *pad_opt = (struct ip6_opt *) (extbuf + offset);
58 pad_opt->ip6o_type = IP6OPT_PADN;
59 pad_opt->ip6o_len = npad - sizeof (struct ip6_opt);
60 /* Clear the memory used by the padding. */
61 memset (pad_opt + 1, '\0', pad_opt->ip6o_len);
67 /* RFC 3542, 10.2
69 This function returns the updated total length taking into account
70 adding an option with length 'len' and alignment 'align'. If
71 EXTBUF is not NULL then, in addition to returning the length, the
72 function inserts any needed pad option, initializes the option
73 (setting the type and length fields) and returns a pointer to the
74 location for the option content in databufp. If the option does
75 not fit in the extension header buffer the function returns -1. */
76 int
77 inet6_opt_append (void *extbuf, socklen_t extlen, int offset, uint8_t type,
78 socklen_t len, uint8_t align, void **databufp)
80 /* Check minimum offset. */
81 if (offset < sizeof (struct ip6_hbh))
82 return -1;
84 /* One cannot add padding options. */
85 if (type == IP6OPT_PAD1 || type == IP6OPT_PADN)
86 return -1;
88 /* The option length must fit in one octet. */
89 if (len > 255)
90 return -1;
92 /* The alignment can only by 1, 2, 4, or 8 and must not exceed the
93 option length. */
94 if (align == 0 || align > 8 || (align & (align - 1)) != 0 || align > len)
95 return -1;
97 /* Determine the needed padding for alignment. Following the
98 current content of the buffer we have the is the IPv6 option type
99 and length, followed immediately by the data. The data has the
100 alignment constraints. Therefore padding must be inserted in the
101 form of padding options before the new option. */
102 int data_offset = offset + sizeof (struct ip6_opt);
103 int npad = (align - data_offset % align) & (align - 1);
105 /* Now we can check whether the buffer is large enough. */
106 if (data_offset + npad + len > extlen)
107 return -1;
109 if (npad != 0)
111 if (extbuf != NULL)
112 add_padding (extbuf, offset, npad);
114 offset += npad;
117 /* Now prepare the option itself. */
118 if (extbuf != NULL)
120 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
122 opt->ip6o_type = type;
123 opt->ip6o_len = len;
125 *databufp = opt + 1;
128 return offset + sizeof (struct ip6_opt) + len;
132 /* RFC 3542, 10.3
134 This function returns the updated total length taking into account
135 the final padding of the extension header to make it a multiple of
136 8 bytes. If EXTBUF is not NULL the function also initializes the
137 option by inserting a Pad1 or PadN option of the proper length. */
139 inet6_opt_finish (void *extbuf, socklen_t extlen, int offset)
141 /* Check minimum offset. */
142 if (offset < sizeof (struct ip6_hbh))
143 return -1;
145 /* Required padding at the end. */
146 int npad = (8 - (offset & 7)) & 7;
148 /* Make sure the buffer is large enough. */
149 if (offset + npad > extlen)
150 return -1;
152 if (extbuf != NULL)
153 add_padding (extbuf, offset, npad);
155 return offset + npad;
159 /* RFC 3542, 10.4
161 This function inserts data items of various sizes in the data
162 portion of the option. VAL should point to the data to be
163 inserted. OFFSET specifies where in the data portion of the option
164 the value should be inserted; the first byte after the option type
165 and length is accessed by specifying an offset of zero. */
167 inet6_opt_set_val (void *databuf, int offset, void *val, socklen_t vallen)
169 memcpy ((uint8_t *) databuf + offset, val, vallen);
171 return offset + vallen;
175 /* RFC 3542, 10.5
177 This function parses received option extension headers returning
178 the next option. EXTBUF and EXTLEN specifies the extension header.
179 OFFSET should either be zero (for the first option) or the length
180 returned by a previous call to 'inet6_opt_next' or
181 'inet6_opt_find'. It specifies the position where to continue
182 scanning the extension buffer. */
184 inet6_opt_next (void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
185 socklen_t *lenp, void **databufp)
187 if (offset == 0)
188 offset = sizeof (struct ip6_hbh);
189 else if (offset < sizeof (struct ip6_hbh))
190 return -1;
192 while (offset < extlen)
194 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
196 if (opt->ip6o_type == IP6OPT_PAD1)
197 /* Single byte padding. */
198 ++offset;
199 else if (opt->ip6o_type == IP6OPT_PADN)
200 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
201 else
203 /* Check whether the option is valid. */
204 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
205 if (offset > extlen)
206 return -1;
208 *typep = opt->ip6o_type;
209 *lenp = opt->ip6o_len;
210 *databufp = opt + 1;
211 return offset;
215 return -1;
219 /* RFC 3542, 10.6
221 This function is similar to the previously described
222 'inet6_opt_next' function, except this function lets the caller
223 specify the option type to be searched for, instead of always
224 returning the next option in the extension header. */
226 inet6_opt_find (void *extbuf, socklen_t extlen, int offset, uint8_t type,
227 socklen_t *lenp, void **databufp)
229 if (offset == 0)
230 offset = sizeof (struct ip6_hbh);
231 else if (offset < sizeof (struct ip6_hbh))
232 return -1;
234 while (offset < extlen)
236 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
238 if (opt->ip6o_type == IP6OPT_PAD1)
240 /* Single byte padding. */
241 ++offset;
242 if (type == IP6OPT_PAD1)
244 *lenp = 0;
245 *databufp = (uint8_t *) extbuf + offset;
246 return offset;
249 else if (opt->ip6o_type != type)
250 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
251 else
253 /* Check whether the option is valid. */
254 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
255 if (offset > extlen)
256 return -1;
258 *lenp = opt->ip6o_len;
259 *databufp = opt + 1;
260 return offset;
264 return -1;
268 /* RFC 3542, 10.7
270 This function extracts data items of various sizes in the data
271 portion of the option. */
273 inet6_opt_get_val (void *databuf, int offset, void *val, socklen_t vallen)
275 memcpy (val, (uint8_t *) databuf + offset, vallen);
277 return offset + vallen;