2 * nas.c Functions to do with a NASLIST. This is here because
3 * radzap needs it as well.
9 static const char rcsid[] = "$Id$";
13 #include <sys/types.h>
31 static void nas_free(NAS *cl)
45 int read_naslist_file(char *file)
59 if ((fp = fopen(file, "r")) == NULL) {
60 log(L_CONS|L_ERR, "cannot open %s", file);
63 while(fgets(buffer, 256, fp) != NULL) {
65 if (strchr(buffer, '\n') == NULL) {
66 log(L_ERR, "%s[%d]: line too long", file, lineno);
69 if (buffer[0] == '#' || buffer[0] == '\n')
73 if (!getword(&p, hostnm, sizeof(hostnm)) ||
74 !getword(&p, shortnm, sizeof(shortnm))) {
75 log(L_ERR, "%s[%d]: unexpected end of line", file, lineno);
78 (void)getword(&p, nastype, sizeof(nastype));
81 * Double-check lengths to be sure they're sane
83 if (strlen(hostnm) >= sizeof(c->longname)) {
84 log(L_ERR, "%s[%d]: host name of length %d is greater than the allowed maximum of %d.",
86 strlen(hostnm), sizeof(c->longname) - 1);
89 if (strlen(shortnm) > sizeof(c->shortname)) {
90 log(L_ERR, "%s[%d]: short name of length %d is greater than the allowed maximum of %d.",
92 strlen(shortnm), sizeof(c->shortname) - 1);
95 if (strlen(nastype) >= sizeof(c->nastype)) {
96 log(L_ERR, "%s[%d]: NAS type of length %d is greater than the allowed maximum of %d.",
98 strlen(nastype), sizeof(c->nastype) - 1);
103 * It should be OK now, let's create the buffer.
105 if ((c = malloc(sizeof(NAS))) == NULL) {
106 log(L_CONS|L_ERR, "%s[%d]: out of memory",
111 strcpy(c->nastype, nastype);
112 strcpy(c->shortname, shortnm);
114 if (strcmp(hostnm, "DEFAULT") == 0) {
116 strcpy(c->longname, hostnm);
118 c->ipaddr = ip_getaddr(hostnm);
119 strcpy(c->longname, ip_hostname(c->ipaddr));
132 * Find a nas by IP address.
133 * If it can't be found, return the DEFAULT nas, instead.
135 NAS *nas_find(uint32_t ipaddr)
142 for (nas = naslist; nas; nas = nas->next) {
143 if (ipaddr == nas->ipaddr)
145 if (strcmp(nas->longname, "DEFAULT") == 0)
154 * Find a nas by name.
155 * If it can't be found, return the DEFAULT nas, instead.
157 NAS *nas_findbyname(char *nasname)
164 for (nas = naslist; nas; nas = nas->next) {
165 if (strcmp(nasname, nas->shortname) == 0 ||
166 strcmp(nasname, nas->longname) == 0)
168 if (strcmp(nas->longname, "DEFAULT") == 0)
177 * Find the name of a nas (prefer short name).
179 char *nas_name(uint32_t ipaddr)
183 if ((cl = nas_find(ipaddr)) != NULL) {
184 if (cl->shortname[0])
185 return cl->shortname;
189 return ip_hostname(ipaddr);
193 * Find the name of a nas (prefer short name) based on the request.
195 char *nas_name2(RADIUS_PACKET *packet)
201 if ((pair = pairfind(packet->vps, PW_NAS_IP_ADDRESS)) != NULL)
202 ipaddr = pair->lvalue;
204 ipaddr = packet->src_ipaddr;
206 if ((cl = nas_find(ipaddr)) != NULL) {
207 if (cl->shortname[0])
208 return cl->shortname;
212 return ip_hostname(ipaddr);