Add these files, used with TDB.
[mpls-ppp.git] / pppd / plugins / radius / radiusclient / lib / clientid.c
blobf15084bda564e89c2ff428f5adf5e9daf4cee40b
1 /*
2 * $Id: clientid.c,v 1.2 2002/02/27 15:51:20 dfs Exp $
4 * Copyright (C) 1995,1996,1997 Lars Fenneberg
6 * See the file COPYRIGHT for the respective terms and conditions.
7 * If the file is missing contact me at lf@elemental.net
8 * and I'll send you a copy.
12 #include <config.h>
13 #include <includes.h>
14 #include <radiusclient.h>
16 struct map2id_s {
17 char *name;
18 UINT4 id;
20 struct map2id_s *next;
23 static struct map2id_s *map2id_list = NULL;
26 * Function: rc_read_mapfile
28 * Purpose: Read in the ttyname to port id map file
30 * Arguments: the file name of the map file
32 * Returns: zero on success, negative integer on failure
35 int rc_read_mapfile(char *filename)
37 char buffer[1024];
38 FILE *mapfd;
39 char *c, *name, *id, *q;
40 struct map2id_s *p;
41 int lnr = 0;
43 if ((mapfd = fopen(filename,"r")) == NULL)
45 rc_log(LOG_ERR,"rc_read_mapfile: can't read %s: %s", filename, strerror(errno));
46 return (-1);
49 #define SKIP(p) while(*p && isspace(*p)) p++;
51 while (fgets(buffer, sizeof(buffer), mapfd) != NULL)
53 lnr++;
55 q = buffer;
57 SKIP(q);
59 if ((*q == '\n') || (*q == '#') || (*q == '\0'))
60 continue;
62 if (( c = strchr(q, ' ')) || (c = strchr(q,'\t'))) {
64 *c = '\0'; c++;
65 SKIP(c);
67 name = q;
68 id = c;
70 if ((p = (struct map2id_s *)malloc(sizeof(*p))) == NULL) {
71 rc_log(LOG_CRIT,"rc_read_mapfile: out of memory");
72 return (-1);
75 p->name = strdup(name);
76 p->id = atoi(id);
77 p->next = map2id_list;
78 map2id_list = p;
80 } else {
82 rc_log(LOG_ERR, "rc_read_mapfile: malformed line in %s, line %d", filename, lnr);
83 return (-1);
88 #undef SKIP
90 fclose(mapfd);
92 return 0;
96 * Function: rc_map2id
98 * Purpose: Map ttyname to port id
100 * Arguments: full pathname of the tty
102 * Returns: port id, zero if no entry found
105 UINT4 rc_map2id(char *name)
107 struct map2id_s *p;
108 char ttyname[PATH_MAX];
110 *ttyname = '\0';
111 if (*name != '/')
112 strcpy(ttyname, "/dev/");
114 strncat(ttyname, name, sizeof(ttyname));
116 for(p = map2id_list; p; p = p->next)
117 if (!strcmp(ttyname, p->name)) return p->id;
119 rc_log(LOG_WARNING,"rc_map2id: can't find tty %s in map database", ttyname);
121 return 0;