Commit | Line | Data |
---|---|---|
805e021f CE |
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 = calloc(1, sizeof(struct bc_config)); | |
61 | if (!tb) | |
62 | return (BC_NOMEM); | |
63 | ||
64 | bc_globalConfig = tb; | |
65 | tb->path = strdup(apath); | |
66 | if (!tb->path) { | |
67 | free(tb); | |
68 | return (BC_NOMEM); | |
69 | } | |
70 | ||
71 | /* now read the important config files; no file means empty list during system init */ | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | static int | |
77 | HostAdd(struct bc_hostEntry **alist, char *aname, afs_int32 aport) | |
78 | { | |
79 | struct bc_hostEntry **tlast, *tentry; | |
80 | struct hostent *th; | |
81 | ||
82 | /* check that the host address is real */ | |
83 | th = gethostbyname(aname); | |
84 | if (!th) | |
85 | return -1; | |
86 | ||
87 | /* check if this guy is already in the list */ | |
88 | for (tentry = *alist; tentry; tentry = tentry->next) | |
89 | if (tentry->portOffset == aport) | |
90 | return EEXIST; | |
91 | ||
92 | /* add this guy to the end of the list */ | |
93 | tlast = alist; | |
94 | for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast); | |
95 | ||
96 | /* tlast now points to the next pointer (or head pointer) we should overwrite */ | |
97 | tentry = calloc(1, sizeof(struct bc_hostEntry)); | |
98 | tentry->name = strdup(aname); | |
99 | *tlast = tentry; | |
100 | tentry->next = (struct bc_hostEntry *)0; | |
101 | tentry->addr.sin_family = AF_INET; | |
102 | memcpy(&tentry->addr.sin_addr.s_addr, th->h_addr, sizeof(afs_int32)); | |
103 | tentry->addr.sin_port = 0; | |
104 | #ifdef STRUCT_SOCKADDR_HAS_SA_LEN | |
105 | tentry->addr.sin_len = sizeof(struct sockaddr_in); | |
106 | #endif | |
107 | tentry->portOffset = aport; | |
108 | return 0; | |
109 | } | |
110 | ||
111 | static int | |
112 | HostDelete(struct bc_hostEntry **alist, char *aname, afs_int32 aport) | |
113 | { | |
114 | struct bc_hostEntry **tlast, *tentry; | |
115 | ||
116 | /* find guy to zap */ | |
117 | tlast = alist; | |
118 | for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast) | |
119 | if (strcmp(tentry->name, aname) == 0 && (aport == tentry->portOffset)) | |
120 | break; | |
121 | if (!tentry) | |
122 | return ENOENT; /* failed to find it */ | |
123 | ||
124 | /* otherwise delete the entry from the list and free appropriate structures */ | |
125 | *tlast = tentry->next; | |
126 | free(tentry->name); | |
127 | free(tentry); | |
128 | return 0; | |
129 | } | |
130 | ||
131 | int | |
132 | bc_AddTapeHost(struct bc_config *aconfig, char *aname, afs_int32 aport) | |
133 | { | |
134 | afs_int32 code; | |
135 | ||
136 | code = HostAdd(&aconfig->tapeHosts, aname, aport); | |
137 | ||
138 | return code; | |
139 | } | |
140 | ||
141 | int | |
142 | bc_DeleteTapeHost(struct bc_config *aconfig, char *aname, afs_int32 aport) | |
143 | { | |
144 | afs_int32 code; | |
145 | ||
146 | code = HostDelete(&aconfig->tapeHosts, aname, aport); | |
147 | ||
148 | return code; | |
149 | } |