From: Johannes Maximilian Kuehn Date: Fri, 8 Aug 2008 22:28:37 +0000 (+0900) Subject: main.c: X-Git-Tag: NTP_4_2_5P142~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d3951fd811d56a27477e1c6dac5d7be7b808ebd;p=thirdparty%2Fntp.git main.c: bug fixes networking.c, kod_management.c: Bug fixes bk: 489cc895F5cfvB6n6GOS9y4Bz07vhw --- diff --git a/gsoc_sntp/kod_management.c b/gsoc_sntp/kod_management.c index 9b6d14452..8d3856e57 100644 --- a/gsoc_sntp/kod_management.c +++ b/gsoc_sntp/kod_management.c @@ -4,6 +4,7 @@ #include "kod_management.h" #include "log.h" #include "sntp-opts.h" +#define DEBUG int kod_init = 0, entryc = 0; char *kod_db_file; @@ -40,6 +41,16 @@ search_entry ( return resc; } +int +kod_entry_exists ( + char *search_str + ) +{ + struct kod_entry **dummy; + + return search_entry(search_str, dummy); +} + void add_entry ( char *hostname, @@ -107,8 +118,10 @@ write_kod_db ( char msg[80]; snprintf(msg, 80, "Can't open KOD db file %s for writing!", kod_db_file); - if(debug) - debug_msg(msg); + +#ifdef DEBUG + debug_msg(msg); +#endif log_msg(msg, 2); @@ -116,7 +129,7 @@ write_kod_db ( } for(a=0; ahostname, nptr->timestamp, nptr->type); + fprintf(db_s, "%s:%i:%s\n", nptr->hostname, nptr->timestamp, nptr->type); nptr = nptr->next; } @@ -132,10 +145,14 @@ kod_init_kod_db ( if(kod_init) return; +#ifdef DEBUG + printf("Initializing KOD DB...\n"); +#endif + register int a, b; /* Max. of 255 characters for hostname, 10 for timestamp, 4 for kisscode, 2 for format : and 1 for \n */ - char *fbuf = (char *) malloc(sizeof(char) * 272); + char fbuf[272]; char *obuf = fbuf; char error = 0; @@ -145,16 +162,22 @@ kod_init_kod_db ( if(db_file == NULL) { char msg[80]; - snprintf(msg, 80, "Cannot open KOD db file %s", db_file); + snprintf(msg, 80, "kod_init_kod_db(): Cannot open KOD db file %s", db_file); - if(debug) - debug_msg(msg); +#ifdef DEBUG + debug_msg(msg); + printf("%s\n", msg); +#endif log_msg(msg, 2); } + printf("Starting to read KOD file %s...\n", db_file); /* First let's see how many entries there are and check for right syntax */ - while(fgets(fbuf, 272, db_s) != 0) { + + int scan_value = 0; + while(!feof(db_s)) { + printf("%s", fgets(fbuf, 272, db_s)); int sepc = 0; for(a=0; a 0) kod_db[b-1].next = &kod_db[b]; } - } +#ifdef DEBUG + for(a=0; a +#include +#include #include +#include #include #include "networking.h" -#include "header.h" +/*#include "header.h"*/ #define NTP_PORT 123 -int ai_fam_templ; +#define DEBUG +int ai_fam_templ; volatile int debug; +/* tOptions sntpOptions; */ + int main ( - char **argv, - int argc + int argc, + char **argv ) { - return sntp_main(argv, argc); + return sntp_main(argc, argv); } int sntp_main ( - char **argv, - int argc + int argc, + char **argv ) { + char *kod_file, *log_file; register int c; +#ifdef DEBUG + printf("Starting %s with %i arguments...\n", argv[0], argc); +#endif + if (isc_net_probeipv6() != ISC_R_SUCCESS) { ai_fam_templ = AF_INET; +#ifdef DEBUG + printf("No ipv6 support available, forcing ipv4\n"); +#endif } - int optct = optionProcess( &sntpOptions, argc, argv ); +#ifndef NO_DISK +#endif + + int optct = optionProcess(&sntpOptions, argc, argv); argc -= optct; - argv += optct; + argv += optct; + + for(c=0; cai_addr))) { + int ow_ret = on_wire(resh[c]); + sync_data_suc = 1; } else { - /* Debug/Log output */ + struct kod_entry **reason; + int kodc = search_entry(resh[c], reason); + + printf("KoD package exists for %s, stopping any further communication.", inet_ntoa(resh[c]->ai_addr)); + } } return 0; } + +int +on_wire ( + struct addrinfo *host + ) +{ + SOCKET sock; + + struct pkt *x_pkt = (struct pkt *) malloc(sizeof(struct pkt)); + struct pkt *r_pkt = (struct pkt *) malloc(sizeof(struct pkt)); + + struct timeval tv_t1, tv_t2; + struct timezone tz; + l_fp t1, t4; + + int error = GETTIMEOFDAY(&tv_t1, &tz); + +#ifdef DEBUG + printf("tv_t1 sec: %i msec: %i\n", tv_t1.tv_sec, tv_t1.tv_usec); +#endif + + TVTOTS(&tv_t1, &(x_pkt->org)); + +/* Data from a valid timestamp */ +/* unsigned char *tptr = &(x_pkt->org); + + tptr[0] = 0xcc; + tptr[1] = 0x44; + tptr[2] = 0x87; + tptr[3] = 0x45;*/ + + x_pkt->stratum = 15; + x_pkt->ppoll = 8; + x_pkt->li_vn_mode = 0x1b; + + + + /*in_addr_t foo = inet_addr("127.0.0.1"); */ + create_socket(&sock, host->ai_addr); + + int rsock = sendpkt(sock, host->ai_addr, x_pkt, 48); + + int rpktl = recvpkt(sock, r_pkt, x_pkt); + + close_socket(sock); + + NTOHL_FP(&(r_pkt->rec), &(r_pkt->rec)); + TSTOTV(&(r_pkt->rec), &tv_t2); + + double time = 0.0; + + LFPTOD(&(r_pkt->rec), time); + + printf("\nrec: %d\n", time); + + + + return 0; +} + + + + + + + + + + + + + + + + diff --git a/gsoc_sntp/networking.c b/gsoc_sntp/networking.c index 4ba563fd1..4d971d083 100644 --- a/gsoc_sntp/networking.c +++ b/gsoc_sntp/networking.c @@ -5,8 +5,25 @@ #include "header.h" #include "log.h" +#ifdef DEBUG +#define WS80 do { \ + register int wsc; \ + for(wsc=0; wsc<80; wsc++) \ + printf("-"); \ + printf("\n"); \ +} while(0); +#endif + + -int resolve_hosts (char **hosts, int hostc, struct addrinfo **res) { + +int +resolve_hosts ( + char **hosts, + int hostc, + struct addrinfo **res + ) +{ register unsigned int a, b; unsigned int entryc = 0; @@ -30,11 +47,11 @@ int resolve_hosts (char **hosts, int hostc, struct addrinfo **res) { memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; + hints.ai_socktype = SOCK_DGRAM; /* hints.ai_protocol = IPPROTO_UDP; */ - error = getaddrinfo(hosts[a], "ntp", &hints, tres[a]); + error = getaddrinfo(hosts[a], "123", &hints, tres[a]); if(error) { size_t msg_length = strlen(hosts[a]) + 21; @@ -52,12 +69,13 @@ int resolve_hosts (char **hosts, int hostc, struct addrinfo **res) { for(dres=*tres[a]; dres; dres=dres->ai_next) { entryc++; #ifdef DEBUG - printf("--------------------------------------------------------------------------------\n"); + WS80 printf("Resolv No.: %i Result of getaddrinfo for %s:\n", entryc, hosts[a]); printf("socktype: %i ", dres->ai_socktype); printf("protocol: %i ", dres->ai_protocol); printf("Prefered socktype: %i IP: %s\n", dres->ai_socktype, inet_ntoa(dres->ai_addr)); - printf("--------------------------------------------------------------------------------\n\n"); + WS80 + printf("\n"); #endif } } @@ -71,10 +89,6 @@ int resolve_hosts (char **hosts, int hostc, struct addrinfo **res) { struct addrinfo **result = (struct addrinfo **) malloc(sizeof(struct addrinfo**) * entryc); for(a=0, b=0; aai_next == NULL) { @@ -103,32 +117,83 @@ int resolve_hosts (char **hosts, int hostc, struct addrinfo **res) { return entryc; } +void +create_socket ( + SOCKET *rsock, + struct sockaddr *dest + ) +{ + *rsock = socket(dest->sa_family, SOCK_DGRAM, 0); + +#ifdef DEBUG + if(*rsock == -1) + printf("Failed to create UDP socket with family %i\n", dest->sa_family); +#endif + + +/* int error = bind(*rsock, dest, SOCKLEN(dest)); + + if(error != 0) { + *rsock = -1; +#ifdef DEBUG + printf("Failed to bind %i %i socket to address %s\n", error, errno, inet_ntoa(dest)); +#endif + } */ +} + +void +close_socket ( + SOCKET rsock + ) +{ + close(rsock); +} + /* Send a packet */ void sendpkt ( - struct sockaddr_storage *dest, + SOCKET rsock, + struct sockaddr *dest, struct pkt *pkt, int len ) { - int sock; - char *foo = (char *) malloc(len); +#ifdef DEBUG + register int a; - snprintf(foo, len, "%s", (char *)pkt); + unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * len); + + for(a=0; a 0 && a%8 == 0) + printf("\n"); - return; + printf("%x: %x \t", a, cpy[a]); + } - int cc = sendto(sock, (char *)pkt, len, 0, (struct sockaddr *)dest, + + printf("\nSending packet to %s...\n", inet_ntoa(dest)); +#endif + + int cc = sendto(rsock, (char *)pkt, len, 0, dest, SOCKLEN(dest)); if (cc == SOCKET_ERROR) { +#ifdef DEBUG + printf("Socket error: %i. Couldn't send packet!\n", cc); +#endif + if (errno != EWOULDBLOCK && errno != ENOBUFS) { } } - +#ifdef DEBUG + else { + printf("Packet sent.\n"); + } +#endif } int @@ -140,12 +205,40 @@ recvdata ( char *done ) { - socklen_t slen = SOCKLEN(rsock); + socklen_t slen = SOCKLEN(&rsock); - int recvc = recvfrom(rsock, rdata, rdata_length, MSG_DONTWAIT, +#ifdef DEBUG + printf("Trying to receive data from...\n"); +#endif + + int recvc = recvfrom(rsock, rdata, rdata_length, 0, sender, &slen); +#ifdef DEBUG + printf("recvfrom returned...\n"); +#endif + +#ifdef DEBUG + register int a; + + if(recvc > 0) { + printf("Received %i bytes from %s:\n", recvc, inet_ntoa(sender)); + unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * recvc); + for(a=0; a 0 && a%8 == 0) + printf("\n"); + + printf("%i: %x \t", a, cpy[a]); + } + } + else { + printf("Failure, recvc: %i\n", recvc); + } +#endif /* Remove this when found a reasonable max. size. For now * notify when there's data left to fetch */ @@ -216,9 +309,10 @@ recvpkt ( if ((PKT_MODE(rpkt->li_vn_mode) != MODE_SERVER && PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE) || rpkt->stratum >= STRATUM_UNSPEC) { - if (debug) - fprintf(stderr, "receive: mode %d stratum %d\n", +#ifdef DEBUG + printf("receive: mode %d stratum %d\n", PKT_MODE(rpkt->li_vn_mode), rpkt->stratum); +#endif return -1; }