1 /*
2 * Copyright (c) 1998,1999,2000
3 * Traakan, Inc., Los Altos, CA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Project: NDMJOB
31 * Ident: $Id: $
32 *
33 * Description:
34 * Miscellaneous C functions to support protocol versions.
35 * See ndmprotocol.h for explanation.
36 */
37
38
39 #include "ndmos.h"
40 #include "ndmprotocol.h"
41
42
43
44
45 /*
46 * XDR MESSAGE TABLES
47 ****************************************************************
48 */
49
50 struct ndmp_xdr_message_table *
ndmp_xmt_lookup(int protocol_version,int msg)51 ndmp_xmt_lookup (int protocol_version, int msg)
52 {
53 struct ndmp_xdr_message_table * table;
54 struct ndmp_xdr_message_table * ent;
55
56 switch (protocol_version) {
57 case 0:
58 table = ndmp0_xdr_message_table;
59 break;
60
61 #ifndef NDMOS_OPTION_NO_NDMP2
62 case NDMP2VER:
63 table = ndmp2_xdr_message_table;
64 break;
65 #endif /* !NDMOS_OPTION_NO_NDMP2 */
66
67 #ifndef NDMOS_OPTION_NO_NDMP3
68 case NDMP3VER:
69 table = ndmp3_xdr_message_table;
70 break;
71 #endif /* !NDMOS_OPTION_NO_NDMP3 */
72
73 #ifndef NDMOS_OPTION_NO_NDMP4
74 case NDMP4VER:
75 table = ndmp4_xdr_message_table;
76 break;
77 #endif /* !NDMOS_OPTION_NO_NDMP4 */
78
79 default:
80 return 0;
81 }
82
83 for (ent = table; ent->msg; ent++) {
84 if (ent->msg == msg) {
85 return ent;
86 }
87 }
88
89 return 0;
90 }
91
92
93
94
95 /*
96 * ENUM STRING TABLES
97 ****************************************************************
98 */
99
100 char *
ndmp_enum_to_str(int val,struct ndmp_enum_str_table * table)101 ndmp_enum_to_str (int val, struct ndmp_enum_str_table *table)
102 {
103 static char vbuf[8][32];
104 static int vbix;
105 char * vbp;
106
107 for (; table->name; table++)
108 if (table->value == val)
109 return table->name;
110
111 vbp = vbuf[vbix&7];
112 vbix++;
113
114 sprintf (vbp, "?0x%x?", val);
115 return vbp;
116 }
117
118 int
ndmp_enum_from_str(int * valp,char * str,struct ndmp_enum_str_table * table)119 ndmp_enum_from_str (int *valp, char *str, struct ndmp_enum_str_table *table)
120 {
121 for (; table->name; table++) {
122 if (strcmp(table->name, str) == 0) {
123 *valp = table->value;
124 return 1;
125 }
126 }
127
128 return 0;
129 }
130
131
132
133
134 /*
135 * MULTI-VERSION ENUM TO STRING CONVERTERS
136 ****************************************************************
137 */
138
139 char *
ndmp_message_to_str(int protocol_version,int msg)140 ndmp_message_to_str (int protocol_version, int msg)
141 {
142 static char yikes_buf[40]; /* non-reentrant */
143
144 switch (protocol_version) {
145 case 0: return ndmp0_message_to_str (msg);
146 #ifndef NDMOS_OPTION_NO_NDMP2
147 case NDMP2VER: return ndmp2_message_to_str (msg);
148 #endif /* !NDMOS_OPTION_NO_NDMP2 */
149
150 #ifndef NDMOS_OPTION_NO_NDMP3
151 case NDMP3VER: return ndmp3_message_to_str (msg);
152 #endif /* !NDMOS_OPTION_NO_NDMP3 */
153
154 #ifndef NDMOS_OPTION_NO_NDMP4
155 case NDMP4VER: return ndmp4_message_to_str (msg);
156 #endif /* !NDMOS_OPTION_NO_NDMP4 */
157
158 default: /* should never happen, if so should be rare */
159 sprintf (yikes_buf, "v%dmsg0x%04x", protocol_version, msg);
160 return yikes_buf;
161 }
162 }
163
164 char *
ndmp_error_to_str(int protocol_version,int err)165 ndmp_error_to_str (int protocol_version, int err)
166 {
167 static char yikes_buf[40]; /* non-reentrant */
168
169 switch (protocol_version) {
170 case 0: return ndmp0_error_to_str (err);
171 case 9: return ndmp9_error_to_str (err);
172 #ifndef NDMOS_OPTION_NO_NDMP2
173 case NDMP2VER: return ndmp2_error_to_str (err);
174 #endif /* !NDMOS_OPTION_NO_NDMP2 */
175
176 #ifndef NDMOS_OPTION_NO_NDMP3
177 case NDMP3VER: return ndmp3_error_to_str (err);
178 #endif /* !NDMOS_OPTION_NO_NDMP3 */
179
180 #ifndef NDMOS_OPTION_NO_NDMP4
181 case NDMP4VER: return ndmp4_error_to_str (err);
182 #endif /* !NDMOS_OPTION_NO_NDMP4 */
183
184 default: /* should never happen, if so should be rare */
185 sprintf (yikes_buf, "v%derr%d", protocol_version, err);
186 return yikes_buf;
187 }
188 }
189
190
191
192
193
194 /*
195 * PRETTY PRINTERS
196 ****************************************************************
197 */
198
199 int
ndmp_pp_header(int vers,void * data,char * buf)200 ndmp_pp_header (int vers, void *data, char *buf)
201 {
202 switch (vers) {
203 case 0:
204 return ndmp0_pp_header (data, buf);
205
206 #ifndef NDMOS_OPTION_NO_NDMP2
207 case NDMP2VER:
208 return ndmp2_pp_header (data, buf);
209 #endif /* !NDMOS_OPTION_NO_NDMP2 */
210
211 #ifndef NDMOS_OPTION_NO_NDMP3
212 case NDMP3VER:
213 return ndmp3_pp_header (data, buf);
214 #endif /* !NDMOS_OPTION_NO_NDMP3 */
215
216 #ifndef NDMOS_OPTION_NO_NDMP4
217 case NDMP4VER:
218 return ndmp4_pp_header (data, buf);
219 #endif /* !NDMOS_OPTION_NO_NDMP4 */
220
221 default:
222 sprintf (buf, "V%d? ", vers);
223 return ndmp0_pp_header (data, NDMOS_API_STREND(buf));
224 }
225 }
226
227 int
ndmp_pp_request(int vers,int msg,void * data,int lineno,char * buf)228 ndmp_pp_request (int vers, int msg, void *data, int lineno, char *buf)
229 {
230 switch (vers) {
231 case 0:
232 return ndmp0_pp_request (msg, data, lineno, buf);
233
234 #ifndef NDMOS_OPTION_NO_NDMP2
235 case NDMP2VER:
236 return ndmp2_pp_request (msg, data, lineno, buf);
237 #endif /* !NDMOS_OPTION_NO_NDMP2 */
238
239 #ifndef NDMOS_OPTION_NO_NDMP3
240 case NDMP3VER:
241 return ndmp3_pp_request (msg, data, lineno, buf);
242 #endif /* !NDMOS_OPTION_NO_NDMP3 */
243
244 #ifndef NDMOS_OPTION_NO_NDMP4
245 case NDMP4VER:
246 return ndmp4_pp_request (msg, data, lineno, buf);
247 #endif /* !NDMOS_OPTION_NO_NDMP4 */
248
249 default:
250 sprintf (buf, "<<INVALID MSG VERS=%d>>", vers);
251 return -1;
252 }
253 }
254
255 int
ndmp_pp_reply(int vers,int msg,void * data,int lineno,char * buf)256 ndmp_pp_reply (int vers, int msg, void *data, int lineno, char *buf)
257 {
258 switch (vers) {
259 case 0:
260 return ndmp0_pp_reply (msg, data, lineno, buf);
261
262 #ifndef NDMOS_OPTION_NO_NDMP2
263 case NDMP2VER:
264 return ndmp2_pp_reply (msg, data, lineno, buf);
265 #endif /* !NDMOS_OPTION_NO_NDMP2 */
266
267 #ifndef NDMOS_OPTION_NO_NDMP3
268 case NDMP3VER:
269 return ndmp3_pp_reply (msg, data, lineno, buf);
270 #endif /* !NDMOS_OPTION_NO_NDMP3 */
271
272 #ifndef NDMOS_OPTION_NO_NDMP4
273 case NDMP4VER:
274 return ndmp4_pp_reply (msg, data, lineno, buf);
275 #endif /* !NDMOS_OPTION_NO_NDMP4 */
276
277 default:
278 sprintf (buf, "<<INVALID MSG VERS=%d>>", vers);
279 return -1;
280 }
281 }
282