]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
utilities.h:
authorJohannes Maximilian Kuehn <kuehn@ntp.org>
Fri, 15 Aug 2008 21:41:28 +0000 (06:41 +0900)
committerJohannes Maximilian Kuehn <kuehn@ntp.org>
Fri, 15 Aug 2008 21:41:28 +0000 (06:41 +0900)
  BitKeeper file /geom_mobile/home/Amidamaru/projects/sntp/repo/ntp-dev/gsoc_sntp/utilities.h
Makefile.am:
  Added sntp-opts.c to sources
  + minor fixes
main.c:
  on_wire, timesetting stuff etc.
log.c, kod_management.c:
  Bug fixes
utilities.o:
  BitKeeper file /geom_mobile/home/Amidamaru/projects/sntp/repo/ntp-dev/gsoc_sntp/utilities.o
utilities.c:
  BitKeeper file /geom_mobile/home/Amidamaru/projects/sntp/repo/ntp-dev/gsoc_sntp/utilities.c
sntp-opts.def:
  fixed arg-type stuff

bk: 48a5f808IuFfNCcjV4Pp9DdbpS4g6g

gsoc_sntp/Makefile.am
gsoc_sntp/kod_management.c
gsoc_sntp/kod_management.h
gsoc_sntp/log.c
gsoc_sntp/log.h
gsoc_sntp/main.c
gsoc_sntp/networking.c
gsoc_sntp/networking.h
gsoc_sntp/sntp-opts.def
gsoc_sntp/utilities.c [new file with mode: 0644]
gsoc_sntp/utilities.h [new file with mode: 0644]

index 2072c5e360bdab9dfdd4386fa3842f24e986f133..26bc948d0bd72f03c5e3b7405f6e8131a512c923 100644 (file)
@@ -3,8 +3,8 @@
 AUTOMAKE_OPTIONS = foreign
 ACLOCAL_AMFLAGS= -I libopts/m4
 
-AM_CPPFLAGS=   $(LIBOPTS_CFLAGS) -I$(top_srcdir)/../include/
-LDADD= $(LIBOPTS_LDADD)
+AM_CPPFLAGS=   $(LIBOPTS_CFLAGS) -I$(top_srcdir)/../include/ -Wall
+LDADD= $(LIBOPTS_LDADD) -lm ../libntp/libntp.a
 
 NULL=
 
@@ -20,6 +20,10 @@ SUBDIRS+= .
 
 sntp_SOURCES = \
        networking.c \
+       kod_management.c \
+       log.c \
+       utilities.c \
+       sntp-opts.c \
        main.c \
        $(NULL)
 
index 8d3856e57241919ce9fa24d20b703f5a38721f22..54b7525e7634bc453daa03648b5c8947535062cc 100644 (file)
@@ -46,9 +46,13 @@ kod_entry_exists (
                char *search_str
                )
 {
-       struct kod_entry **dummy;
+       struct kod_entry **dummy = NULL;
 
-       return search_entry(search_str, dummy);
+       if(dummy == NULL)
+               return 0;
+
+       else
+               return 1;
 }
 
 void 
@@ -177,7 +181,8 @@ kod_init_kod_db (
 
        int scan_value = 0;
        while(!feof(db_s)) {
-               printf("%s", fgets(fbuf, 272, db_s));
+               fgets(fbuf, 272, db_s);
+               
                int sepc = 0;
                for(a=0; a<strlen(fbuf); a++) {
                        if(fbuf[a] == ':') 
@@ -226,10 +231,6 @@ kod_init_kod_db (
                        error = 1;
                } 
                else {
-#ifdef DEBUG
-                       printf("KOD entry %i %i: %s at %i type %s\n", a, b,  kod_db[b].hostname, 
-                                       kod_db[b].timestamp, kod_db[b].type);
-#endif
                        if(b > 0) 
                                kod_db[b-1].next = &kod_db[b];
                }
@@ -240,6 +241,7 @@ kod_init_kod_db (
                printf("KOD entry %i: %s at %i type %s\n", a, kod_db[a].hostname, 
                                kod_db[a].timestamp, kod_db[a].type);
 
+       printf("\n");
 #endif
 
        if(ferror(db_s) || error) {
index b119687f44b04977f77e128fd8f92eabf5eec91c..fcb22d048ac6f5ecf28bef9f34417d40aedb4df6 100644 (file)
@@ -4,11 +4,12 @@
 struct kod_entry {
        char hostname[255];
        unsigned int timestamp;
-       char type[4];
+       char type[5];
        struct kod_entry *next;
 };
 
 int search_entry (char *hostname, struct kod_entry **dst);
+int kod_entry_exists (char *search_str);
 
 void add_entry (char *hostname, char *type);
 void delete_entry (char *hostname, char *type);
index cd77524391d8e6ae74596739544a489ab71b904b..71fd3e93ce1d4ace4b7968020af0300923022f68 100644 (file)
@@ -9,22 +9,19 @@ FILE *log_file;
 
 
 void log_msg(char *message, char type) {
-       if(filelog) {
-               if(init) {
-                       fprintf(log_file, message);
-               }
-               else {
-                       fprintf(stderr, "Error: Log not initialized!\n");
-               }
+       if(init) {
+               printf("log!\n");
+               printf(log_file, message);
        }
        else {
+               printf("ELSEZWEIG!!!!! HAAAAAARRRRRRRRRRRRRRRRRG!\n");
                switch(type) {
                        case 0:
-                               type = LOG_DEBUG;
+                               type = LOG_CONS;
                                break;
                        
                        case 1:
-                               type = LOG_WARNING;
+                               type = LOG_DEBUG | LOG_CONS;
                                break;
        
                        case 2: 
@@ -37,15 +34,17 @@ void log_msg(char *message, char type) {
 }
 
 void debug_msg(char *message) {
-       if(filelog) {
+       if(HAVE_OPT(FILELOG)) {
                fprintf(stderr, message);
+               fprintf(stderr, "\n");
        }
        else {
-               syslog(LOG_DEBUG | LOG_PERROR, message);
+               syslog(LOG_DEBUG | LOG_PERROR | LOG_CONS, message);
        }
 }
 
 void init_log(char *logfile) {
+       printf("INIT IST KRIEEEEEG %s!!!\n", logfile);
        log_file = fopen(logfile, "a");
        
        if(log_file == NULL) {
index aa359c39798dfda358e605192982b2414e410bab..a17a5227b8d11844df3ceaa3641a839c58811b81 100644 (file)
@@ -12,9 +12,6 @@
 void log_msg(char *message, char type);
 void debug_msg(char *message);
 
-char init = 0;
-FILE *log_file;
-
 void init_log(char *logfile);
 void cleanup_log(void);
 
index 431bc45b2bf9d6349997cc6a88fd9dc7f5e61bff..70fc9c739acfbd420f52bcdf991db7b67a3bae47 100644 (file)
@@ -1,10 +1,5 @@
-#if 0
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#endif
-
+#include <ntp_fp.h>
+#include <ntp.h>
 #include <ntp_stdlib.h>
 #include <ntp_unixtime.h>
 #include <l_stdlib.h>
 
 #include <sntp-opts.h>
 
+#include "kod_management.h"
 #include "networking.h"
-/*#include "header.h"*/
-
-#define NTP_PORT 123
+#include "utilities.h"
+#include "log.h"
 
 #define DEBUG
 
 int ai_fam_templ;
 volatile int debug;
+char adr_buf[INET6_ADDRSTRLEN];
+
+
+void set_li_vn_mode (struct pkt *spkt, char leap, char version, char mode); 
+int sntp_main (int argc, char **argv);
+int on_wire (struct addrinfo *host);
+void set_li_vn_mode (struct pkt *spkt, char leap, char version, char mode);
+int set_time (double offset);
 
-/* tOptions sntpOptions; */
 
 
 int 
@@ -44,10 +46,6 @@ sntp_main (
        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
@@ -57,15 +55,12 @@ sntp_main (
 
 #ifndef NO_DISK
 #endif
+       log_msg("Started sntp", 0);
 
        int optct = optionProcess(&sntpOptions, argc, argv);
        argc -= optct;
        argv += optct; 
 
-       for(c=0; c<argc; c++) 
-               printf("%s\n", argv[c]);
-
-
        /* Parse config file if declared TODO */
 
        /* Initialize logging system */
@@ -77,9 +72,6 @@ sntp_main (
                kod_init_kod_db(OPT_ARG(KOD));
        }
 
-       kod_init_kod_db("test.kod");
-
-
 
        /* Considering employing a variable that prevents functions of doing anything until 
         * everything is initialized properly 
@@ -97,17 +89,29 @@ sntp_main (
         */
        int sync_data_suc = 0;
        for(c=0; c<resc && !sync_data_suc; c++) {
-               if(!kod_entry_exists(inet_ntoa(resh[c]->ai_addr))) {
+               getnameinfo(resh[c]->ai_addr, resh[c]->ai_addrlen, adr_buf, 
+                               INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
+
+               if(!kod_entry_exists(adr_buf) &&
+                  is_reachable(resh[c])) {
                        int ow_ret = on_wire(resh[c]);
-                       sync_data_suc = 1;
+                       if(ow_ret < 0) {
+                               printf("on_wire failed!\n");
+                       }
+                       else {
+                               sync_data_suc = 1;
+                       }
                }
                else {
                        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));
+                       printf("KoD package exists for %s, stopping any further communication.\n", 
+                                       adr_buf);
 
                }
+
+               freeaddrinfo(resh[c]);
        }
 
        return 0;
@@ -118,72 +122,192 @@ on_wire (
                struct addrinfo *host
                                        )
 {
+       register int try;
        SOCKET sock;
+       
+       for(try=0; try<5; try++) {
+               struct pkt *x_pkt = (struct pkt *) malloc(sizeof(struct pkt));
+               struct pkt *r_pkt = (struct pkt *) malloc(sizeof(struct pkt));
+
+               struct timeval tv_org, tv_dst;
+       
+               double t21, t34, delta, offset;
 
-       struct pkt *x_pkt = (struct pkt *) malloc(sizeof(struct pkt));
-       struct pkt *r_pkt = (struct pkt *) malloc(sizeof(struct pkt));
+               int error, rsock, rpktl;
 
-       struct timeval tv_t1, tv_t2;
-       struct timezone tz;
-       l_fp t1, t4;
+               l_fp p_rec, p_xmt, p_ref, p_org, org, tmp, dst, theta;
 
-       int error = GETTIMEOFDAY(&tv_t1, &tz);
+
+               error = GETTIMEOFDAY(&tv_org, (struct timezone *)NULL);
 
 #ifdef DEBUG
-       printf("tv_t1 sec: %i msec: %i\n", tv_t1.tv_sec, tv_t1.tv_usec);
+               printf("Current time sec: %i msec: %i\n\n", tv_org.tv_sec, tv_org.tv_usec);
 #endif
 
-       TVTOTS(&tv_t1, &(x_pkt->org));
+               tv_org.tv_sec += JAN_1970;
+               TVTOTS(&tv_org, &org);
+               HTONL_FP(&org, &(x_pkt->org));
+
+               x_pkt->stratum = STRATUM_TO_PKT(STRATUM_UNSPEC);
+               x_pkt->ppoll = 8;
+               /* FIXME! Modus broadcast + adr. check -> bdr. pkt */
+               set_li_vn_mode(x_pkt, LEAP_NOTINSYNC, 4, 3);
 
-/* Data from a valid timestamp */
-/*     unsigned char *tptr = &(x_pkt->org);
 
-       tptr[0] = 0xcc;
-       tptr[1] = 0x44;
-       tptr[2] = 0x87;
-       tptr[3] = 0x45;*/
+       
+               create_socket(&sock, (struct sockaddr_storage *)host->ai_addr);
 
-       x_pkt->stratum = 15;
-       x_pkt->ppoll = 8;
-       x_pkt->li_vn_mode = 0x1b;
+               rsock = sendpkt(sock, host->ai_addr, x_pkt, LEN_PKT_NOMAC);
+               rpktl = recvpkt(sock, r_pkt, x_pkt);
 
+               close_socket(sock);
+
+               if(rpktl == -1) 
+                       return -1; 
+               
+               /* -2 would indicate that we should try to get a packet 
+                * from this server again 
+                */
+               if(rpktl != -2) {
+                       NTOHL_FP(&r_pkt->reftime, &p_ref);
+                       NTOHL_FP(&r_pkt->org, &p_org);
+                       NTOHL_FP(&r_pkt->rec, &p_rec);
+                       NTOHL_FP(&r_pkt->xmt, &p_xmt);
+
+                       if(ENABLED_OPT(NORMALVERBOSE)) {
+                               getnameinfo(host->ai_addr, host->ai_addrlen, adr_buf, 
+                                               INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
+
+                               printf("\non_wire: Received %i bytes from %s\n", rpktl, adr_buf);
+                       }
+
+#ifdef DEBUG
+                       pkt_output(r_pkt, rpktl, stdout);
+               
+                       printf("on_wire: rpkt->reftime:\n");
+                       l_fp_output(&(r_pkt->reftime), stdout);
+                       printf("on_wire: rpkt->org:\n");
+                       l_fp_output(&(r_pkt->org), stdout);
+                       printf("on_wire: rpkt->rec:\n");
+                       l_fp_output(&(r_pkt->rec), stdout);
+                       printf("on_wire: rpkt->rec:\n");
+                       l_fp_output_bin(&(r_pkt->rec), stdout);
+                       printf("on_wire: rpkt->rec:\n");
+                       l_fp_output_dec(&(r_pkt->rec), stdout);
+                       printf("on_wire: rpkt->xmt:\n");
+                       l_fp_output(&(r_pkt->xmt), stdout);
+#endif
 
+                       GETTIMEOFDAY(&tv_dst, (struct timezone *)NULL);
        
-       /*in_addr_t foo = inet_addr("127.0.0.1"); */
-       create_socket(&sock, host->ai_addr);
+                       tmp = p_rec;
+                       L_SUB(&tmp, &p_org);
 
-       int rsock = sendpkt(sock, host->ai_addr, x_pkt, 48);
+                       LFPTOD(&tmp, t21);
 
-       int rpktl = recvpkt(sock, r_pkt, x_pkt);
+                       TVTOTS(&tv_dst, &dst);
 
-       close_socket(sock);
+                       tmp = dst;
+                       L_SUB(&tmp, &p_xmt);
 
-       NTOHL_FP(&(r_pkt->rec), &(r_pkt->rec));
-       TSTOTV(&(r_pkt->rec), &tv_t2);
+                       LFPTOD(&tmp, t34);
 
-       double time = 0.0;
+                       offset = (t21 + t34) / 2.;
+                       delta = t21 - t34;
 
-       LFPTOD(&(r_pkt->rec), time);
 
-       printf("\nrec: %d\n", time);
+                       if(ENABLED_OPT(NORMALVERBOSE))
+                               printf("on_wire: t21: %.6f\t t34: %.6f\ndelta: %.6f\t offset: %.6f\n", 
+                                               t21, t34, delta, offset);
 
+                       set_time(offset);
 
+                       return 0;
+               }
+       }
 
-       return 0;
-}
+       char logmsg[32 + INET6_ADDRSTRLEN];
+       getnameinfo(host->ai_addr, host->ai_addrlen, adr_buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
 
+       snprintf(logmsg, 32 + INET6_ADDRSTRLEN, "Received no useable packet from %s!", adr_buf);
 
+       if(ENABLED_OPT(NORMALVERBOSE))
+               printf("on_wire: Received no useable packet from %s!\n", adr_buf);
 
 
+       log_msg(logmsg, 1);
 
+       return -1;
+}
 
+void
+set_li_vn_mode (
+               struct pkt *spkt,
+               char leap,
+               char version,
+               char mode
+              ) 
+{
 
+       if(leap > 3) {
+               debug_msg("set_li_vn_mode: leap > 3 using max. 3");
+               leap = 3;
+       }
 
+       if(mode > 7) {
+               debug_msg("set_li_vn_mode: mode > 7, using client mode 3");
+               mode = 3;
+       }
 
-       
+       spkt->li_vn_mode  = leap << 6;
+       spkt->li_vn_mode |= version << 3;
+       spkt->li_vn_mode |= mode;
+}
+
+int
+set_time (
+               double offset
+        )
+{
+       struct timeval tp;
 
+       if(ENABLED_OPT(SETTOD)) {
 
+               GETTIMEOFDAY(&tp, (struct timezone *)NULL);
 
+               tp.tv_sec += (int) offset;
+               tp.tv_usec += offset - (double)((int)offset);
 
+               if(SETTIMEOFDAY(&tp, (struct timezone *)NULL) < 0) {
+                       if(errno == EPERM)
+                               printf("set_time: You don't have enough priviledges to call settimeofday(), cannot set time!\n");
 
+                       else 
+                               printf("set_time: settimeofday() returned with an error, couldn't set time!\n");
+
+                       return -1;
+               }
+               else {
+                       return 0;
+               }
+       }
+       else {
+               tp.tv_sec = (int) offset;
+               tp.tv_usec = offset - (double)((int)offset);
+
+               if(ADJTIMEOFDAY(&tp, NULL) < 0) {
+                       if(errno == EPERM)
+                               printf("set_time: You don't have enough priviledges to call adjtime(), cannot set time!\n");
+                       else 
+                               printf("set_time: adjtime() returned with an error, couldn't set time!\n");
+                       
+                       return -1;
+               }
+               else {
+                       return 0;
+               }
+       }
+
+
+}
 
index 4d971d0836838210e1c760b94418181589d6580b..90b8aa4995a7ec4c26c18b72a04c77aacdc2beef 100644 (file)
@@ -1,20 +1,12 @@
 #include <config.h>
 
-/* #include "sntp-opts.h"      /**/
+#include "sntp-opts.h" 
 #include "networking.h"
 #include "header.h"
+#include "utilities.h"
 #include "log.h"
 
-#ifdef DEBUG
-#define WS80 do { \
-       register int wsc; \
-       for(wsc=0; wsc<80; wsc++) \
-               printf("-"); \
-       printf("\n"); \
-} while(0);
-#endif
-
-
+char adr_buf[INET6_ADDRSTRLEN];
 
 
 int 
@@ -27,8 +19,6 @@ resolve_hosts (
        register unsigned int a, b;
        unsigned int entryc = 0; 
 
-       /* res = (struct addrinfo **) malloc(sizeof(struct addrinfo) * (hostc + 1)); */
-
        if(hostc < 1) 
                return 0;
        
@@ -69,13 +59,13 @@ resolve_hosts (
                        for(dres=*tres[a]; dres; dres=dres->ai_next) {
                                entryc++;
 #ifdef DEBUG   
-                               WS80
+                               getnameinfo(dres->ai_addr, dres->ai_addrlen, adr_buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
+                               STDLINE
                                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));
-                               WS80
-                               printf("\n");   
+                               printf("Prefered socktype: %i IP: %s\n", dres->ai_socktype, adr_buf);
+                               STDLINE
 #endif
                        }
                }
@@ -106,7 +96,9 @@ resolve_hosts (
 
 #ifdef DEBUG
        for(a=0; a<entryc; a++)
-               printf("%x: IP %s\n", result[a], inet_ntoa(result[a]->ai_addr));
+               getnameinfo(result[a]->ai_addr, result[a]->ai_addrlen, adr_buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
+
+               printf("%x: IP %s\n", result[a], adr_buf); 
 #endif
 
        *res = (struct addrinfo *) malloc(sizeof(struct addrinfo *) * entryc);
@@ -120,25 +112,16 @@ resolve_hosts (
 void 
 create_socket (
                SOCKET *rsock,
-               struct sockaddr *dest
+               struct sockaddr_storage *dest
                )
 {
-       *rsock = socket(dest->sa_family, SOCK_DGRAM, 0);
+       *rsock = socket(dest->ss_family, SOCK_DGRAM, 0);
 
 #ifdef DEBUG
        if(*rsock == -1)
-               printf("Failed to create UDP socket with family %i\n", dest->sa_family);
+               printf("Failed to create UDP socket with family %i\n", dest->ss_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 
@@ -153,32 +136,19 @@ close_socket (
 void
 sendpkt (
        SOCKET rsock,
-       struct sockaddr *dest,
+       struct sockaddr_storage *dest,
        struct pkt *pkt,
        int len
        )
 {
 #ifdef DEBUG
-       register int a;
-
-       unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * len);
-       
-       for(a=0; a<len; a++)
-               cpy[a] = ((unsigned char *) pkt)[a];
-
-       for(a=0; a<len; a++) {
-               if(a > 0 && a%8 == 0) 
-                       printf("\n");
-
-               printf("%x: %x \t", a, cpy[a]);
-       }
-
+       pkt_output(pkt, len, stdout);
+       getnameinfo((struct sockaddr *) dest, dest->ss_len, adr_buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
 
-       printf("\nSending packet to %s...\n", inet_ntoa(dest));
+       printf("\nSending packet to %s...\n", adr_buf);
 #endif
 
-       int cc = sendto(rsock, (char *)pkt, len, 0, dest,
-                       SOCKLEN(dest));
+       int cc = sendto(rsock, (char *)pkt, len, 0, (struct sockaddr *)dest, dest->ss_len);
 
        if (cc == SOCKET_ERROR) {
 #ifdef DEBUG
@@ -199,7 +169,7 @@ sendpkt (
 int
 recvdata (
                SOCKET rsock,
-               struct sockaddr *sender,
+               struct sockaddr_storage *sender,
                char *rdata,
                size_t rdata_length,
                char *done
@@ -212,7 +182,7 @@ recvdata (
 #endif
 
        int recvc = recvfrom(rsock, rdata, rdata_length, 0, 
-                       sender, &slen);
+                       (struct sockaddr *) sender, &slen);
 #ifdef DEBUG
        printf("recvfrom returned...\n");
 #endif
@@ -221,19 +191,11 @@ recvdata (
        register int a;
 
        if(recvc > 0) {
-               printf("Received %i bytes from %s:\n", recvc, inet_ntoa(sender));
+               getnameinfo((struct sockaddr *)sender, sender->ss_len, adr_buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
 
-               unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * recvc);
-       
-               for(a=0; a<recvc; a++)
-                       cpy[a] = ((unsigned char *) rdata)[a];
+               printf("Received %i bytes from %s:\n", recvc, adr_buf);
 
-               for(a=0; a<recvc; a++) {
-                       if(a > 0 && a%8 == 0) 
-                               printf("\n");
-
-                       printf("%i: %x \t", a, cpy[a]);
-               }
+               pkt_output((struct pkt *) rdata, recvc, stdout);
        }
        else {
                printf("Failure, recvc: %i\n", recvc);
@@ -251,8 +213,7 @@ recvdata (
 }
 
 /* Fetch data, check if it's data for us and whether it's useable or not. If not, return -1
- * so we can delete this server from our list and continue with another one. KOD package handling
- * is not done here. 
+ * so we can delete this server from our list and continue with another one. 
  */
 int 
 recvpkt (
@@ -261,6 +222,7 @@ recvpkt (
                struct pkt *spkt
        )
 {
+       register int a;
        int has_mac;
        int is_authentic;
 
@@ -271,7 +233,7 @@ recvpkt (
        l_fp ci;
 
 
-       struct sockaddr sender;
+       struct sockaddr_storage sender;
        char *rdata, done;
 
        /* Much space, just to be sure */
@@ -291,7 +253,7 @@ recvpkt (
                has_mac = 0;
 
        else if(pkt_length > LEN_PKT_NOMAC) 
-               has_mac = 1;
+               has_mac = pkt_length - LEN_PKT_NOMAC;
        
        else
                if(debug) {
@@ -299,23 +261,67 @@ recvpkt (
                        return -1;
                }
 
-       rpkt = (struct pkt *)rdata;
+       if(has_mac) {
+               if(has_mac > MAX_MAC_LEN || has_mac % 4 != 0) {
+                       return -1;
+                                                       }
+               /* Do auth stuff */
+       }
+
+       /*struct pkt *tmp_rpkt = (struct pkt *)rdata;
+       rpkt->li_vn_mode = tmp_rpkt->li_vn_mode; 
+       rpkt->stratum = tmp_rpkt->stratum;
+       rpkt->ppoll = tmp_rpkt->ppoll;
+       rpkt->precision = tmp_rpkt->precision;
+       rpkt->rootdelay = tmp_rpkt->rootdelay;
+       rpkt->rootdisp = tmp_rpkt->rootdisp;
+       rpkt->refid = tmp_rpkt->refid;
+       rpkt->reftime = tmp_rpkt->reftime;
+       rpkt->org = tmp_rpkt->org;
+       rpkt->rec = tmp_rpkt->rec;
+       rpkt->xmt = tmp_rpkt->xmt; */
+
+       if(pkt_length > LEN_PKT_MAC) {
+               if(ENABLED_OPT(NORMALVERBOSE))
+                       printf("sntp recvpkt: Received packet is too big (%i bytes), trying again to get a useable packet\n", 
+                                       pkt_length);
+
+               return -2;
+       }
+       
+       for(a=0; a<pkt_length; a++) 
+               ((unsigned char *) rpkt)[a] = rdata[a];
 
+       printf("%x %x\n", PKT_VERSION(rpkt->li_vn_mode), NTP_OLDVERSION);
        if (PKT_VERSION(rpkt->li_vn_mode) < NTP_OLDVERSION ||
                PKT_VERSION(rpkt->li_vn_mode) > NTP_VERSION) {
                return -1;
-       }
+       } 
 
-       if ((PKT_MODE(rpkt->li_vn_mode) != MODE_SERVER
-                && PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE)
-               || rpkt->stratum >= STRATUM_UNSPEC) {
+       if (PKT_MODE(rpkt->li_vn_mode) != MODE_SERVER
+                && PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE) {
 #ifdef DEBUG
-                       printf("receive: mode %d stratum %d\n",
+                       printf("sntp recvpkt: mode %d stratum %i\n",
                           PKT_MODE(rpkt->li_vn_mode), rpkt->stratum);
 #endif
                return -1;
        }
 
+       if(rpkt->stratum == STRATUM_PKT_UNSPEC) {
+               if(ENABLED_OPT(NORMALVERBOSE))
+                       printf("sntp recvpkt: Unusable packet, discarding. (stratum: %i)\n", rpkt->stratum);
+               
+               return -1;
+       }
+
+       if(PKT_LEAP(rpkt->li_vn_mode) == LEAP_NOTINSYNC) {
+               if(ENABLED_OPT(NORMALVERBOSE)) 
+                       printf("sntp recvpkt: Server not in sync, skipping this server\n");
+
+               return -1;
+       }
+
+
        /*
         * Decode the org timestamp and make sure we're getting a response
         * to our last request. 
@@ -327,7 +333,6 @@ recvpkt (
                return -1;
        }
 
-       /* Do authentication stuff here */
 
        /* Left for now, finishin other stuff. I think I might want that somewhere else,
         * don't want this function to do on-wire tasks. Sanity checks are right here I think
@@ -360,7 +365,11 @@ is_reachable (
        SOCKET sockfd;
 
        sockfd = socket(dst->ai_family, SOCK_DGRAM, 0);
+
        if (sockfd == -1) {
+#ifdef DEBUG
+               printf("is_reachable: Couldn't create socket\n");
+#endif
                return 0;
        }
 
@@ -368,6 +377,7 @@ is_reachable (
                closesocket(sockfd);
                return 0;
        }
+
        closesocket(sockfd);
        return 1;
 }
index bf034c94aaeae4010de8c4778a854ca81d9f76b1..fb5b26cc96e49b26f600c3ca74dd76de45fc096c 100644 (file)
@@ -15,7 +15,6 @@
 #include <ntp_stdlib.h>
 
 #include "data_formats.h"
-
 /* FIXME, see portability issue 1 */ /* irrelevant for now */
 #ifndef SYS_WINNT
 #define SOCKET int
@@ -36,10 +35,13 @@ int is_reachable (struct addrinfo *dst);
 
 int resolve_hosts (char **hosts, int hostc, struct addrinfo **res);
 
-/* From ntpdate.c */
-void send_pkt (struct sockaddr_storage *dest, struct pkt *pkt, int len);
+void create_socket (SOCKET *rsock, struct sockaddr_storage *dest);
+
+void close_socket (SOCKET rsock);
+
+void send_pkt (SOCKET rsock, struct sockaddr_storage *dest, struct pkt *pkt, int len);
 
-int recvdata (SOCKET rsock, struct sockaddr *sender, char *rdata, size_t rdata_length, char *done);
+int recvdata (SOCKET rsock, struct sockaddr_storage *sender, char *rdata, size_t rdata_length, char *done);
 
 int recvpkt (SOCKET rsock, struct pkt *rpkt, struct pkt *spkt);
 
index 639b37cf6aefbda7b35f2294ac0b043a2523fa3d..f7762d572bc6f4758c3b05dd11122c0b43ad1770 100644 (file)
@@ -4,6 +4,11 @@ autogen definitions options;
 
 #include autogen-version.def
 
+prog-name      = "sntp";
+prog-title     = "standard SNTP program";
+homerc         =  $HOME, ".";
+argument       = '...';
+
 copyright = {
     date  = "2008";
     owner = "ntp.org";
@@ -13,9 +18,6 @@ copyright = {
 };
 
 
-prog-name      = "sntp";
-prog-title     = "standard SNTP program";
-homerc         =  $HOME, ".";
 long-opts;
 
 config-header  = "config.h";
@@ -69,6 +71,7 @@ flag = {
 flag = {
     name      = debug;
     value     = d;
+    ifdef     = DEBUG;
     descrip   = "Debugging output";
     doc = <<-  _EndOfDoc_
        Very verbose debugging output that will interfere with the timing
@@ -82,6 +85,7 @@ flag = {
 flag = {
     name      = kod;
     value     = k;
+    arg-type  = string;
     descrip   = "Specify a file for the KOD packet storage";
     doc = <<-  _EndOfDoc_
     Specifies the file to be used to store KOD packaet information.
@@ -101,6 +105,7 @@ flag = {
 flag = {
        name    = filelog;
        value   = l;
+       arg-type  = string;
        flags-cant = syslog;
        descrip = "Logging to specified logfile";
        doc = <<-  _EndOfDoc_
@@ -110,7 +115,7 @@ flag = {
 };
 
 flag = {
-    name      = settimeofday;
+    name      = settod;
     value     = r;
     flags-cant = adjtime;
     descrip   = "Set (step) the time with settimeofday()";
@@ -121,12 +126,22 @@ flag = {
 flag = {
     name      = adjtime;
     value     = a;
-    flags-cant = settimeofday;
+    flags-cant = settod;
     descrip   = "Set (slew) the time with adjtime()";
     doc = <<-  _EndOfDoc_
        _EndOfDoc_;
 };
 
+flag = {
+       name    = hostlist;
+       value   = h;
+       descrip = "Specify a list of ntp servers";
+       doc = <<-  _EndOfDoc_
+       Comma seperated list of ntp server hosts like 
+       \"time.foo.org,time.bar.org,time.baz.org\"
+       _EndOfDoc_;
+};
+
 detail = <<-  _END_DETAIL
 .I sntp
 can be used as a SNTP client to query a NTP or SNTP server and either display
@@ -171,7 +186,7 @@ in the local clock.  This option should be set only when debugging the source.
 .TP
 .B \-r
 indicates that the system clock should be reset by
-.IR settimeofday .
+.IR settod .
 Naturally, this will work only if the user has enough privilege.
 .TP
 .B \-a
diff --git a/gsoc_sntp/utilities.c b/gsoc_sntp/utilities.c
new file mode 100644 (file)
index 0000000..1d6de37
--- /dev/null
@@ -0,0 +1,101 @@
+#include "utilities.h"
+
+void 
+pkt_output (
+               struct pkt *dpkg,
+               int pkt_length, 
+               FILE *output
+          )
+{
+       register int a;
+
+       unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * pkt_length);               
+
+       for(a=0; a<pkt_length; a++) 
+               cpy[a] = ((unsigned char *) dpkg)[a];
+
+
+       fprintf(output, HLINE);
+
+       for(a=0; a<pkt_length; a++) {
+               if(a > 0 && a%8 == 0)
+                       fprintf(output, "\n");
+
+               fprintf(output, "%i: %x \t", a, cpy[a]);
+       }
+
+       fprintf(output, "\n");
+       fprintf(output, HLINE);
+}
+
+void
+l_fp_output (
+               l_fp *ts,
+               FILE *output
+           )
+{
+       register int a;
+
+       fprintf(output, HLINE);
+
+       for(a=0; a<8; a++) 
+               fprintf(output, "%i: %x \t", a, ((unsigned char *) ts)[a]);
+
+       fprintf(output, "\n");
+       fprintf(output, HLINE);
+
+}
+
+void 
+l_fp_output_bin (
+               l_fp *ts,
+               FILE *output
+               )
+{
+       register int a, b, bexp;
+
+       fprintf(output, HLINE);
+
+       for(a=0; a<8; a++) {
+               short tmp = ((unsigned char *) ts)[a];
+               tmp++;
+
+               fprintf(output, "%i: ", a);
+
+               for(b=7; b>=0; b--) {
+                       int texp = (int) pow(2, b);
+
+                       if(tmp - texp > 0) {
+                               fprintf(output, "1");
+                               tmp -= texp;
+                       }
+                       else {
+                               fprintf(output, "0");
+                       }
+               }
+
+               fprintf(output, " ");
+       }
+
+       fprintf(output, "\n");
+       fprintf(output, HLINE);
+}
+
+void
+l_fp_output_dec (
+               l_fp *ts,
+               FILE *output
+           )
+{
+       register int a;
+
+       fprintf(output, HLINE);
+
+       for(a=0; a<8; a++) 
+               fprintf(output, "%i: %i \t", a, ((unsigned char *) ts)[a]);
+
+       fprintf(output, "\n");
+       fprintf(output, HLINE);
+
+}
+
diff --git a/gsoc_sntp/utilities.h b/gsoc_sntp/utilities.h
new file mode 100644 (file)
index 0000000..25da6f9
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef UTILITIES_H
+#define UTILITIES_H
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ntp_fp.h>
+#include <ntp.h>
+
+#define HLINE "--------------------------------------------------------------------------------\n"
+#define PHLINE fprintf(output, HLINE);
+#define STDLINE printf(HLINE);
+
+
+void pkt_output (struct pkt *dpkg, int pkt_length, FILE *output);
+void l_fp_output (l_fp *ts, FILE *output);
+void l_fp_output_bin (l_fp *ts, FILE *output);
+void l_fp_output_dec (l_fp *ts, FILE *output);
+
+
+#endif