57d49b0a071c6c1fbe7e0348369d84e07f1fff1f
[openafs.git] / src / bucoord / config.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #ifdef AFS_NT40_ENV
17 #include <winsock2.h>
18 #else
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #endif
23 #include <stdio.h>
24 #include <errno.h>
25
26 #include "bc.h"
27
28 struct bc_config *bc_globalConfig;
29
30 static TrimLine(abuffer,aport)
31 afs_int32 *aport;
32 register char *abuffer; {
33     register int tc;
34     char garb[100];
35
36     *aport = 0;
37     sscanf(abuffer,"%s %u",garb,aport);
38     while(tc = *abuffer) {
39         if (tc == ' ') {
40             *abuffer = 0;
41             return 0;
42         }
43         abuffer++;
44     }
45     return 0;
46 }
47
48 FILE *bc_open(aconfig, aname, aext, amode)
49 register struct bc_config *aconfig;
50 char *amode;
51 char *aext;
52 char *aname; {
53     register FILE *tf;
54     char tpath[256];
55
56     strcpy(tpath, aconfig->path);
57     strcat(tpath, "/");
58     strcat(tpath, aname);
59     if (aext) strcat(tpath, aext);
60     tf = fopen(tpath, amode);
61     return tf;
62 }
63
64 bc_InitConfig(apath) 
65     char *apath;
66 {
67     register struct bc_config *tb;
68     register struct bc_hostEntry *the;
69
70     /* initialize global config structure */
71     tb = (struct bc_config *) malloc(sizeof(struct bc_config));
72     if (!tb) return(BC_NOMEM);
73
74     bc_globalConfig = tb;
75     bzero(tb, sizeof(struct bc_config));
76     tb->path = (char *) malloc(strlen(apath)+1);
77     if (!tb->path) 
78     {
79         free(tb);
80         return(BC_NOMEM);
81     }
82
83     strcpy(tb->path, apath);
84
85     /* now read the important config files; no file means empty list during system init */
86
87     return 0;
88 }
89
90 static HostAdd(alist, aname,aport)
91 struct bc_hostEntry **alist;
92 afs_int32 aport;
93 char *aname; {
94     register struct bc_hostEntry **tlast, *tentry;
95     struct hostent *th;
96
97     /* check that the host address is real */
98     th = gethostbyname(aname);
99     if (!th) return -1;
100
101     /* check if this guy is already in the list */
102     for(tentry = *alist; tentry; tentry=tentry->next)
103         if (tentry->portOffset == aport) return EEXIST;
104
105     /* add this guy to the end of the list */
106     tlast = alist;
107     for(tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast);
108
109     /* tlast now points to the next pointer (or head pointer) we should overwrite */
110     tentry = (struct bc_hostEntry *) malloc(sizeof(struct bc_hostEntry));
111     bzero(tentry, sizeof(*tentry));
112     tentry->name = (char *) malloc(strlen(aname)+1);
113     strcpy(tentry->name, aname);
114     *tlast = tentry;
115     tentry->next = (struct bc_hostEntry *) 0;
116     tentry->addr.sin_family = AF_INET;
117     bcopy(th->h_addr, &tentry->addr.sin_addr.s_addr, sizeof(afs_int32));
118     tentry->addr.sin_port = 0;
119     tentry->portOffset = aport;
120     return 0;
121 }
122
123 static HostDelete(alist, aname,aport)
124 struct bc_hostEntry **alist;
125 afs_int32 aport;
126 char *aname; {
127     register struct bc_hostEntry **tlast, *tentry;
128
129     /* find guy to zap */
130     tlast = alist;
131     for(tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast)
132         if (strcmp(tentry->name, aname) == 0 && (aport == tentry->portOffset))
133             break;
134     if (!tentry) return ENOENT; /* failed to find it */
135
136     /* otherwise delete the entry from the list and free appropriate structures */
137     *tlast = tentry->next;
138     free(tentry->name);
139     free(tentry);
140     return 0;
141 }
142
143 bc_AddTapeHost(aconfig, aname,aport)
144 struct bc_config *aconfig;
145 afs_int32 aport;
146 char *aname; {
147     register afs_int32 code;
148
149     code = HostAdd(&aconfig->tapeHosts, aname,aport);
150
151     return code;
152 }
153
154 bc_DeleteTapeHost(aconfig, aname, aport)
155 struct bc_config *aconfig;
156 afs_int32 aport;
157 char *aname; {
158     register afs_int32 code;
159
160     code = HostDelete(&aconfig->tapeHosts, aname, aport);
161
162     return code;
163 }