From: Johannes Maximilian Kuehn Date: Mon, 17 Nov 2008 00:16:44 +0000 (+0100) Subject: utilities.h, utilities.c: X-Git-Tag: NTP_4_2_5P143~3^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ecd1babe9a9f93bad542c159715653fd9f597f44;p=thirdparty%2Fntp.git utilities.h, utilities.c: 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 --- diff --git a/gsoc_sntp/kod_management.c b/gsoc_sntp/kod_management.c index 022d97093..cf5f8ee5c 100644 --- a/gsoc_sntp/kod_management.c +++ b/gsoc_sntp/kod_management.c @@ -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; diff --git a/gsoc_sntp/main.c b/gsoc_sntp/main.c index d35d8c487..c3e03abbf 100644 --- a/gsoc_sntp/main.c +++ b/gsoc_sntp/main.c @@ -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); diff --git a/gsoc_sntp/networking.c b/gsoc_sntp/networking.c index 79ff4246f..ee149f98e 100644 --- a/gsoc_sntp/networking.c +++ b/gsoc_sntp/networking.c @@ -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) { diff --git a/gsoc_sntp/utilities.c b/gsoc_sntp/utilities.c index 722f397c9..d6942c56d 100644 --- a/gsoc_sntp/utilities.c +++ b/gsoc_sntp/utilities.c @@ -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; +} + + + + diff --git a/gsoc_sntp/utilities.h b/gsoc_sntp/utilities.h index db26db83e..8d9575f63 100644 --- a/gsoc_sntp/utilities.h +++ b/gsoc_sntp/utilities.h @@ -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