86624fd505fdb7478d2e651a0652c112a627bc42
[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 <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 <errno.h>
24
25 #include "bc.h"
26
27 struct bc_config *bc_globalConfig;
28
29 #if 0
30 static int
31 TrimLine(char *abuffer, afs_int32 *aport)
32 {
33     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 #endif
48
49 FILE *
50 bc_open(struct bc_config *aconfig, char *aname, char *aext, char *amode)
51 {
52     FILE *tf;
53     char tpath[256];
54
55     strcpy(tpath, aconfig->path);
56     strcat(tpath, "/");
57     strcat(tpath, aname);
58     if (aext)
59         strcat(tpath, aext);
60     tf = fopen(tpath, amode);
61     return tf;
62 }
63
64 int
65 bc_InitConfig(char *apath)
66 {
67     struct bc_config *tb;
68
69     /* initialize global config structure */
70     tb = (struct bc_config *)malloc(sizeof(struct bc_config));
71     if (!tb)
72         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         free(tb);
79         return (BC_NOMEM);
80     }
81
82     strcpy(tb->path, apath);
83
84     /* now read the important config files; no file means empty list during system init */
85
86     return 0;
87 }
88
89 static int
90 HostAdd(struct bc_hostEntry **alist, char *aname, afs_int32 aport)
91 {
92     struct bc_hostEntry **tlast, *tentry;
93     struct hostent *th;
94
95     /* check that the host address is real */
96     th = gethostbyname(aname);
97     if (!th)
98         return -1;
99
100     /* check if this guy is already in the list */
101     for (tentry = *alist; tentry; tentry = tentry->next)
102         if (tentry->portOffset == aport)
103             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 int
127 HostDelete(struct bc_hostEntry **alist, char *aname, afs_int32 aport)
128 {
129     struct bc_hostEntry **tlast, *tentry;
130
131     /* find guy to zap */
132     tlast = alist;
133     for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast)
134         if (strcmp(tentry->name, aname) == 0 && (aport == tentry->portOffset))
135             break;
136     if (!tentry)
137         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 int
147 bc_AddTapeHost(struct bc_config *aconfig, char *aname, afs_int32 aport)
148 {
149     afs_int32 code;
150
151     code = HostAdd(&aconfig->tapeHosts, aname, aport);
152
153     return code;
154 }
155
156 int
157 bc_DeleteTapeHost(struct bc_config *aconfig, char *aname, afs_int32 aport)
158 {
159     afs_int32 code;
160
161     code = HostDelete(&aconfig->tapeHosts, aname, aport);
162
163     return code;
164 }