From: Dave Hart Date: Wed, 26 Aug 2009 00:48:46 +0000 (+0000) Subject: [Bug 1283] default to remembering KoD in sntp X-Git-Tag: NTP_4_2_5P206~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1fad5e492aaa812cfec39f6060e24c0206b70a2d;p=thirdparty%2Fntp.git [Bug 1283] default to remembering KoD in sntp clean up numerous sntp/kod_management.c bugs use all addresses resolved from each DNS name bk: 4a94866eyG-z9Y-DJ7BKTr6NmYwMew --- diff --git a/ChangeLog b/ChangeLog index 805b28efd..54da7b6bb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +* [Bug 1283] default to remembering KoD in sntp. +* clean up numerous sntp/kod_management.c bugs. +* use all addresses resolved from each DNS name in sntp. (4.2.5p205) 2009/08/18 Released by Harlan Stenn * accopt.html typo fixes from Dave Mills. * [Bug 1285] Log ntpq :config/config-from-file events. diff --git a/configure.ac b/configure.ac index f0c067cfe..a201292f5 100644 --- a/configure.ac +++ b/configure.ac @@ -2017,7 +2017,7 @@ AC_ARG_ENABLE( AC_DEFINE_UNQUOTED(DSTMINUTES, $ans, [The number of minutes in a DST adjustment]) AC_MSG_RESULT([$ans]) -AC_MSG_CHECKING([[if ntpd will retry on permanent DNS errors]]) +AC_MSG_CHECKING([[if ntpd will retry permanent DNS failures]]) AC_ARG_ENABLE( [ignore-dns-errors], AS_HELP_STRING( diff --git a/libntp/emalloc.c b/libntp/emalloc.c index 0bdf102a8..8798a3193 100644 --- a/libntp/emalloc.c +++ b/libntp/emalloc.c @@ -6,6 +6,8 @@ #include "ntp_syslog.h" #include "ntp_stdlib.h" +extern char *progname; + #if !defined(_MSC_VER) || !defined(_DEBUG) @@ -20,8 +22,11 @@ erealloc( mem = realloc(prev, size); if (NULL == mem) { - msyslog(LOG_ERR, "fatal out of memory (%u bytes)", - (u_int)size); + msyslog(LOG_ERR, + "fatal out of memory (%u bytes)", (u_int)size); + fprintf(stderr, + "%s: fatal out of memory (%u bytes)", progname, + (u_int)size); exit(1); } @@ -49,9 +54,11 @@ estrdup( if (NULL == copy) { msyslog(LOG_ERR, - "fatal out of memory duplicating %u byte " - "string '%s'", - (u_int)strlen(str) + 1, str); + "fatal out of memory duplicating %u bytes", + (u_int)strlen(str) + 1); + fprintf(stderr, + "%s: fatal out of memory duplicating %u bytes", + progname, (u_int)strlen(str) + 1); exit(1); } @@ -82,8 +89,12 @@ debug_erealloc( mem = _realloc_dbg(prev, size, _NORMAL_BLOCK, file, line); if (NULL == mem) { - msyslog(LOG_ERR, "fatal: out of memory in %s line %d size %u", - file, line, (u_int)size); + msyslog(LOG_ERR, + "fatal: out of memory in %s line %d size %u", + file, line, (u_int)size); + fprintf(stderr, + "%s: fatal: out of memory in %s line %d size %u", + progname, file, line, (u_int)size); exit(1); } @@ -98,9 +109,11 @@ debug_estrdup( ) { char * copy; + size_t bytes; - copy = debug_erealloc(NULL, strlen(str) + 1, file, line); - strcpy(copy, str); + bytes = strlen(str) + 1; + copy = debug_erealloc(NULL, bytes, file, line); + memcpy(copy, str, bytes); return copy; } diff --git a/ntpd/ntp_config.c b/ntpd/ntp_config.c index b02bddc7e..f9fb0de71 100644 --- a/ntpd/ntp_config.c +++ b/ntpd/ntp_config.c @@ -260,10 +260,11 @@ static void config_vars(struct config_tree *); static void config_peers(struct config_tree *); static void config_unpeers(struct config_tree *); -static void config_ntpd(struct config_tree *); #ifdef SIM static void config_sim(struct config_tree *); static void config_ntpdsim(struct config_tree *); +#else +static void config_ntpd(struct config_tree *); #endif enum gnn_type { @@ -2883,14 +2884,9 @@ free_config_qos( ) { struct attr_val *my_qosconfig; - char *s; -#ifdef HAVE_IPTOS_SUPPORT - unsigned int qtos = 0; -#endif while (NULL != (my_qosconfig = dequeue(ptree->qos))) { - s = my_qosconfig->value.s; - free(s); + free(my_qosconfig->value.s); free_node(my_qosconfig); } } @@ -3667,9 +3663,6 @@ free_config_sim( struct config_tree *ptree ) { - server_info *serv_info; - struct attr_val *init_stmt; - if (NULL == ptree->sim_details) return; @@ -3688,7 +3681,7 @@ free_config_sim( * the simulator. The simulator ignores a lot of the standard ntpd configuration * options */ - +#ifndef SIM static void config_ntpd( struct config_tree *ptree @@ -3712,6 +3705,8 @@ config_ntpd( config_fudge(ptree); config_qos(ptree); } +#endif /* !SIM */ + #ifdef SIM static void diff --git a/sntp/configure.ac b/sntp/configure.ac index aba6f52d0..69be79eff 100644 --- a/sntp/configure.ac +++ b/sntp/configure.ac @@ -126,6 +126,7 @@ m4_defun([_LT_AC_LANG_CXX_CONFIG], [:]) m4_defun([_LT_AC_LANG_F77_CONFIG], [:]) AC_PROG_LIBTOOL +NTP_DIR_SEP # Checks for libraries. diff --git a/sntp/crypto.c b/sntp/crypto.c index 5867e0c06..9f2096844 100644 --- a/sntp/crypto.c +++ b/sntp/crypto.c @@ -41,7 +41,7 @@ auth_md5 ( MD5Init(&ctx); - char *digest_data = (char *) malloc(sizeof(char) * (LEN_PKT_NOMAC + cmp_key->key_len)); + char *digest_data = (char *) emalloc(sizeof(char) * (LEN_PKT_NOMAC + cmp_key->key_len)); for(a=0; a +#include +#include #include "kod_management.h" #include "log.h" #include "sntp-opts.h" +#include "ntp_stdlib.h" #define DEBUG -int kod_init = 0, entryc = 0; +int kod_init = 0, kod_db_cnt = 0; const char *kod_db_file; -struct kod_entry *kod_db; -FILE *db_s; +struct kod_entry **kod_db; /* array of pointers to kod_entry */ /* @@ -39,223 +41,220 @@ search_entry ( ) { register int a, b, resc = 0; - struct kod_entry *sptr; - sptr = kod_db; - for (a = 0; a < entryc && sptr; a++) { - if (!strcmp(sptr->hostname, hostname)) + for (a = 0; a < kod_db_cnt; a++) + if (!strcmp(kod_db[a]->hostname, hostname)) resc++; - sptr = sptr->next; - } - if (!resc) return 0; - *dst = malloc(sizeof(struct kod_entry) * resc); - if (NULL == *dst) - return 0; + *dst = emalloc(resc * sizeof(**dst)); - sptr = kod_db; b = 0; - for (a = 0; a < entryc && sptr; a++) { - if (!strcmp(sptr->hostname, hostname)) { - (*dst)[b] = *sptr; - (*dst)[b].next = &((*dst)[b + 1]); + for (a = 0; a < kod_db_cnt; a++) + if (!strcmp(kod_db[a]->hostname, hostname)) { + (*dst)[b] = *kod_db[a]; b++; } - sptr = sptr->next; - } - if (b) - (*dst)[b - 1].next = NULL; - return resc; } -#if 0 /* presently useless */ -int -kod_entry_exists ( - char *search_str - ) + +void +add_entry( + char *hostname, + char *type /* 4 bytes not \0 terminated */ + ) { - struct kod_entry **dummy = NULL; + int n; - if(dummy == NULL) - return 0; + n = kod_db_cnt++; + kod_db = erealloc(kod_db, kod_db_cnt * sizeof(kod_db[0])); + kod_db[n] = emalloc(sizeof(*kod_db[n])); - else - return 1; -} -#endif + kod_db[n]->timestamp = time(NULL); -void -add_entry ( - char *hostname, - char *type - ) -{ - if(kod_init) { - struct kod_entry *new_entry = (struct kod_entry *) malloc(sizeof(struct kod_entry)); - strcpy(new_entry->hostname, hostname); - strncpy(new_entry->type, type, 4); - new_entry->next = NULL; - - kod_db[entryc-1].next = new_entry; - entryc++; - } -} + memcpy(kod_db[n]->type, type, 4); + kod_db[n]->type[sizeof(kod_db[n]->type) - 1] = '\0'; -void -kod_atexit ( - ) -{ - if(kod_init) - write_kod_db(); + strncpy(kod_db[n]->hostname, hostname, + sizeof(kod_db[n]->hostname)); + kod_db[n]->hostname[sizeof(kod_db[n]->hostname) - 1] = '\0'; } + void -delete_entry ( - char *hostname, - char *type - ) +delete_entry( + char *hostname, + char *type + ) { register int a; - struct kod_entry *nptr = kod_db; + for (a = 0; a < kod_db_cnt; a++) + if (!strcmp(kod_db[a]->hostname, hostname) + && !strcmp(kod_db[a]->type, type)) + break; - for(a=0; ahostname, hostname) && !strncmp(nptr->type, type, 4)) { - struct kod_entry *cptr = nptr; - struct kod_entry *nnptr = nptr; + if (a == kod_db_cnt) + return; - nptr--; - nnptr++; + free(kod_db[a]); + kod_db_cnt--; - nptr->next = nnptr; - free(cptr); - nptr = NULL; - } - else { - nptr = nptr->next; - } - } + if (a < kod_db_cnt) + memmove(&kod_db[a], &kod_db[a + 1], + (kod_db_cnt - a) * sizeof(kod_db[0])); } + void -write_kod_db ( - ) +write_kod_db(void) { - if(!kod_init) - return; - + FILE *db_s; + char *pch; + int dirmode; register int a; - struct kod_entry *nptr = kod_db; - if(fopen(kod_db_file, "w") == NULL) { - char msg[80]; - snprintf(msg, 80, "Can't open KOD db file %s for writing!", kod_db_file); + db_s = fopen(kod_db_file, "w"); + + /* + * If opening fails, blindly attempt to create each directory + * in the path first, then retry the open. + */ + if (NULL == db_s && strlen(kod_db_file)) { + dirmode = S_IRUSR | S_IWUSR | S_IXUSR + | S_IRGRP | S_IXGRP + | S_IROTH | S_IXOTH; + pch = strchr(kod_db_file + 1, DIR_SEP); + while (NULL != pch) { + *pch = '\0'; + mkdir(kod_db_file, dirmode); + *pch = DIR_SEP; + pch = strchr(pch + 1, DIR_SEP); + } + } + + db_s = fopen(kod_db_file, "w"); + if (NULL == db_s) { + char msg[80]; + snprintf(msg, sizeof(msg), + "Can't open KOD db file %s for writing!", + kod_db_file); #ifdef DEBUG debug_msg(msg); #endif - log_msg(msg, 2); return; } - for(a=0; ahostname, nptr->timestamp, nptr->type); - nptr = nptr->next; + for (a = 0; a < kod_db_cnt; a++) { + fprintf(db_s, "%16.16llx %s %s\n", (unsigned long long) + kod_db[a]->timestamp, kod_db[a]->type, + kod_db[a]->hostname); } fflush(db_s); fclose(db_s); } - + + void -kod_init_kod_db ( - const char *db_file - ) +kod_init_kod_db( + const char *db_file + ) { - register int a, b; - - /* Max. of 255 characters for hostname, 10 for timestamp, 4 for kisscode, 2 for format : and 1 for \n */ + /* + * Max. of 254 characters for hostname, 10 for timestamp, 4 for + * kisscode, 2 for spaces, 1 for \n, and 1 for \0 + */ char fbuf[272]; + FILE *db_s; + int a, b, sepc; + unsigned long long ull; + char *str_ptr; char error = 0; - if (kod_init) - return; + atexit(write_kod_db); #ifdef DEBUG printf("Initializing KOD DB...\n"); #endif + kod_db_file = estrdup(db_file); + + db_s = fopen(db_file, "r"); - if(db_s == NULL) { + if (NULL == db_s) { char msg[80]; - snprintf(msg, 80, "kod_init_kod_db(): Cannot open KOD db file %s", db_file); - + snprintf(msg, sizeof(msg), "kod_init_kod_db(): Cannot open KoD db file %s", db_file); #ifdef DEBUG debug_msg(msg); printf("%s\n", msg); #endif - log_msg(msg, 2); + + return; } if(ENABLED_OPT(NORMALVERBOSE)) - printf("Starting to read KOD file %s...\n", db_file); + 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(!feof(db_s)) { - int sepc = 0; - fgets(fbuf, sizeof(fbuf), db_s); + + /* ignore blank lines */ + if ('\n' == fbuf[0]) + continue; + sepc = 0; for(a=0; atype, kod_db[b]->hostname)) { - kod_db[b].next = NULL; - if (b > 0) - kod_db[b-1].next = &kod_db[b]; + free(kod_db[b]); + kod_db[b] = NULL; + error = 1; + break; + } + + kod_db[b]->timestamp = (time_t)ull; } if (ferror(db_s) || error) { char msg[80]; - snprintf(msg, sizeof(msg), "An error occured while parsing the KOD db file %s", db_file); + snprintf(msg, sizeof(msg), "An error occured while parsing the KoD db file %s", db_file); #ifdef DEBUG debug_msg(msg); #endif @@ -286,18 +295,15 @@ kod_init_kod_db ( } #ifdef DEBUG - for(a=0; ahostname, + (unsigned long long)kod_db[a]->timestamp, + kod_db[a]->type); printf("\n"); #endif fclose(db_s); - - kod_db_file = db_file; - - kod_init = 1; - atexit(kod_atexit); } diff --git a/sntp/kod_management.h b/sntp/kod_management.h index 3629dbd37..239c078cd 100644 --- a/sntp/kod_management.h +++ b/sntp/kod_management.h @@ -18,21 +18,20 @@ #ifndef KOD_MANAGEMENT_H #define KOD_MANAGEMENT_H +#include + struct kod_entry { char hostname[255]; - unsigned int timestamp; + time_t timestamp; char type[5]; - struct kod_entry *next; }; -int search_entry (char *hostname, struct kod_entry **dst); -int kod_entry_exists (char *search_str); +int search_entry(char *hostname, struct kod_entry **dst); -void add_entry (char *hostname, char *type); -void delete_entry (char *hostname, char *type); -void kod_init_kod_db (const char *db_file); -void write_kod_db (void); -void kod_atexit (void); +void add_entry(char *hostname, char *type); +void delete_entry(char *hostname, char *type); +void kod_init_kod_db(const char *db_file); +void write_kod_db(void); #endif diff --git a/sntp/main.c b/sntp/main.c index 39353c42e..552f0e558 100644 --- a/sntp/main.c +++ b/sntp/main.c @@ -15,6 +15,7 @@ #include "utilities.h" #include "log.h" +char *progname = "sntp"; /* for msyslog */ int ai_fam_pref; volatile int debug; @@ -28,16 +29,6 @@ int on_wire (struct addrinfo *host); int set_time (double offset); -#if !HAVE_MALLOC -void * -rpl_malloc (size_t n) -{ - if (n == 0) - n = 1; - return malloc (n); -} -#endif /* !HAVE_MALLOC */ - int main ( int argc, @@ -61,7 +52,11 @@ sntp_main ( int optct; int sync_data_suc = 0; struct addrinfo **resh = NULL; + struct addrinfo *ai; int resc; + int kodc; + int ow_ret; + char *hostname; /* IPv6 available? */ if (isc_net_probeipv6() != ISC_R_SUCCESS) { @@ -92,15 +87,33 @@ sntp_main ( if (HAVE_OPT(FILELOG)) init_log(OPT_ARG(FILELOG)); - /* If there's a specified KOD file init KOD system. - * If not and system may save to HD use default file. + /* + * If there's a specified KOD file init KOD system. If not use + * default file. For embedded systems with no writable + * filesystem, -K /dev/null can be used to disable KoD storage. */ if (HAVE_OPT(KOD)) kod_init_kod_db(OPT_ARG(KOD)); + else + kod_init_kod_db("/var/db/ntp-kod"); if (HAVE_OPT(KEYFILE)) auth_init(OPT_ARG(KEYFILE), &keys); +#ifdef EXERCISE_KOD_DB + add_entry("192.168.169.170", "DENY"); + add_entry("192.168.169.171", "DENY"); + add_entry("192.168.169.172", "DENY"); + add_entry("192.168.169.173", "DENY"); + add_entry("192.168.169.174", "DENY"); + delete_entry("192.168.169.174", "DENY"); + delete_entry("192.168.169.172", "DENY"); + delete_entry("192.168.169.170", "DENY"); + if ((kodc = search_entry("192.168.169.173", &reason)) == 0) + printf("entry for 192.168.169.173 not found but should have been!\n"); + else + free(reason); +#endif /* Considering employing a variable that prevents functions of doing anything until * everything is initialized properly @@ -116,29 +129,27 @@ sntp_main ( * let's just pay attention to previous KoDs. */ for (c = 0; c < resc && !sync_data_suc; c++) { - getnameinfo(resh[c]->ai_addr, resh[c]->ai_addrlen, adr_buf, - sizeof(adr_buf), NULL, 0, NI_NUMERICHOST); - - int kodc; - char *hostname = addrinfo_to_str(resh[c]); - - if ((kodc = search_entry(hostname, &reason)) == 0) { - if (is_reachable(resh[c])) { - int ow_ret = on_wire(resh[c]); - - if (ow_ret < 0) - printf("on_wire failed for server %s!\n", hostname); - else - sync_data_suc = 1; + ai = resh[c]; + do { + hostname = addrinfo_to_str(ai); + + if ((kodc = search_entry(hostname, &reason)) == 0) { + if (is_reachable(ai)) { + ow_ret = on_wire(ai); + if (ow_ret < 0) + printf("on_wire failed for server %s!\n", hostname); + else + sync_data_suc = 1; + } + } else { + printf("%d prior KoD%s for %s, skipping.\n", + kodc, (kodc > 1) ? "s" : "", hostname); + free(reason); } - } else { - printf("KoD %i packages exists for %s, stopping any further communication.\n", - kodc, adr_buf); - free(reason); - } - + free(hostname); + ai = ai->ai_next; + } while (NULL != ai); freeaddrinfo(resh[c]); - free(hostname); } free(resh); @@ -153,8 +164,9 @@ on_wire ( { register int try; SOCKET sock; - struct pkt *x_pkt = (struct pkt *) alloca(sizeof(struct pkt)); - struct pkt *r_pkt = (struct pkt *) alloca(sizeof(struct pkt)); + struct pkt x_pkt; + struct pkt r_pkt; + char *ref; for(try=0; try<5; try++) { struct timeval tv_xmt, tv_dst; @@ -163,8 +175,8 @@ on_wire ( char *hostname = NULL, *ts_str = NULL; l_fp p_rec, p_xmt, p_ref, p_org, xmt, tmp, dst; - memset(r_pkt, 0, sizeof(*r_pkt)); - memset(x_pkt, 0, sizeof(*x_pkt)); + memset(&r_pkt, 0, sizeof(r_pkt)); + memset(&x_pkt, 0, sizeof(x_pkt)); error = GETTIMEOFDAY(&tv_xmt, (struct timezone *)NULL); @@ -176,17 +188,17 @@ on_wire ( #endif TVTOTS(&tv_xmt, &xmt); - HTONL_FP(&xmt, &(x_pkt->xmt)); + HTONL_FP(&xmt, &(x_pkt.xmt)); - x_pkt->stratum = STRATUM_TO_PKT(STRATUM_UNSPEC); - x_pkt->ppoll = 8; + 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); + set_li_vn_mode(&x_pkt, LEAP_NOTINSYNC, 4, 3); create_socket(&sock, (sockaddr_u *)host->ai_addr); - sendpkt(sock, (sockaddr_u *)host->ai_addr, x_pkt, LEN_PKT_NOMAC); - rpktl = recvpkt(sock, r_pkt, x_pkt); + sendpkt(sock, (sockaddr_u *)host->ai_addr, &x_pkt, LEN_PKT_NOMAC); + rpktl = recvpkt(sock, &r_pkt, &x_pkt); closesocket(sock); @@ -209,16 +221,19 @@ on_wire ( case KOD_DEMOBILIZE: /* Received a DENY or RESTR KOD packet */ hostname = addrinfo_to_str(host); - add_entry(hostname, (char *) &r_pkt->refid); + ref = (char *)&r_pkt.refid; + add_entry(hostname, ref); if(ENABLED_OPT(NORMALVERBOSE)) - printf("sntp on_wire: Received KOD packet with code: %s from %s, demobilizing all connections\n", - (char *) r_pkt->refid, hostname); + printf("sntp on_wire: Received KOD packet with code: %c%c%c%c from %s, demobilizing all connections\n", + ref[0], ref[1], ref[2], ref[3], + hostname); - char *log_str = (char *) malloc(sizeof(char) * (INET6_ADDRSTRLEN + 72)); - snprintf(log_str, sizeof(log_str), - "Received a KOD packet with code %s from %s, demobilizing all connections", - (char *) &r_pkt->refid, hostname); + char *log_str = (char *) emalloc(sizeof(char) * (INET6_ADDRSTRLEN + 72)); + snprintf(log_str, INET6_ADDRSTRLEN + 72, + "Received a KOD packet with code %c%c%c%c from %s, demobilizing all connections", + ref[0], ref[1], ref[2], ref[3], + hostname); log_msg(log_str, 2); @@ -232,10 +247,10 @@ on_wire ( case 1: /* Convert timestamps from network to host byte order */ - 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); + 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, @@ -245,20 +260,20 @@ on_wire ( } #ifdef DEBUG - pkt_output(r_pkt, rpktl, stdout); + pkt_output(&r_pkt, rpktl, stdout); - printf("sntp on_wire: rpkt->reftime:\n"); - l_fp_output(&(r_pkt->reftime), stdout); - printf("sntp on_wire: rpkt->org:\n"); - l_fp_output(&(r_pkt->org), stdout); - printf("sntp on_wire: rpkt->rec:\n"); - l_fp_output(&(r_pkt->rec), stdout); - printf("sntp on_wire: rpkt->rec:\n"); - l_fp_output_bin(&(r_pkt->rec), stdout); - printf("sntp on_wire: rpkt->rec:\n"); - l_fp_output_dec(&(r_pkt->rec), stdout); - printf("sntp on_wire: rpkt->xmt:\n"); - l_fp_output(&(r_pkt->xmt), stdout); + printf("sntp on_wire: r_pkt.reftime:\n"); + l_fp_output(&(r_pkt.reftime), stdout); + printf("sntp on_wire: r_pkt.org:\n"); + l_fp_output(&(r_pkt.org), stdout); + printf("sntp on_wire: r_pkt.rec:\n"); + l_fp_output(&(r_pkt.rec), stdout); + printf("sntp on_wire: r_pkt.rec:\n"); + l_fp_output_bin(&(r_pkt.rec), stdout); + printf("sntp on_wire: r_pkt.rec:\n"); + l_fp_output_dec(&(r_pkt.rec), stdout); + printf("sntp on_wire: r_pkt.xmt:\n"); + l_fp_output(&(r_pkt.xmt), stdout); #endif /* Compute offset etc. */ diff --git a/sntp/netutils.c b/sntp/netutils.c index b6d1d0f17..185250573 100644 --- a/sntp/netutils.c +++ b/sntp/netutils.c @@ -112,7 +112,7 @@ void compile_header ( char **dst ) { - unsigned char *comph = (unsigned char *) malloc(sizeof(char) * 12); + unsigned char *comph = (unsigned char *) emalloc(sizeof(char) * 12); /* When declaring id_high unsigned char, will the char cast on the right side * convert to unsigned char, too? @@ -201,7 +201,7 @@ size_t compile_query ( length += 4; - char *res = (char *) malloc(sizeof(char) * length); + char *res = (char *) emalloc(sizeof(char) * length); for(a=0; a 0 && a%8 == 0) + 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, "%d: %x \t", a, pkt[a]); } fprintf(output, "\n"); fprintf(output, HLINE); - - free(cpy); } /* Output a long floating point value in hex in the style described above @@ -138,7 +130,7 @@ addrinfo_to_str ( struct addrinfo *addr ) { - char *buf = (char *) malloc(sizeof(char) * INET6_ADDRSTRLEN); + char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN); getnameinfo(addr->ai_addr, addr->ai_addrlen, buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); @@ -156,7 +148,7 @@ ss_to_str ( sockaddr_u *saddr ) { - char *buf = (char *) malloc(sizeof(char) * INET6_ADDRSTRLEN); + char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN); getnameinfo(&saddr->sa, SOCKLEN(saddr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); @@ -177,13 +169,10 @@ tv_to_str ( "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - char *buf = (char *) malloc(sizeof(char) * 48); + char *buf = (char *) emalloc(sizeof(char) * 48); time_t cur_time = time(NULL); struct tm *tm_ptr; - if (NULL == buf) - return "tv_to_str() malloc(48) failed"; - tm_ptr = localtime(&cur_time); diff --git a/sntp/utilities.h b/sntp/utilities.h index 78962f403..604ac69ef 100644 --- a/sntp/utilities.h +++ b/sntp/utilities.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include