1*65b390aaSDag-Erling Smørgrav /*
2*65b390aaSDag-Erling Smørgrav * edns-subnet/edns-subnet.c - Subnet option related constants
3*65b390aaSDag-Erling Smørgrav *
4*65b390aaSDag-Erling Smørgrav * Copyright (c) 2013, NLnet Labs. All rights reserved.
5*65b390aaSDag-Erling Smørgrav *
6*65b390aaSDag-Erling Smørgrav * This software is open source.
7*65b390aaSDag-Erling Smørgrav *
8*65b390aaSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
9*65b390aaSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
10*65b390aaSDag-Erling Smørgrav * are met:
11*65b390aaSDag-Erling Smørgrav *
12*65b390aaSDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice,
13*65b390aaSDag-Erling Smørgrav * this list of conditions and the following disclaimer.
14*65b390aaSDag-Erling Smørgrav *
15*65b390aaSDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice,
16*65b390aaSDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation
17*65b390aaSDag-Erling Smørgrav * and/or other materials provided with the distribution.
18*65b390aaSDag-Erling Smørgrav *
19*65b390aaSDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may
20*65b390aaSDag-Erling Smørgrav * be used to endorse or promote products derived from this software without
21*65b390aaSDag-Erling Smørgrav * specific prior written permission.
22*65b390aaSDag-Erling Smørgrav *
23*65b390aaSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*65b390aaSDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*65b390aaSDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*65b390aaSDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*65b390aaSDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*65b390aaSDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*65b390aaSDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*65b390aaSDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*65b390aaSDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*65b390aaSDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*65b390aaSDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*65b390aaSDag-Erling Smørgrav */
35*65b390aaSDag-Erling Smørgrav /**
36*65b390aaSDag-Erling Smørgrav * \file
37*65b390aaSDag-Erling Smørgrav * Subnet option related constants.
38*65b390aaSDag-Erling Smørgrav */
39*65b390aaSDag-Erling Smørgrav
40*65b390aaSDag-Erling Smørgrav #include "config.h"
41*65b390aaSDag-Erling Smørgrav
42*65b390aaSDag-Erling Smørgrav #ifdef CLIENT_SUBNET /* keeps splint happy */
43*65b390aaSDag-Erling Smørgrav #include "edns-subnet/edns-subnet.h"
44*65b390aaSDag-Erling Smørgrav #include <string.h>
45*65b390aaSDag-Erling Smørgrav
46*65b390aaSDag-Erling Smørgrav int
copy_clear(uint8_t * dst,size_t dstlen,uint8_t * src,size_t srclen,size_t n)47*65b390aaSDag-Erling Smørgrav copy_clear(uint8_t* dst, size_t dstlen, uint8_t* src, size_t srclen, size_t n)
48*65b390aaSDag-Erling Smørgrav {
49*65b390aaSDag-Erling Smørgrav size_t intpart = n / 8; /* bytes */
50*65b390aaSDag-Erling Smørgrav size_t fracpart = n % 8; /* bits */
51*65b390aaSDag-Erling Smørgrav size_t written = intpart;
52*65b390aaSDag-Erling Smørgrav if (intpart > dstlen || intpart > srclen)
53*65b390aaSDag-Erling Smørgrav return 1;
54*65b390aaSDag-Erling Smørgrav if (fracpart && (intpart+1 > dstlen || intpart+1 > srclen))
55*65b390aaSDag-Erling Smørgrav return 1;
56*65b390aaSDag-Erling Smørgrav memcpy(dst, src, intpart);
57*65b390aaSDag-Erling Smørgrav if (fracpart) {
58*65b390aaSDag-Erling Smørgrav dst[intpart] = src[intpart] & ~(0xFF >> fracpart);
59*65b390aaSDag-Erling Smørgrav written++;
60*65b390aaSDag-Erling Smørgrav }
61*65b390aaSDag-Erling Smørgrav memset(dst + written, 0, dstlen - written);
62*65b390aaSDag-Erling Smørgrav return 0;
63*65b390aaSDag-Erling Smørgrav }
64*65b390aaSDag-Erling Smørgrav
65*65b390aaSDag-Erling Smørgrav #endif /* CLIENT_SUBNET */
66