]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
utilities.h, utilities.c:
authorJohannes Maximilian Kuehn <kuehn@ntp.org>
Mon, 17 Nov 2008 00:16:44 +0000 (01:16 +0100)
committerJohannes Maximilian Kuehn <kuehn@ntp.org>
Mon, 17 Nov 2008 00:16:44 +0000 (01:16 +0100)
  Added the tv_to_str function which converts a struct timeval to a timestamp string.
kod_management.c:
  Replaced strlcpy with strncpy for compatibility reasons.
main.c:
  Fixed the time difference report part and the set_time call part of bug #1088

bk: 4920b7ecvinZLfeG9pjlY1K_sJN07w

gsoc_sntp/kod_management.c
gsoc_sntp/main.c
gsoc_sntp/networking.c
gsoc_sntp/utilities.c
gsoc_sntp/utilities.h

index 022d97093fc812ee316714f48a64a05742c0929e..cf5f8ee5c45fef4af2704cc0ceecbabdb404260c 100644 (file)
@@ -84,7 +84,7 @@ add_entry (
        if(kod_init) {
                struct kod_entry *new_entry = (struct kod_entry *) malloc(sizeof(struct kod_entry));
                strcpy(new_entry->hostname, hostname);
-               strlcpy(new_entry->type, type, 4);
+               strncpy(new_entry->type, type, 4);
                new_entry->next = NULL;
 
                kod_db[entryc-1].next = new_entry;
index d35d8c487692b145365900659a7a9ae062a7ce0a..c3e03abbf242872a451e9d09180c2db695095336 100644 (file)
@@ -154,7 +154,7 @@ on_wire (
 
                int error, rpktl, sw_case;
 
-               char *hostname;
+               char *hostname = NULL, *ts_str = NULL;
 
                l_fp p_rec, p_xmt, p_ref, p_org, xmt, tmp, dst;
 
@@ -188,8 +188,6 @@ on_wire (
                else
                        sw_case = rpktl;
 
-               printf("sw_case: %i\n", sw_case);
-
                switch(sw_case) {
                        case SERVER_UNUSEABLE:
                                return -1;
@@ -207,7 +205,7 @@ on_wire (
                                add_entry(hostname, (char *) &r_pkt->refid);
 
                                if(ENABLED_OPT(NORMALVERBOSE))
-                                       printf("on_wire: Received KOD packet with code: %s from %s, demobilizing all connections\n", 
+                                       printf("sntp on_wire: Received KOD packet with code: %s from %s, demobilizing all connections\n", 
                                                        (char *) r_pkt->refid, hostname);
 
                                char *log_str = (char *) malloc(sizeof(char) * (INET6_ADDRSTRLEN + 72));
@@ -236,23 +234,23 @@ on_wire (
                                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);
+                               printf("sntp on_wire: Received %i bytes from %s\n", rpktl, adr_buf);
                        }
 
 #ifdef DEBUG
                        pkt_output(r_pkt, rpktl, stdout);
        
-                       printf("on_wire: rpkt->reftime:\n");
+                       printf("sntp on_wire: rpkt->reftime:\n");
                        l_fp_output(&(r_pkt->reftime), stdout);
-                       printf("on_wire: rpkt->org:\n");
+                       printf("sntp on_wire: rpkt->org:\n");
                        l_fp_output(&(r_pkt->org), stdout);
-                       printf("on_wire: rpkt->rec:\n");
+                       printf("sntp on_wire: rpkt->rec:\n");
                        l_fp_output(&(r_pkt->rec), stdout);
-                       printf("on_wire: rpkt->rec:\n");
+                       printf("sntp on_wire: rpkt->rec:\n");
                        l_fp_output_bin(&(r_pkt->rec), stdout);
-                       printf("on_wire: rpkt->rec:\n");
+                       printf("sntp on_wire: rpkt->rec:\n");
                        l_fp_output_dec(&(r_pkt->rec), stdout);
-                       printf("on_wire: rpkt->xmt:\n");
+                       printf("sntp on_wire: rpkt->xmt:\n");
                        l_fp_output(&(r_pkt->xmt), stdout);
 #endif
 
@@ -276,12 +274,22 @@ on_wire (
                        offset = (t21 + t34) / 2.;
                        delta = t21 - t34;
 
-
                        if(ENABLED_OPT(NORMALVERBOSE))
-                               printf("on_wire: t21: %.6f\t t34: %.6f\ndelta: %.6f\t offset: %.6f\n", 
+                               printf("sntp on_wire:\tt21: %.6f\t\t t34: %.6f\n\t\tdelta: %.6f\t offset: %.6f\n", 
                                        t21, t34, delta, offset);
 
-                       set_time(offset); 
+                       ts_str = tv_to_str(&tv_dst);
+
+                       printf("%s ", ts_str);
+
+                       if(offset > 0)
+                               printf("+");
+                       
+                       printf("%.3f\n", offset);
+                       free(ts_str);
+
+                       if(ENABLED_OPT(SETTOD) || ENABLED_OPT(ADJTIME))
+                               return set_time(offset); 
 
                        return 0;
                }
@@ -293,7 +301,7 @@ on_wire (
        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);
+               printf("sntp on_wire: Received no useable packet from %s!\n", adr_buf);
 
 
        log_msg(logmsg, 1);
index 79ff4246fb9280f0043fa58975fd5a9701515ab7..ee149f98e6a6b1a30ac1de4e80a9d1c38495bf5b 100644 (file)
@@ -173,14 +173,14 @@ sendpkt (
        if(ENABLED_OPT(NORMALVERBOSE)) {
                getnameinfo((struct sockaddr *) dest, SOCKLEN(dest), adr_buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
 
-               printf("sntp sendpkt: Sending packet to %s...\n", adr_buf);
+               printf("sntp sendpkt: Sending packet to %s... ", adr_buf);
        }
 
        int cc = sendto(rsock, (char *)pkt, len, 0, (struct sockaddr *)dest, SOCKLEN(dest));
 
        if (cc == SOCKET_ERROR) {
 #ifdef DEBUG
-               printf("sntp sendpkt: Socket error: %i. Couldn't send packet!\n", cc);
+               printf("\n sntp sendpkt: Socket error: %i. Couldn't send packet!\n", cc);
 #endif
 
                if (errno != EWOULDBLOCK && errno != ENOBUFS) {
index 722f397c9bb230de93a0d00207702d506d7c3117..d6942c56db3101904c8226e85abdbe38bee593c8 100644 (file)
@@ -157,3 +157,39 @@ ss_to_str (
 
        return buf;
 }
+
+/* Converts a struct tv to a date string
+ */
+char *
+tv_to_str (
+               struct timeval *tv
+         )
+{
+       static const char *month_names[] = {
+               "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+       };
+
+       char *buf = (char *) malloc(sizeof(char) * 48);
+
+       time_t cur_time = time(NULL);
+
+       struct tm *tm_ptr = (struct tm *) malloc(sizeof(struct tm));
+       tm_ptr = localtime(&cur_time);
+
+
+       snprintf(buf, 48, "%i %s %.2d %.2d:%.2d:%.2d.%.3d", 
+                       tm_ptr->tm_year + 1900,
+                       month_names[tm_ptr->tm_mon],
+                       tm_ptr->tm_mday,
+                       tm_ptr->tm_hour,
+                       tm_ptr->tm_min,
+                       tm_ptr->tm_sec,
+                       (int)tv->tv_usec);
+
+       return buf;
+}
+
+
+
+               
index db26db83ec2bc6bc0ab8cbf6b0a1ef8158063170..8d9575f6314f9c7cc47a889b1a792188fc7acb59 100644 (file)
@@ -53,5 +53,6 @@ void l_fp_output_dec (l_fp *ts, FILE *output);
 
 char *addrinfo_to_str (struct addrinfo *addr);
 char *ss_to_str (struct sockaddr_storage *saddr);
+char *tv_to_str (struct timeval *tv);
 
 #endif