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  * XDR MESSAGE TABLES
45  ****************************************************************
46  */
47 
ndmp_xmt_lookup(int protocol_version,int msg)48 struct ndmp_xdr_message_table* ndmp_xmt_lookup(int protocol_version, int msg)
49 {
50   struct ndmp_xdr_message_table* table;
51   struct ndmp_xdr_message_table* ent;
52 
53   switch (protocol_version) {
54     case 0:
55       table = ndmp0_xdr_message_table;
56       break;
57 
58 #ifndef NDMOS_OPTION_NO_NDMP2
59     case NDMP2VER:
60       table = ndmp2_xdr_message_table;
61       break;
62 #endif /* !NDMOS_OPTION_NO_NDMP2 */
63 
64 #ifndef NDMOS_OPTION_NO_NDMP3
65     case NDMP3VER:
66       table = ndmp3_xdr_message_table;
67       break;
68 #endif /* !NDMOS_OPTION_NO_NDMP3 */
69 
70 #ifndef NDMOS_OPTION_NO_NDMP4
71     case NDMP4VER:
72       table = ndmp4_xdr_message_table;
73       break;
74 #endif /* !NDMOS_OPTION_NO_NDMP4 */
75 
76     default:
77       return 0;
78   }
79 
80   for (ent = table; ent->msg; ent++) {
81     if (ent->msg == msg) { return ent; }
82   }
83 
84   return 0;
85 }
86 
87 
88 /*
89  * ENUM STRING TABLES
90  ****************************************************************
91  */
92 
ndmp_enum_to_str(int val,struct ndmp_enum_str_table * table)93 char* ndmp_enum_to_str(int val, struct ndmp_enum_str_table* table)
94 {
95   static char vbuf[8][32];
96   static int vbix;
97   char* vbp;
98 
99   for (; table->name; table++)
100     if (table->value == val) return table->name;
101 
102   vbp = vbuf[vbix & 7];
103   vbix++;
104 
105   sprintf(vbp, "?0x%x?", val);
106   return vbp;
107 }
108 
ndmp_enum_from_str(int * valp,char * str,struct ndmp_enum_str_table * table)109 int ndmp_enum_from_str(int* valp, char* str, struct ndmp_enum_str_table* table)
110 {
111   for (; table->name; table++) {
112     if (strcmp(table->name, str) == 0) {
113       *valp = table->value;
114       return 1;
115     }
116   }
117 
118   return 0;
119 }
120 
121 
122 /*
123  * MULTI-VERSION ENUM TO STRING CONVERTERS
124  ****************************************************************
125  */
126 
ndmp_message_to_str(int protocol_version,int msg)127 char* ndmp_message_to_str(int protocol_version, int msg)
128 {
129   static char yikes_buf[40]; /* non-reentrant */
130 
131   switch (protocol_version) {
132     case 0:
133       return ndmp0_message_to_str(msg);
134 #ifndef NDMOS_OPTION_NO_NDMP2
135     case NDMP2VER:
136       return ndmp2_message_to_str(msg);
137 #endif /* !NDMOS_OPTION_NO_NDMP2 */
138 
139 #ifndef NDMOS_OPTION_NO_NDMP3
140     case NDMP3VER:
141       return ndmp3_message_to_str(msg);
142 #endif /* !NDMOS_OPTION_NO_NDMP3 */
143 
144 #ifndef NDMOS_OPTION_NO_NDMP4
145     case NDMP4VER:
146       return ndmp4_message_to_str(msg);
147 #endif /* !NDMOS_OPTION_NO_NDMP4 */
148 
149     default: /* should never happen, if so should be rare */
150       sprintf(yikes_buf, "v%dmsg0x%04x", protocol_version, msg);
151       return yikes_buf;
152   }
153 }
154 
ndmp_error_to_str(int protocol_version,int err)155 char* ndmp_error_to_str(int protocol_version, int err)
156 {
157   static char yikes_buf[40]; /* non-reentrant */
158 
159   switch (protocol_version) {
160     case 0:
161       return ndmp0_error_to_str(err);
162     case 9:
163       return ndmp9_error_to_str(err);
164 #ifndef NDMOS_OPTION_NO_NDMP2
165     case NDMP2VER:
166       return ndmp2_error_to_str(err);
167 #endif /* !NDMOS_OPTION_NO_NDMP2 */
168 
169 #ifndef NDMOS_OPTION_NO_NDMP3
170     case NDMP3VER:
171       return ndmp3_error_to_str(err);
172 #endif /* !NDMOS_OPTION_NO_NDMP3 */
173 
174 #ifndef NDMOS_OPTION_NO_NDMP4
175     case NDMP4VER:
176       return ndmp4_error_to_str(err);
177 #endif /* !NDMOS_OPTION_NO_NDMP4 */
178 
179     default: /* should never happen, if so should be rare */
180       sprintf(yikes_buf, "v%derr%d", protocol_version, err);
181       return yikes_buf;
182   }
183 }
184 
185 
186 /*
187  * PRETTY PRINTERS
188  ****************************************************************
189  */
190 
ndmp_pp_header(int vers,void * data,char * buf)191 int ndmp_pp_header(int vers, void* data, char* buf)
192 {
193   switch (vers) {
194     case 0:
195       return ndmp0_pp_header(data, buf);
196 
197 #ifndef NDMOS_OPTION_NO_NDMP2
198     case NDMP2VER:
199       return ndmp2_pp_header(data, buf);
200 #endif /* !NDMOS_OPTION_NO_NDMP2 */
201 
202 #ifndef NDMOS_OPTION_NO_NDMP3
203     case NDMP3VER:
204       return ndmp3_pp_header(data, buf);
205 #endif /* !NDMOS_OPTION_NO_NDMP3 */
206 
207 #ifndef NDMOS_OPTION_NO_NDMP4
208     case NDMP4VER:
209       return ndmp4_pp_header(data, buf);
210 #endif /* !NDMOS_OPTION_NO_NDMP4 */
211 
212     default:
213       sprintf(buf, "V%d? ", vers);
214       return ndmp0_pp_header(data, NDMOS_API_STREND(buf));
215   }
216 }
217 
ndmp_pp_request(int vers,int msg,void * data,int lineno,char * buf)218 int ndmp_pp_request(int vers, int msg, void* data, int lineno, char* buf)
219 {
220   switch (vers) {
221     case 0:
222       return ndmp0_pp_request(msg, data, lineno, buf);
223 
224 #ifndef NDMOS_OPTION_NO_NDMP2
225     case NDMP2VER:
226       return ndmp2_pp_request(msg, data, lineno, buf);
227 #endif /* !NDMOS_OPTION_NO_NDMP2 */
228 
229 #ifndef NDMOS_OPTION_NO_NDMP3
230     case NDMP3VER:
231       return ndmp3_pp_request(msg, data, lineno, buf);
232 #endif /* !NDMOS_OPTION_NO_NDMP3 */
233 
234 #ifndef NDMOS_OPTION_NO_NDMP4
235     case NDMP4VER:
236       return ndmp4_pp_request(msg, data, lineno, buf);
237 #endif /* !NDMOS_OPTION_NO_NDMP4 */
238 
239     default:
240       sprintf(buf, "<<INVALID MSG VERS=%d>>", vers);
241       return -1;
242   }
243 }
244 
ndmp_pp_reply(int vers,int msg,void * data,int lineno,char * buf)245 int ndmp_pp_reply(int vers, int msg, void* data, int lineno, char* buf)
246 {
247   switch (vers) {
248     case 0:
249       return ndmp0_pp_reply(msg, data, lineno, buf);
250 
251 #ifndef NDMOS_OPTION_NO_NDMP2
252     case NDMP2VER:
253       return ndmp2_pp_reply(msg, data, lineno, buf);
254 #endif /* !NDMOS_OPTION_NO_NDMP2 */
255 
256 #ifndef NDMOS_OPTION_NO_NDMP3
257     case NDMP3VER:
258       return ndmp3_pp_reply(msg, data, lineno, buf);
259 #endif /* !NDMOS_OPTION_NO_NDMP3 */
260 
261 #ifndef NDMOS_OPTION_NO_NDMP4
262     case NDMP4VER:
263       return ndmp4_pp_reply(msg, data, lineno, buf);
264 #endif /* !NDMOS_OPTION_NO_NDMP4 */
265 
266     default:
267       sprintf(buf, "<<INVALID MSG VERS=%d>>", vers);
268       return -1;
269   }
270 }
271