From: Dave Hart Date: Wed, 10 Nov 2010 02:29:39 +0000 (+0000) Subject: Backport sntp changes from -dev X-Git-Tag: NTP_4_2_6P3_RC9~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e6c25d10fe2f8a24799335f6231fdbe75a3e05c;p=thirdparty%2Fntp.git Backport sntp changes from -dev bk: 4cda0393uZFUH3m8jSJLEE9i932y3g --- diff --git a/m4/ntp_lib_m.m4 b/m4/ntp_lib_m.m4 new file mode 100644 index 000000000..582051a52 --- /dev/null +++ b/m4/ntp_lib_m.m4 @@ -0,0 +1,16 @@ +dnl ###################################################################### +dnl What do we need for math libraries? +AC_DEFUN([NTP_LIB_M], [ +LIBM= +case "$host" in + *-*-darwin*) + ;; + *) + _libs=$LIBS + AC_SEARCH_LIBS(cos, m, LIBM="-lm") + LIBS=$_libs + ;; +esac +AC_SUBST([LIBM]) +]) +dnl ====================================================================== diff --git a/sntp/Makefile.am b/sntp/Makefile.am index ad55a55d2..bb0952f9b 100644 --- a/sntp/Makefile.am +++ b/sntp/Makefile.am @@ -10,7 +10,7 @@ AM_CPPFLAGS= $(LIBOPTS_CFLAGS) -I$(top_srcdir)/../include \ -I$(top_srcdir)/../lib/isc/nothreads/include \ -I$(top_srcdir)/../lib/isc/unix/include -LDADD= $(LIBOPTS_LDADD) -lm ../libntp/libntp.a @LCRYPTO@ +LDADD= $(LIBOPTS_LDADD) $(LIBM) ../libntp/libntp.a @LCRYPTO@ run_ag= cd $(srcdir) && env PATH="$(abs_builddir):$(PATH)" \ autogen -L ../include --writable @@ -39,8 +39,10 @@ sntp_SOURCES = \ log.c \ log.h \ main.c \ + main.h \ networking.c \ networking.h \ + sntp.c \ sntp-opts.c \ sntp-opts.h \ utilities.c \ diff --git a/sntp/configure.ac b/sntp/configure.ac index 0eee100db..6ff842f46 100644 --- a/sntp/configure.ac +++ b/sntp/configure.ac @@ -134,6 +134,7 @@ AC_PROG_LIBTOOL NTP_DIR_SEP # Checks for libraries. +NTP_LIB_M AC_CHECK_FUNC([gethostent], , [AC_SEARCH_LIBS([gethostent], [nsl], , , [$libxnet -lsocket])]) diff --git a/sntp/crypto.c b/sntp/crypto.c index c8fb85e7f..0e582a9a2 100644 --- a/sntp/crypto.c +++ b/sntp/crypto.c @@ -1,3 +1,4 @@ +#include #include "crypto.h" #include diff --git a/sntp/header.h b/sntp/header.h index 4e3fa9661..21805f195 100644 --- a/sntp/header.h +++ b/sntp/header.h @@ -24,7 +24,7 @@ No changes should be needed for any system that is even remotely like Unix. */ # define SAVENAME "/etc/sntp.state" /* Stores the recovery state */ #endif -#define DEBUG +//#define DEBUG diff --git a/sntp/kod_management.c b/sntp/kod_management.c index 4bdb4e8d9..dfc989d14 100644 --- a/sntp/kod_management.c +++ b/sntp/kod_management.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,7 +7,7 @@ #include "log.h" #include "sntp-opts.h" #include "ntp_stdlib.h" -#define DEBUG +//#define DEBUG int kod_init = 0, kod_db_cnt = 0; const char *kod_db_file; diff --git a/sntp/log.c b/sntp/log.c index 52d9fd25d..5b39acc79 100644 --- a/sntp/log.c +++ b/sntp/log.c @@ -1,3 +1,4 @@ +#include #include "log.h" #include "sntp-opts.h" @@ -15,7 +16,7 @@ void log_msg(char *message, int type) { fprintf(log_file, "%s: %s\n", timestamp, message); fflush(log_file); } else { - syslog(type, message); + syslog(type, "%s", message); } } @@ -31,7 +32,7 @@ void debug_msg(char *message) { #ifdef LOG_PERROR | LOG_PERROR #endif - | LOG_CONS, message); + | LOG_CONS, "%s", message); } } diff --git a/sntp/main.c b/sntp/main.c index 23ccd4c12..b32800ced 100644 --- a/sntp/main.c +++ b/sntp/main.c @@ -1,24 +1,11 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "crypto.h" +#include "main.h" + #include "kod_management.h" #include "networking.h" #include "utilities.h" #include "log.h" -char *progname = "sntp"; /* for msyslog */ - int ai_fam_pref = AF_UNSPEC; -volatile int debug; struct key *keys = NULL; @@ -40,15 +27,6 @@ do { \ } while (0) -int -main ( - int argc, - char **argv - ) -{ - return sntp_main(argc, argv); -} - /* * The actual main function. */ @@ -178,6 +156,187 @@ static union { #define r_pkt rbuf.pkt +int +generate_pkt ( + struct pkt *x_pkt, + const struct timeval *tv_xmt, + int key_id, + struct key *pkt_key + ) +{ + l_fp xmt; + int pkt_len = LEN_PKT_NOMAC; + memset(x_pkt, 0, sizeof(struct pkt)); + TVTOTS(tv_xmt, &xmt); + HTONL_FP(&xmt, &(x_pkt->xmt)); + 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); + if (pkt_key != NULL) { + int mac_size = 20; /* max room for MAC */ + x_pkt->exten[0] = htonl(key_id); + mac_size = make_mac((char *)x_pkt, pkt_len, mac_size, pkt_key, (char *)&x_pkt->exten[1]); + if (mac_size) + pkt_len += mac_size + 4; + } + return pkt_len; +} + +int +handle_pkt ( + int rpktl, + struct pkt *rpkt, + struct addrinfo *host + ) +{ + struct timeval tv_dst; + int sw_case, digits; + char *hostname = NULL, *log_str, *ref, *ts_str = NULL; + double offset, precision, root_dispersion; + char addr_buf[INET6_ADDRSTRLEN]; + + if(rpktl > 0) + sw_case = 1; + else + sw_case = rpktl; + + switch(sw_case) { + case SERVER_UNUSEABLE: + return -1; + break; + + case PACKET_UNUSEABLE: + break; + + case SERVER_AUTH_FAIL: + break; + + case KOD_DEMOBILIZE: + /* Received a DENY or RESTR KOD packet */ + hostname = addrinfo_to_str(host); + ref = (char *)&rpkt->refid; + add_entry(hostname, ref); + + if (ENABLED_OPT(NORMALVERBOSE)) + printf("sntp handle_pkt: Received KOD packet with code: %c%c%c%c from %s, demobilizing all connections\n", + ref[0], ref[1], ref[2], ref[3], + hostname); + + log_str = emalloc(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, LOG_WARNING | LOG_CONS); + free(log_str); + break; + + case KOD_RATE: + /* Hmm... probably we should sleep a bit here */ + break; + + case 1: + if (ENABLED_OPT(NORMALVERBOSE)) { + getnameinfo(host->ai_addr, host->ai_addrlen, addr_buf, + sizeof(addr_buf), NULL, 0, NI_NUMERICHOST); + printf("sntp handle_pkt: Received %i bytes from %s\n", rpktl, addr_buf); + } + + GETTIMEOFDAY(&tv_dst, (struct timezone *)NULL); + tv_dst.tv_sec += JAN_1970; + + offset_calculation(rpkt, rpktl, &tv_dst, &offset, &precision, &root_dispersion); + + for (digits = 0; (precision *= 10.) < 1.; ++digits) + /* empty */ ; + if (digits > 6) + digits = 6; + + ts_str = tv_to_str(&tv_dst); + printf("%s ", ts_str); + if(offset > 0) + printf("+"); + printf("%.*f", digits, offset); + if (root_dispersion > 0.) + printf(" +/- %f secs", root_dispersion); + printf("\n"); + free(ts_str); + + if(ENABLED_OPT(SETTOD) || ENABLED_OPT(ADJTIME)) + return set_time(offset); + + return 0; + } + + return 1; +} + +void +offset_calculation ( + struct pkt *rpkt, + int rpktl, + struct timeval *tv_dst, + double *offset, + double *precision, + double *root_dispersion + ) +{ + l_fp p_rec, p_xmt, p_ref, p_org, tmp, dst; + u_fp p_rdly, p_rdsp; + double t21, t34, delta; + + /* Convert timestamps from network to host byte order */ + p_rdly = NTOHS_FP(rpkt->rootdelay); + p_rdsp = NTOHS_FP(rpkt->rootdisp); + NTOHL_FP(&rpkt->reftime, &p_ref); + NTOHL_FP(&rpkt->org, &p_org); + NTOHL_FP(&rpkt->rec, &p_rec); + NTOHL_FP(&rpkt->xmt, &p_xmt); + + *precision = LOGTOD(rpkt->precision); +#ifdef DEBUG + printf("sntp precision: %f\n", *precision); +#endif /* DEBUG */ + + *root_dispersion = FPTOD(p_rdsp); + +#ifdef DEBUG + printf("sntp rootdelay: %f\n", FPTOD(p_rdly)); + printf("sntp rootdisp: %f\n", *root_dispersion); + + pkt_output(rpkt, rpktl, stdout); + + printf("sntp offset_calculation: rpkt->reftime:\n"); + l_fp_output(&(rpkt->reftime), stdout); + printf("sntp offset_calculation: rpkt->org:\n"); + l_fp_output(&(rpkt->org), stdout); + printf("sntp offset_calculation: rpkt->rec:\n"); + l_fp_output(&(rpkt->rec), stdout); + printf("sntp offset_calculation: rpkt->rec:\n"); + l_fp_output_bin(&(rpkt->rec), stdout); + printf("sntp offset_calculation: rpkt->rec:\n"); + l_fp_output_dec(&(rpkt->rec), stdout); + printf("sntp offset_calculation: rpkt->xmt:\n"); + l_fp_output(&(rpkt->xmt), stdout); +#endif + + /* Compute offset etc. */ + tmp = p_rec; + L_SUB(&tmp, &p_org); + LFPTOD(&tmp, t21); + TVTOTS(tv_dst, &dst); + tmp = p_xmt; + L_SUB(&tmp, &dst); + LFPTOD(&tmp, t34); + *offset = (t21 + t34) / 2.; + delta = t21 - t34; + + if(ENABLED_OPT(NORMALVERBOSE)) + printf("sntp offset_calculation:\tt21: %.6f\t\t t34: %.6f\n\t\tdelta: %.6f\t offset: %.6f\n", + t21, t34, delta, *offset); +} + /* The heart of (S)NTP, exchange NTP packets and compute values to correct the local clock */ int on_wire ( @@ -189,8 +348,6 @@ on_wire ( char addr_buf[INET6_ADDRSTRLEN]; register int try; SOCKET sock; - struct pkt x_pkt; - char *ref; struct key *pkt_key = NULL; int key_id = 0; @@ -199,17 +356,12 @@ on_wire ( get_key(key_id, &pkt_key); } for(try=0; try<5; try++) { - struct timeval tv_xmt, tv_dst; - double t21, t34, delta, offset, precision, root_dispersion; - int digits, error, rpktl, sw_case; - char *hostname = NULL, *ts_str = NULL; - char *log_str; - u_fp p_rdly, p_rdsp; - l_fp p_rec, p_xmt, p_ref, p_org, xmt, tmp, dst; + struct timeval tv_xmt; + struct pkt x_pkt; + int error, rpktl, handle_pkt_res; memset(&r_pkt, 0, sizeof rbuf); - memset(&x_pkt, 0, sizeof(x_pkt)); - + error = GETTIMEOFDAY(&tv_xmt, (struct timezone *)NULL); tv_xmt.tv_sec += JAN_1970; @@ -223,144 +375,17 @@ on_wire ( rpktl = recv_bcst_pkt(sock, &r_pkt, sizeof rbuf, (sockaddr_u *)bcast->ai_addr); closesocket(sock); } else { - int pkt_len = LEN_PKT_NOMAC; - TVTOTS(&tv_xmt, &xmt); - HTONL_FP(&xmt, &(x_pkt.xmt)); - 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); - if (pkt_key != NULL) { - int mac_size = 20; /* max room for MAC */ - x_pkt.exten[0] = htonl(key_id); - mac_size = make_mac((char *)&x_pkt, pkt_len, mac_size, pkt_key, (char *)&x_pkt.exten[1]); - if (mac_size) - pkt_len += mac_size + 4; - } + int pkt_len = generate_pkt(&x_pkt, &tv_xmt, key_id, pkt_key); + create_socket(&sock, (sockaddr_u *)host->ai_addr); sendpkt(sock, (sockaddr_u *)host->ai_addr, &x_pkt, pkt_len); rpktl = recvpkt(sock, &r_pkt, sizeof rbuf, &x_pkt); closesocket(sock); } - if(rpktl > 0) - sw_case = 1; - else - sw_case = rpktl; - - switch(sw_case) { - case SERVER_UNUSEABLE: - return -1; - break; - - case PACKET_UNUSEABLE: - break; - - case SERVER_AUTH_FAIL: - break; - - case KOD_DEMOBILIZE: - /* Received a DENY or RESTR KOD packet */ - hostname = addrinfo_to_str(host); - ref = (char *)&r_pkt.refid; - add_entry(hostname, ref); - - if (ENABLED_OPT(NORMALVERBOSE)) - 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); - - log_str = emalloc(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, LOG_WARNING | LOG_CONS); - free(log_str); - break; - - case KOD_RATE: - /* Hmm... probably we should sleep a bit here */ - break; - - case 1: - /* Convert timestamps from network to host byte order */ - p_rdly = NTOHS_FP(r_pkt.rootdelay); - p_rdsp = NTOHS_FP(r_pkt.rootdisp); - 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, addr_buf, - sizeof(addr_buf), NULL, 0, NI_NUMERICHOST); - printf("sntp on_wire: Received %i bytes from %s\n", rpktl, addr_buf); - } - - precision = LOGTOD(r_pkt.precision); -#ifdef DEBUG - printf("sntp precision: %f\n", precision); -#endif /* DEBUG */ - for (digits = 0; (precision *= 10.) < 1.; ++digits) - /* empty */ ; - if (digits > 6) - digits = 6; - - root_dispersion = FPTOD(p_rdsp); - -#ifdef DEBUG - printf("sntp rootdelay: %f\n", FPTOD(p_rdly)); - printf("sntp rootdisp: %f\n", root_dispersion); - - pkt_output(&r_pkt, rpktl, 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. */ - GETTIMEOFDAY(&tv_dst, (struct timezone *)NULL); - tv_dst.tv_sec += JAN_1970; - tmp = p_rec; - L_SUB(&tmp, &p_org); - LFPTOD(&tmp, t21); - TVTOTS(&tv_dst, &dst); - tmp = p_xmt; - L_SUB(&tmp, &dst); - LFPTOD(&tmp, t34); - offset = (t21 + t34) / 2.; - delta = t21 - t34; - - if(ENABLED_OPT(NORMALVERBOSE)) - printf("sntp on_wire:\tt21: %.6f\t\t t34: %.6f\n\t\tdelta: %.6f\t offset: %.6f\n", - t21, t34, delta, offset); - - ts_str = tv_to_str(&tv_dst); - printf("%s ", ts_str); - if(offset > 0) - printf("+"); - printf("%.*f", digits, offset); - if (root_dispersion > 0.) - printf(" +/- %f secs", root_dispersion); - printf("\n"); - free(ts_str); - - if(ENABLED_OPT(SETTOD) || ENABLED_OPT(ADJTIME)) - return set_time(offset); - - return 0; - } + handle_pkt_res = handle_pkt(rpktl, &r_pkt, host); + if (handle_pkt_res < 1) + return handle_pkt_res; } getnameinfo(host->ai_addr, host->ai_addrlen, addr_buf, sizeof(addr_buf), NULL, 0, NI_NUMERICHOST); diff --git a/sntp/main.h b/sntp/main.h new file mode 100644 index 000000000..cbdeda2db --- /dev/null +++ b/sntp/main.h @@ -0,0 +1,29 @@ +#ifndef MAIN_H +#define MAIN_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "crypto.h" + +void set_li_vn_mode (struct pkt *spkt, char leap, char version, char mode); +int sntp_main (int argc, char **argv); +int generate_pkt (struct pkt *x_pkt, const struct timeval *tv_xmt, + int key_id, struct key *pkt_key); +int handle_pkt (int rpktl, struct pkt *rpkt, struct addrinfo *host); +void offset_calculation (struct pkt *rpkt, int rpktl, struct timeval *tv_dst, + double *offset, double *precision, + double *root_dispersion); +int on_wire (struct addrinfo *host, struct addrinfo *bcastaddr); +int set_time (double offset); + +#endif /* MAIN_H */ diff --git a/sntp/networking.c b/sntp/networking.c index d1d988870..07928083e 100644 --- a/sntp/networking.c +++ b/sntp/networking.c @@ -281,12 +281,13 @@ recv_bcst_data ( return recv_bytes; } -static int +int process_pkt ( struct pkt *rpkt, sockaddr_u *sas, int pkt_len, int mode, + struct pkt *spkt, char * func_name ) { @@ -425,6 +426,23 @@ unusable: printf("sntp %s: Server not in sync, skipping this server\n", func_name); return SERVER_UNUSEABLE; } + + /* + * Decode the org timestamp and make sure we're getting a response + * to our last request, but only if we're not in broadcast mode. + */ +#ifdef DEBUG + printf("rpkt->org:\n"); + l_fp_output(&rpkt->org, stdout); + printf("spkt->xmt:\n"); + l_fp_output(&spkt->xmt, stdout); +#endif + if (mode != MODE_BROADCAST && !L_ISEQU(&rpkt->org, &spkt->xmt)) { + if (ENABLED_OPT(NORMALVERBOSE)) + printf("sntp process_pkt: pkt.org and peer.xmt differ\n"); + return PACKET_UNUSEABLE; + } + return pkt_len; } @@ -441,7 +459,7 @@ recv_bcst_pkt ( if (pkt_len < 0) { return BROADCAST_FAILED; } - pkt_len = process_pkt(rpkt, sas, pkt_len, MODE_BROADCAST, "recv_bcst_pkt"); + pkt_len = process_pkt(rpkt, sas, pkt_len, MODE_BROADCAST, NULL, "recv_bcst_pkt"); return pkt_len; } @@ -487,24 +505,8 @@ recvpkt ( } pkt_len = recvdata(rsock, &sender, (char *)rpkt, rsize); if (pkt_len > 0) - pkt_len = process_pkt(rpkt, &sender, pkt_len, MODE_SERVER, "recvpkt"); - if (pkt_len < 0) - return pkt_len; - /* - * Decode the org timestamp and make sure we're getting a response - * to our last request. - */ -#ifdef DEBUG - printf("rpkt->org:\n"); - l_fp_output(&rpkt->org, stdout); - printf("spkt->xmt:\n"); - l_fp_output(&spkt->xmt, stdout); -#endif - if (!L_ISEQU(&rpkt->org, &spkt->xmt)) { - if (ENABLED_OPT(NORMALVERBOSE)) - printf("sntp recvpkt: pkt.org and peer.xmt differ\n"); - return PACKET_UNUSEABLE; - } + pkt_len = process_pkt(rpkt, &sender, pkt_len, MODE_SERVER, spkt, "recvpkt"); + return pkt_len; } diff --git a/sntp/networking.h b/sntp/networking.h index d09531b49..8cb72c44d 100644 --- a/sntp/networking.h +++ b/sntp/networking.h @@ -24,6 +24,14 @@ #include "sntp-opts.h" #include "utilities.h" +/* + * for 4.2.6 only define AUTOKEY if OPENSSL, so that backported 4.2.7 + * references to AUTOKEY work -- in 4.2.7 AUTOKEY is independent of OPENSSL + */ +#ifdef OPENSSL +#define AUTOKEY +#endif + /* FIXME To be replaced by the constants in ntp.h */ #define SERVER_UNUSEABLE -1 /* Skip server */ #define PACKET_UNUSEABLE -2 /* Discard packet and try to get a useable packet again if not tried too often */ @@ -50,6 +58,8 @@ int recv_bcst_data (SOCKET rsock, char *rdata, int rdata_len, sockaddr_u *sas, s int recv_bcst_pkt (SOCKET rsock, struct pkt *rpkt, unsigned int rsize, sockaddr_u *sas); +int process_pkt (struct pkt *rpkt, sockaddr_u *sas, int pkt_len, int mode, struct pkt *spkt, char * func_name); + /* Shortened peer structure. Not absolutely necessary yet */ struct speer { struct speer *next; @@ -71,7 +81,7 @@ struct speer { l_fp reftime; keyid_t keyid; -#ifdef OPENSSL +#ifdef AUTOKEY #define clear_to_zero opcode u_int32 opcode; /* last request opcode */ associd_t assoc; /* peer association ID */ @@ -99,9 +109,9 @@ struct speer { int keynumber; /* current key number */ struct value encrypt; /* send encrypt values */ struct value sndval; /* send autokey values */ -#else /* OPENSSL */ +#else /* !AUTOKEY follows */ #define clear_to_zero status -#endif /* OPENSSL */ +#endif /* !AUTOKEY */ l_fp rec; /* receive time stamp */ l_fp xmt; /* transmit time stamp */ diff --git a/sntp/sntp.c b/sntp/sntp.c new file mode 100644 index 000000000..28101a03f --- /dev/null +++ b/sntp/sntp.c @@ -0,0 +1,13 @@ +#include "main.h" + +char *progname = "sntp"; /* for msyslog */ +volatile int debug; + +int +main ( + int argc, + char **argv + ) +{ + return sntp_main(argc, argv); +} diff --git a/sntp/utilities.c b/sntp/utilities.c index f8588cff2..6180ecb7a 100644 --- a/sntp/utilities.c +++ b/sntp/utilities.c @@ -1,3 +1,4 @@ +#include #include "utilities.h" /* Display a NTP packet in hex with leading address offset @@ -110,15 +111,15 @@ l_fp_output_dec ( */ char * addrinfo_to_str ( - struct addrinfo *addr - ) + struct addrinfo *addr + ) { - char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN); - - getnameinfo(addr->ai_addr, addr->ai_addrlen, buf, - INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); + sockaddr_u s; + + memset(&s, 0, sizeof(s)); + memcpy(&s, addr->ai_addr, min(sizeof(s), addr->ai_addrlen)); - return buf; + return ss_to_str(&s); } /* Convert a sockaddr_u to a string containing the address in @@ -128,14 +129,13 @@ addrinfo_to_str ( */ char * ss_to_str ( - sockaddr_u *saddr - ) + sockaddr_u *saddr + ) { - char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN); - - getnameinfo(&saddr->sa, SOCKLEN(saddr), buf, - INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); - + char * buf; + + buf = emalloc(INET6_ADDRSTRLEN); + strncpy(buf, stoa(saddr), INET6_ADDRSTRLEN); return buf; }