next part of ipv6-ization. dnsd code is "interesting"...

This commit is contained in:
Denis Vlasenko 2007-01-12 14:57:37 +00:00
parent 6536a9b583
commit 2c91652bbc
3 changed files with 53 additions and 108 deletions

View File

@ -19,15 +19,15 @@
#include "busybox.h" #include "busybox.h"
static char *fileconf = "/etc/dnsd.conf"; static const char *fileconf = "/etc/dnsd.conf";
#define LOCK_FILE "/var/run/dnsd.lock" #define LOCK_FILE "/var/run/dnsd.lock"
#define LOG_FILE "/var/log/dnsd.log"
// Must matct getopt32 call // Must match getopt32 call
#define OPT_daemon (option_mask32 & 0x10) #define OPT_daemon (option_mask32 & 0x10)
#define OPT_verbose (option_mask32 & 0x20) #define OPT_verbose (option_mask32 & 0x20)
//#define DEBUG 1 //#define DEBUG 1
#define DEBUG 0
enum { enum {
MAX_HOST_LEN = 16, // longest host name allowed is 15 MAX_HOST_LEN = 16, // longest host name allowed is 15
@ -76,8 +76,6 @@ struct dns_entry { // element of known name, ip address and reversed ip address
}; };
static struct dns_entry *dnsentry = NULL; static struct dns_entry *dnsentry = NULL;
// FIXME! unused! :(
static int daemonmode;
static uint32_t ttl = DEFAULT_TTL; static uint32_t ttl = DEFAULT_TTL;
/* /*
@ -108,21 +106,6 @@ static void undot(uint8_t * rip)
} }
} }
/*
* Append message to log file
*/
static void log_message(char *filename, char *message)
{
FILE *logfile;
if (!daemonmode)
return;
logfile = fopen(filename, "a");
if (!logfile)
return;
fprintf(logfile, "%s\n", message);
fclose(logfile);
}
/* /*
* Read one line of hostname/IP from file * Read one line of hostname/IP from file
* Returns 0 for each valid entry read, -1 at EOF * Returns 0 for each valid entry read, -1 at EOF
@ -191,31 +174,6 @@ static void dnsentryinit(void)
fclose(fp); fclose(fp);
} }
/*
* Set up UDP socket
*/
static int listen_socket(char *iface_addr, int listen_port)
{
struct sockaddr_in a;
char msg[100];
int sck;
sck = xsocket(PF_INET, SOCK_DGRAM, 0);
if (setsockopt_reuseaddr(sck) < 0)
bb_perror_msg_and_die("setsockopt() failed");
memset(&a, 0, sizeof(a));
a.sin_port = htons(listen_port);
a.sin_family = AF_INET;
if (!inet_aton(iface_addr, &a.sin_addr))
bb_perror_msg_and_die("bad iface address");
xbind(sck, (struct sockaddr *)&a, sizeof(a));
xlisten(sck, 50);
sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
iface_addr, (int)listen_port);
log_message(LOG_FILE, msg);
return sck;
}
/* /*
* Look query up in dns records and return answer if found * Look query up in dns records and return answer if found
* qs is the query string, first byte the string length * qs is the query string, first byte the string length
@ -309,7 +267,7 @@ static int process_packet(uint8_t * buf)
goto empty_packet; goto empty_packet;
// We have a standard query // We have a standard query
log_message(LOG_FILE, (char *)from); bb_info_msg("%s", (char *)from);
lookup_result = table_lookup(type, answstr, (uint8_t*)from); lookup_result = table_lookup(type, answstr, (uint8_t*)from);
if (lookup_result != 0) { if (lookup_result != 0) {
outr.flags = 3 | 0x0400; //name do not exist and auth outr.flags = 3 | 0x0400; //name do not exist and auth
@ -363,27 +321,26 @@ static int process_packet(uint8_t * buf)
static void interrupt(int x) static void interrupt(int x)
{ {
unlink(LOCK_FILE); unlink(LOCK_FILE);
write(2, "interrupt exiting\n", 18); bb_error_msg("interrupt, exiting\n");
exit(2); exit(2);
} }
int dnsd_main(int argc, char **argv) int dnsd_main(int argc, char **argv)
{ {
char *listen_interface = NULL;
char *sttl, *sport;
len_and_sockaddr *lsa;
int udps; int udps;
uint16_t port = 53; uint16_t port = 53;
uint8_t buf[MAX_PACK_LEN]; uint8_t buf[MAX_PACK_LEN];
char *listen_interface = "0.0.0.0";
char *sttl, *sport;
getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
//if (option_mask32 & 0x1) // -i //if (option_mask32 & 0x1) // -i
//if (option_mask32 & 0x2) // -c //if (option_mask32 & 0x2) // -c
if (option_mask32 & 0x4) // -t if (option_mask32 & 0x4) // -t
if (!(ttl = atol(sttl))) ttl = xatou_range(sttl, 1, 0xffffffff);
bb_show_usage();
if (option_mask32 & 0x8) // -p if (option_mask32 & 0x8) // -p
if (!(port = atol(sport))) port = xatou_range(sttl, 1, 0xffff);
bb_show_usage();
if (OPT_verbose) { if (OPT_verbose) {
bb_info_msg("listen_interface: %s", listen_interface); bb_info_msg("listen_interface: %s", listen_interface);
@ -391,13 +348,16 @@ int dnsd_main(int argc, char **argv)
bb_info_msg("fileconf: %s", fileconf); bb_info_msg("fileconf: %s", fileconf);
} }
if (OPT_daemon) if (OPT_daemon) {
//FIXME: NOMMU will NOT set LOGMODE_SYSLOG!
#ifdef BB_NOMMU #ifdef BB_NOMMU
/* reexec for vfork() do continue parent */ /* reexec for vfork() do continue parent */
vfork_daemon_rexec(1, 0, argc, argv, "-d"); vfork_daemon_rexec(1, 0, argc, argv, "-d");
#else #else
xdaemon(1, 0); xdaemon(1, 0);
#endif #endif
logmode = LOGMODE_SYSLOG;
}
dnsentryinit(); dnsentryinit();
@ -411,7 +371,12 @@ int dnsd_main(int argc, char **argv)
signal(SIGURG, SIG_IGN); signal(SIGURG, SIG_IGN);
#endif #endif
udps = listen_socket(listen_interface, port); lsa = host2sockaddr(listen_interface, port);
udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
xbind(udps, &lsa->sa, lsa->len);
// xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think?
bb_info_msg("Accepting UDP packets on %s",
xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
while (1) { while (1) {
fd_set fdset; fd_set fdset;
@ -420,6 +385,8 @@ int dnsd_main(int argc, char **argv)
FD_ZERO(&fdset); FD_ZERO(&fdset);
FD_SET(udps, &fdset); FD_SET(udps, &fdset);
// Block until a message arrives // Block until a message arrives
// FIXME: Fantastic. select'ing on just one fd??
// Why no just block on it doing recvfrom() ?
r = select(udps + 1, &fdset, NULL, NULL, NULL); r = select(udps + 1, &fdset, NULL, NULL, NULL);
if (r < 0) if (r < 0)
bb_perror_msg_and_die("select error"); bb_perror_msg_and_die("select error");
@ -428,27 +395,26 @@ int dnsd_main(int argc, char **argv)
/* Can this test ever be false? - yes */ /* Can this test ever be false? - yes */
if (FD_ISSET(udps, &fdset)) { if (FD_ISSET(udps, &fdset)) {
struct sockaddr_in from; socklen_t fromlen = lsa->len;
int fromlen = sizeof(from); // FIXME: need to get *DEST* address (to which of our addresses
r = recvfrom(udps, buf, sizeof(buf), 0, // this query was directed), and reply from the same address.
(struct sockaddr *)&from, // Or else we can exhibit usual UDP ugliness:
(void *)&fromlen); // [ip1.multihomed.ip2] <= query to ip1 <= peer
// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
if (OPT_verbose) if (OPT_verbose)
fprintf(stderr, "\n--- Got UDP "); bb_info_msg("Got UDP packet");
log_message(LOG_FILE, "\n--- Got UDP ");
if (r < 12 || r > 512) { if (r < 12 || r > 512) {
bb_error_msg("invalid packet size"); bb_error_msg("invalid packet size");
continue; continue;
} }
if (r > 0) { if (r <= 0)
r = process_packet(buf); continue;
if (r > 0) r = process_packet(buf);
sendto(udps, buf, if (r <= 0)
r, 0, (struct sockaddr *)&from, continue;
fromlen); sendto(udps, buf, r, 0, &lsa->sa, fromlen);
} }
} // end if }
} // end while
return 0;
} }

View File

@ -36,12 +36,6 @@
#define BUFSIZE 4000 #define BUFSIZE 4000
#if ENABLE_FEATURE_IPV6
typedef struct sockaddr_in6 sockaddr_type;
#else
typedef struct sockaddr_in sockaddr_type;
#endif
#if ENABLE_LOGIN #if ENABLE_LOGIN
static const char *loginpath = "/bin/login"; static const char *loginpath = "/bin/login";
#else #else
@ -462,13 +456,10 @@ telnetd_main(int argc, char **argv)
#if ENABLE_FEATURE_TELNETD_STANDALONE #if ENABLE_FEATURE_TELNETD_STANDALONE
/* First check for and accept new sessions. */ /* First check for and accept new sessions. */
if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) { if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
sockaddr_type sa;
int fd; int fd;
socklen_t salen;
struct tsession *new_ts; struct tsession *new_ts;
salen = sizeof(sa); fd = accept(master_fd, NULL, 0);
fd = accept(master_fd, (struct sockaddr *)&sa, &salen);
if (fd < 0) if (fd < 0)
goto again; goto again;
/* Create a new session and link it into our active list */ /* Create a new session and link it into our active list */

View File

@ -240,10 +240,8 @@ static int tftp(
} }
} }
/* send packet */ /* send packet */
timeout = TFTP_NUM_RETRIES; /* re-initialize */ timeout = TFTP_NUM_RETRIES; /* re-initialize */
do { do {
len = cp - xbuf; len = cp - xbuf;
@ -260,13 +258,12 @@ static int tftp(
break; break;
} }
if (finished && (opcode == TFTP_ACK)) { if (finished && (opcode == TFTP_ACK)) {
break; break;
} }
/* receive packet */ /* receive packet */
recv_again:
tv.tv_sec = TFTP_TIMEOUT; tv.tv_sec = TFTP_TIMEOUT;
tv.tv_usec = 0; tv.tv_usec = 0;
@ -288,25 +285,16 @@ static int tftp(
bb_perror_msg("recvfrom"); bb_perror_msg("recvfrom");
break; break;
} }
timeout = 0;
if (from->sa_family == peer_lsa->sa.sa_family) {
#if ENABLE_FEATURE_IPV6 #if ENABLE_FEATURE_IPV6
if (from->sa_family == AF_INET6 if (from->sa_family == AF_INET6)
&& ((struct sockaddr_in6*)from)->sin6_port == port if (((struct sockaddr_in6*)from)->sin6_port != port)
) goto recv_again;
break;
#endif #endif
/* Non-internet sockets are ok */ if (from->sa_family == AF_INET)
if (from->sa_family != AF_INET) if (((struct sockaddr_in*)from)->sin_port != port)
break; goto recv_again;
if (((struct sockaddr_in*)from)->sin_port == port) timeout = 0;
break; break;
}
/* family doesn't match, or
* it is INET[v6] and port doesn't match -
* fall-through for bad packets!
* (discard the packet - treat as timeout) */
timeout = TFTP_NUM_RETRIES;
case 0: case 0:
bb_error_msg("timeout"); bb_error_msg("timeout");
timeout--; timeout--;
@ -436,11 +424,11 @@ static int tftp(
} }
} }
#if ENABLE_FEATURE_CLEAN_UP if (ENABLE_FEATURE_CLEAN_UP) {
close(socketfd); close(socketfd);
free(xbuf); free(xbuf);
free(rbuf); free(rbuf);
#endif }
return finished ? EXIT_SUCCESS : EXIT_FAILURE; return finished ? EXIT_SUCCESS : EXIT_FAILURE;
} }