freebsd-almost-working-client-20020216
[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     memset(tb, 0, 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     memset(tentry, 0, 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     memcpy(&tentry->addr.sin_addr.s_addr, th->h_addr, sizeof(afs_int32));
118     tentry->addr.sin_port = 0;
119 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
120     tentry->addr.sin_len = sizeof(struct sockaddr_in);
121 #endif
122     tentry->portOffset = aport;
123     return 0;
124 }
125
126 static HostDelete(alist, aname,aport)
127 struct bc_hostEntry **alist;
128 afs_int32 aport;
129 char *aname; {
130     register struct bc_hostEntry **tlast, *tentry;
131
132     /* find guy to zap */
133     tlast = alist;
134     for(tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast)
135         if (strcmp(tentry->name, aname) == 0 && (aport == tentry->portOffset))
136             break;
137     if (!tentry) return ENOENT; /* failed to find it */
138
139     /* otherwise delete the entry from the list and free appropriate structures */
140     *tlast = tentry->next;
141     free(tentry->name);
142     free(tentry);
143     return 0;
144 }
145
146 bc_AddTapeHost(aconfig, aname,aport)
147 struct bc_config *aconfig;
148 afs_int32 aport;
149 char *aname; {
150     register afs_int32 code;
151
152     code = HostAdd(&aconfig->tapeHosts, aname,aport);
153
154     return code;
155 }
156
157 bc_DeleteTapeHost(aconfig, aname, aport)
158 struct bc_config *aconfig;
159 afs_int32 aport;
160 char *aname; {
161     register afs_int32 code;
162
163     code = HostDelete(&aconfig->tapeHosts, aname, aport);
164
165     return code;
166 }