e184c49e9aa5342b34cd20caf32c7b6939cb6dd1
[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 #include <roken.h>
14
15 #include "bc.h"
16
17 struct bc_config *bc_globalConfig;
18
19 #if 0
20 static int
21 TrimLine(char *abuffer, afs_int32 *aport)
22 {
23     int tc;
24     char garb[100];
25
26     *aport = 0;
27     sscanf(abuffer, "%s %u", garb, aport);
28     while ((tc = *abuffer)) {
29         if (tc == ' ') {
30             *abuffer = 0;
31             return 0;
32         }
33         abuffer++;
34     }
35     return 0;
36 }
37 #endif
38
39 FILE *
40 bc_open(struct bc_config *aconfig, char *aname, char *aext, char *amode)
41 {
42     FILE *tf;
43     char tpath[256];
44
45     strcpy(tpath, aconfig->path);
46     strcat(tpath, "/");
47     strcat(tpath, aname);
48     if (aext)
49         strcat(tpath, aext);
50     tf = fopen(tpath, amode);
51     return tf;
52 }
53
54 int
55 bc_InitConfig(char *apath)
56 {
57     struct bc_config *tb;
58
59     /* initialize global config structure */
60     tb = (struct bc_config *)malloc(sizeof(struct bc_config));
61     if (!tb)
62         return (BC_NOMEM);
63
64     bc_globalConfig = tb;
65     memset(tb, 0, sizeof(struct bc_config));
66     tb->path = strdup(apath);
67     if (!tb->path) {
68         free(tb);
69         return (BC_NOMEM);
70     }
71
72     /* now read the important config files; no file means empty list during system init */
73
74     return 0;
75 }
76
77 static int
78 HostAdd(struct bc_hostEntry **alist, char *aname, afs_int32 aport)
79 {
80     struct bc_hostEntry **tlast, *tentry;
81     struct hostent *th;
82
83     /* check that the host address is real */
84     th = gethostbyname(aname);
85     if (!th)
86         return -1;
87
88     /* check if this guy is already in the list */
89     for (tentry = *alist; tentry; tentry = tentry->next)
90         if (tentry->portOffset == aport)
91             return EEXIST;
92
93     /* add this guy to the end of the list */
94     tlast = alist;
95     for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast);
96
97     /* tlast now points to the next pointer (or head pointer) we should overwrite */
98     tentry = (struct bc_hostEntry *)malloc(sizeof(struct bc_hostEntry));
99     memset(tentry, 0, sizeof(*tentry));
100     tentry->name = strdup(aname);
101     *tlast = tentry;
102     tentry->next = (struct bc_hostEntry *)0;
103     tentry->addr.sin_family = AF_INET;
104     memcpy(&tentry->addr.sin_addr.s_addr, th->h_addr, sizeof(afs_int32));
105     tentry->addr.sin_port = 0;
106 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
107     tentry->addr.sin_len = sizeof(struct sockaddr_in);
108 #endif
109     tentry->portOffset = aport;
110     return 0;
111 }
112
113 static int
114 HostDelete(struct bc_hostEntry **alist, char *aname, afs_int32 aport)
115 {
116     struct bc_hostEntry **tlast, *tentry;
117
118     /* find guy to zap */
119     tlast = alist;
120     for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast)
121         if (strcmp(tentry->name, aname) == 0 && (aport == tentry->portOffset))
122             break;
123     if (!tentry)
124         return ENOENT;          /* failed to find it */
125
126     /* otherwise delete the entry from the list and free appropriate structures */
127     *tlast = tentry->next;
128     free(tentry->name);
129     free(tentry);
130     return 0;
131 }
132
133 int
134 bc_AddTapeHost(struct bc_config *aconfig, char *aname, afs_int32 aport)
135 {
136     afs_int32 code;
137
138     code = HostAdd(&aconfig->tapeHosts, aname, aport);
139
140     return code;
141 }
142
143 int
144 bc_DeleteTapeHost(struct bc_config *aconfig, char *aname, afs_int32 aport)
145 {
146     afs_int32 code;
147
148     code = HostDelete(&aconfig->tapeHosts, aname, aport);
149
150     return code;
151 }