context.c revision e61793f0865117ad87a19d6e245bea8f3b712d1b
2a3747006563cfa1c07516ec594cc6d1f0db7ff2Tinderbox User * Copyright (C) 2000, 2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: context.c,v 1.42 2001/11/19 03:08:40 mayer Exp $ */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Some systems define the socket length argument as an int, some as size_t,
b20ee662a7c847c9ef7b96ab9e5e34543efe5c0dMark Andrews * some as socklen_t. The last is what the current POSIX standard mandates.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * This definition is here so it can be portable but easily changed if needed.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#define LWRES_SOCKADDR_LEN_T unsigned int
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * Make a socket nonblocking.
1ccbfca64ae86ace521053773001cb995352f96fBob HalleyLIBLWRES_EXTERNAL_DATA lwres_uint16_t lwres_udp_port = LWRES_UDP_PORT;
1ccbfca64ae86ace521053773001cb995352f96fBob HalleyLIBLWRES_EXTERNAL_DATA const char *lwres_resolv_conf = LWRES_RESOLV_CONF;
1ccbfca64ae86ace521053773001cb995352f96fBob Halleystatic void *
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonlwres_context_create(lwres_context_t **contextp, void *arg,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(contextp != NULL && *contextp == NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * If we were not given anything special to use, use our own
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * functions. These are just wrappers around malloc() and free().
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (malloc_function == NULL || free_function == NULL) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ctx = malloc_function(arg, sizeof(lwres_context_t));
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * Set up the context.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleylwres_context_destroy(lwres_context_t **contextp) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonlwres_context_nextserial(lwres_context_t *ctx) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halleylwres_context_initserial(lwres_context_t *ctx, lwres_uint32_t serial) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleylwres_context_freemem(lwres_context_t *ctx, void *mem, size_t len) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halleylwres_context_allocmem(lwres_context_t *ctx, size_t len) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleystatic void *
1ccbfca64ae86ace521053773001cb995352f96fBob Halley memcpy(&ctx->address, &ctx->confdata.lwservers[0],
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /* The default is the IPv4 loopback address 127.0.0.1. */
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews if (ctx->address.family == LWRES_ADDRTYPE_V4) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence } else if (ctx->address.family == LWRES_ADDRTYPE_V6) {
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington ret = sendto(ctx->sock, sendbase, sendlen, 0, NULL, 0);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * The address of fromlen is cast to void * to shut up compiler
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * warnings, namely on systems that have the sixth parameter
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * prototyped as a signed int when LWRES_SOCKADDR_LEN_T is
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * defined as unsigned.
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley ret = recvfrom(ctx->sock, recvbase, recvlen, 0, sa, (void *)&fromlen);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * If we got something other than what we expect, have the caller
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * wait for another packet. This can happen if an old result
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * comes in, or if someone is sending us random stuff.
7a00d69909ace5dc11bcff9c1e07c311f92a7f8eWitold Krecicki || memcmp(&sin.sin_addr, ctx->address.address,
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * Type of tv_sec is long, so make sure the unsigned long timeout
0deebcd15ad440c7ecaaa77f8e06232b331aae79Mark Andrews * does not overflow it.
86529bb6979dbe3ffd7bc2df078fac21cb339953Mark Andrews ret2 = select(ctx->sock + 1, &readfds, NULL, NULL, &timeout);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * What happened with select?