Index: src/util.h =================================================================== --- src/util.h (revision 2535) +++ src/util.h (working copy) @@ -42,6 +42,10 @@ #define IFCONF_BUFFSIZE 16384 #define IFCONF_BUFFMAXSIZE 1048576 +#ifndef GETHOSTNAME_DELAY +#define GETHOSTNAME_DELAY 120 +#endif + struct configfile { char *label; off_t seek; @@ -106,6 +110,8 @@ extern int IfaceSetFlag(const char *ifname, int value) ssize_t GetDataAddrs(int sock, void *dbuf, size_t dbufsize, struct sockaddr_storage *peer, socklen_t peer_len, struct u_addr *addr); + +extern char* GetHostName(void); #ifndef HAVE_NTOA_R extern char *ether_ntoa_r(const struct ether_addr *n, char *a); Index: src/util.c =================================================================== --- src/util.c (revision 2535) +++ src/util.c (working copy) @@ -25,6 +25,7 @@ #include #include #include +#include /* * DEFINITIONS @@ -1623,4 +1624,43 @@ int GetLocalSocket(void) if (gLocalSocket < 0) gLocalSocket = socket(PF_LOCAL, socktype(SOCK_DGRAM), 0); return(gLocalSocket); +} + +/* + * Cache hostname and avoid frequent system calls + * but catch up with changes eventually. + */ +char* GetHostName(void) +{ + static const char *amnesiac = "NONAME"; + static char *hostname = NULL; + static long namelen; + static time_t was; + time_t now; + + if (hostname == NULL) { + namelen = sysconf(_SC_HOST_NAME_MAX); + if (namelen <= 0) + namelen = MAXHOSTNAMELEN; + /* add space for terminating zero */ + namelen++; + hostname = Malloc(MB_UTIL, namelen); + time(&was); + + if (gethostname(hostname, namelen) < 0) + return (__DECONST(char*, amnesiac)); + return (hostname); + } + +#if GETHOSTNAME_DELAY != 0 + time(&now); + if (now - was < GETHOSTNAME_DELAY) + return (hostname); + + was = now; +#endif + + if (gethostname(hostname, namelen) < 0) + return (__DECONST(char*, amnesiac)); + return (hostname); } Index: src/l2tp.c =================================================================== --- src/l2tp.c (revision 2533) +++ src/l2tp.c (working copy) @@ -508,12 +508,8 @@ L2tpOpen(Link l) /* Create vendor name AVP */ avps = ppp_l2tp_avp_list_create(); - if (pi->conf.hostname[0] != 0) { - strlcpy(hostname, pi->conf.hostname, sizeof(hostname)); - } else { - (void)gethostname(hostname, sizeof(hostname) - 1); - hostname[sizeof(hostname) - 1] = '\0'; - } + strlcpy(hostname, pi->conf.hostname[0] ? pi->conf.hostname : + GetHostName(), sizeof(hostname)); cap = htonl(L2TP_BEARER_DIGITAL|L2TP_BEARER_ANALOG); win = htons(8); /* XXX: this value is empirical. */ if ((ppp_l2tp_avp_list_append(avps, 1, 0, AVP_HOST_NAME, @@ -1478,12 +1474,8 @@ L2tpServerEvent(int type, void *arg) /* Create vendor name AVP */ avps = ppp_l2tp_avp_list_create(); - if (pi->conf.hostname[0] != 0) { - strlcpy(hostname, pi->conf.hostname, sizeof(hostname)); - } else { - (void)gethostname(hostname, sizeof(hostname) - 1); - hostname[sizeof(hostname) - 1] = '\0'; - } + strlcpy(hostname, pi->conf.hostname[0] ? pi->conf.hostname : + GetHostName(), sizeof(hostname)); cap = htonl(L2TP_BEARER_DIGITAL|L2TP_BEARER_ANALOG); win = htons(8); /* XXX: this value is empirical. */ if ((ppp_l2tp_avp_list_append(avps, 1, 0, AVP_HOST_NAME, Index: src/l2tp_ctrl.c =================================================================== --- src/l2tp_ctrl.c (revision 2533) +++ src/l2tp_ctrl.c (working copy) @@ -41,6 +41,7 @@ */ #include "ppp.h" +#include "util.h" #include #include #include @@ -556,8 +557,7 @@ ppp_l2tp_ctrl_create(struct pevent_ctx *ctx, pthread_m } if ((index = ppp_l2tp_avp_list_find(ctrl->avps, 0, AVP_HOST_NAME)) == -1) { - (void)gethostname(ctrl->self_name, sizeof(ctrl->self_name) - 1); - ctrl->self_name[sizeof(ctrl->self_name) - 1] = '\0'; + strlcpy(ctrl->self_name, GetHostName(), sizeof(ctrl->self_name)); if (ppp_l2tp_avp_list_append(ctrl->avps, 1, 0, AVP_HOST_NAME, ctrl->self_name, strlen(ctrl->self_name)) == -1) goto fail; Index: src/pppoe.c =================================================================== --- src/pppoe.c (revision 2534) +++ src/pppoe.c (working copy) @@ -1435,17 +1435,8 @@ PppoeListenEvent(int type, void *arg) /* Put the PPPoE node into OFFER mode. */ memset(idata, 0, sizeof(*idata)); strlcpy(idata->hook, session_hook, sizeof(idata->hook)); - if (pi->acname[0] != 0) { - strlcpy(idata->data, pi->acname, MAX_SESSION); - } else { - if (gethostname(idata->data, MAX_SESSION) == -1) { - Log(LG_ERR, ("[%s] PPPoE: gethostname() failed", - l->name)); - idata->data[0] = 0; - } - if (idata->data[0] == 0) - strlcpy(idata->data, "NONAME", MAX_SESSION); - } + strlcpy(idata->data, pi->acname[0] ? pi->acname : + GetHostName(), MAX_SESSION); idata->data_len=strlen(idata->data); if (NgSendMsg(pi->PIf->csock, path, NGM_PPPOE_COOKIE, NGM_PPPOE_OFFER, Index: src/pptp_ctrl.c =================================================================== --- src/pptp_ctrl.c (revision 2533) +++ src/pptp_ctrl.c (working copy) @@ -955,8 +955,7 @@ abort: msg.frameCap = PPTP_FRAMECAP_ANY; msg.bearCap = PPTP_BEARCAP_ANY; msg.firmware = PPTP_FIRMWARE_REV; - gethostname(c->self_name, sizeof(c->self_name) - 1); - c->self_name[sizeof(c->self_name) - 1] = '\0'; + strlcpy(c->self_name, GetHostName(), sizeof(c->self_name)); strlcpy(msg.host, c->self_name, sizeof(msg.host)); strlcpy(msg.vendor, MPD_VENDOR, sizeof(msg.vendor)); PptpCtrlNewCtrlState(c, PPTP_CTRL_ST_WAIT_CTL_REPLY); @@ -2020,8 +2019,7 @@ PptpStartCtrlConnRequest(PptpCtrl c, struct pptpStartC reply.bearCap = PPTP_BEARCAP_ANY; reply.firmware = PPTP_FIRMWARE_REV; reply.result = PPTP_SCCR_RESL_OK; - gethostname(c->self_name, sizeof(c->self_name) - 1); - c->self_name[sizeof(c->self_name) - 1] = '\0'; + strlcpy(c->self_name, GetHostName(), sizeof(c->self_name)); strlcpy(reply.host, c->self_name, sizeof(reply.host)); strlcpy(reply.vendor, MPD_VENDOR, sizeof(reply.vendor)); Index: src/radius.c =================================================================== --- src/radius.c (revision 2533) +++ src/radius.c (working copy) @@ -563,7 +563,6 @@ static int RadiusStart(AuthData auth, short request_type) { RadConf const conf = &auth->conf.radius; - char host[MAXHOSTNAMELEN]; int porttype; char buf[48]; char *tmpval; @@ -577,17 +576,7 @@ RadiusStart(AuthData auth, short request_type) return (RAD_NACK); } - if (conf->identifier) { - tmpval = conf->identifier; - } else { - if (gethostname(host, sizeof(host)) == -1) { - Log(LG_ERR|LG_RADIUS, - ("[%s] RADIUS: gethostname() for RAD_NAS_IDENTIFIER failed", - auth->info.lnkname)); - return (RAD_NACK); - } - tmpval = host; - } + tmpval = conf->identifier ? conf->identifier : GetHostName(); Log(LG_RADIUS2, ("[%s] RADIUS: Put RAD_NAS_IDENTIFIER: %s", auth->info.lnkname, tmpval)); if (rad_put_string(auth->radius.handle, RAD_NAS_IDENTIFIER, tmpval) == -1) {