From b1d7bfb634991d99ed53bed8627cc1773f675312 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Sun, 4 Feb 2007 15:22:55 +0000 Subject: [PATCH] #4 added the common-files --- src/common.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/common.c create mode 100644 src/common.h diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..c6c8539 --- /dev/null +++ b/src/common.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2006 Bjorn Andersson , Erik Ekman + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#ifdef DARWIN +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" + +int +open_dns(int localport, in_addr_t listen_ip) +{ + struct sockaddr_in addr; + int flag; + int fd; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(localport); + /* listen_ip already in network byte order from inet_addr, or 0 */ + addr.sin_addr.s_addr = listen_ip; + + if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) + err(1, "socket"); + + flag = 1; +#ifdef SO_REUSEPORT + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag)); +#endif + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); + + if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) + err(1, "bind"); + + printf("Opened UDP socket\n"); + + return fd; +} + +void +close_dns(int fd) +{ + close(fd); +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..e28089e --- /dev/null +++ b/src/common.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2006 Bjorn Andersson , Erik Ekman + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __COMMON_H__ +#define __COMMON_H__ + +struct packet +{ + int len; + int offset; + char data[64*1024]; +}; + +struct query { + char name[258]; + short type; + short id; + struct sockaddr from; + int fromlen; +}; + +struct user { + int id; + struct sockaddr host; + int addrlen; +}; + +int open_dns(int, in_addr_t); +void close_dns(int); + +#endif