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