1 /*
2                                   NETWIB
3                              Network library
4                 Copyright(c) 1999-2010 Laurent Constantin
5                                   -----
6 
7   Main server   : http://www.laurentconstantin.com/
8   Backup server : http://laurentconstantin.free.fr/
9   [my current email address is on the web servers]
10 
11                                   -----
12   This file is part of Netwib.
13 
14   Netwib is free software: you can redistribute it and/or modify
15   it under the terms of the GNU General Public License version 3
16   as published by the Free Software Foundation.
17 
18   Netwib is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details (http://www.gnu.org/).
22 
23 ------------------------------------------------------------------------
24 */
25 
26 #include <netwib/inc/maininc.h>
27 
28 /*-------------------------------------------------------------*/
netwib_device_init_kbd(netwib_constbuf * pmessage,netwib_constbuf * pdefaultdevice,netwib_buf * pdevice)29 netwib_err netwib_device_init_kbd(netwib_constbuf *pmessage,
30                                   netwib_constbuf *pdefaultdevice,
31                                   netwib_buf *pdevice)
32 {
33   netwib_conf_devices conf;
34   netwib_conf_devices_index *pconfindex;
35   netwib_buf buf;
36   netwib_uint32 choice, defaultchoice;
37   netwib_cmp cmp;
38   netwib_bool oneset;
39   netwib_err ret;
40 
41   netwib_er(netwib_buf_init_mallocdefault(&buf));
42   if (pmessage != NULL) {
43     netwib_er(netwib_buf_append_buf(pmessage, &buf));
44     netwib_er(netwib_buf_append_string("\n", &buf));
45   } else {
46     netwib_er(netwib_buf_append_string("Select device number :\n", &buf));
47   }
48 
49   netwib_er(netwib_conf_devices_index_init(&conf, &pconfindex));
50   ret = NETWIB_ERR_OK;
51   defaultchoice = NETWIB_UINT32_INIT_KBD_NODEF;
52   oneset = NETWIB_FALSE;
53   while (NETWIB_TRUE) {
54     ret = netwib_conf_devices_index_next(pconfindex);
55     if (ret == NETWIB_ERR_DATAEND) {
56       ret = NETWIB_ERR_OK;
57       break;
58     }
59     netwib_eg(ret);
60     oneset = NETWIB_TRUE;
61     netwib_eg(netwib_buf_append_fmt(&buf, " %{r 3;uint32} - %{l 5;buf} (%{buf})\n", conf.devnum, &conf.deviceeasy, &conf.device));
62     if (pdefaultdevice != NETWIB_DEVICE_INIT_KBD_NODEF) {
63       netwib_eg(netwib_buf_cmp(pdefaultdevice, &conf.device, &cmp));
64       if (cmp != NETWIB_CMP_EQ) {
65         netwib_eg(netwib_buf_cmp(pdefaultdevice, &conf.deviceeasy, &cmp));
66       }
67       if (cmp == NETWIB_CMP_EQ) {
68         defaultchoice = conf.devnum;
69       }
70     }
71   }
72   if (!oneset) {
73     netwib_er(netwib_buf_append_kbd(pmessage, pdefaultdevice, pdevice));
74     netwib_goto(NETWIB_ERR_OK);
75   }
76   netwib_er(netwib_buf_display(&buf, NETWIB_ENCODETYPE_DATA));
77 
78   netwib__buf_reinit(&buf);
79   netwib_er(netwib_buf_append_string("Your choice", &buf));
80   netwib_er(netwib_uint32_init_kbd(&buf, 1, conf.devnum, defaultchoice,
81                                    &choice));
82 
83   if (pdevice != NULL) {
84     netwib_eg(netwib_conf_devices_index_close(&pconfindex));
85     netwib_eg(netwib_conf_devices_index_init(&conf, &pconfindex));
86     while (NETWIB_TRUE) {
87       ret = netwib_conf_devices_index_next(pconfindex);
88       if (ret == NETWIB_ERR_DATAEND) {
89         /* configuration changed, so re-ask user */
90         ret = netwib_device_init_kbd(pmessage, pdefaultdevice, pdevice);
91         break;
92       }
93       netwib_eg(ret);
94       if (conf.devnum == choice) {
95         netwib_eg(netwib_buf_append_buf(&conf.deviceeasy, pdevice));
96         break;
97       }
98     }
99   }
100 
101  netwib_gotolabel:
102   netwib_er(netwib_buf_close(&buf));
103   netwib_er(netwib_conf_devices_index_close(&pconfindex));
104   return(ret);
105 }
106 
107 /*-------------------------------------------------------------*/
netwib_buf_append_device_hwtype(netwib_device_hwtype type,netwib_buf * pbuf)108 netwib_err netwib_buf_append_device_hwtype(netwib_device_hwtype type,
109                                            netwib_buf *pbuf)
110 {
111   netwib_conststring pc=NULL;
112 
113   switch(type) {
114     case NETWIB_DEVICE_HWTYPE_UNKNOWN :
115       pc = "unknown";
116       break;
117     case NETWIB_DEVICE_HWTYPE_ETHER :
118       pc = "ethernet";
119       break;
120     case NETWIB_DEVICE_HWTYPE_LOOPBACK :
121       pc = "loopback";
122       break;
123     case NETWIB_DEVICE_HWTYPE_PPP :
124       pc = "ppp";
125       break;
126     case NETWIB_DEVICE_HWTYPE_PLIP :
127       pc = "plip";
128       break;
129     case NETWIB_DEVICE_HWTYPE_SLIP :
130       pc = "slip";
131       break;
132     default :
133       return(NETWIB_ERR_PAINVALIDTYPE);
134   }
135 
136   netwib_er(netwib_buf_append_string(pc, pbuf));
137   return(NETWIB_ERR_OK);
138 }
139 
140 /*-------------------------------------------------------------*/
141 #define NETWIB_PRIV_DEVICE_HWTYPE_MAX 10
netwib_device_hwtype_init_kbd(netwib_constbuf * pmessage,netwib_device_hwtype defaulthwtype,netwib_device_hwtype * phwtype)142 netwib_err netwib_device_hwtype_init_kbd(netwib_constbuf *pmessage,
143                                          netwib_device_hwtype defaulthwtype,
144                                          netwib_device_hwtype *phwtype)
145 {
146   netwib_device_hwtype array[NETWIB_PRIV_DEVICE_HWTYPE_MAX];
147   netwib_buf msg, buf;
148   netwib_uint32 i, choice, defaultchoice;
149 
150   netwib_er(netwib_buf_init_mallocdefault(&buf));
151   if (pmessage != NULL) {
152     netwib_er(netwib_buf_append_buf(pmessage, &buf));
153     netwib_er(netwib_buf_append_string("\n", &buf));
154   }
155 
156   i = 0;
157   defaultchoice = NETWIB_UINT32_INIT_KBD_NODEF;
158 
159 #define netwib_hwtype_lih(ot) {netwib_er(netwib_buf_append_fmt(&buf, " %{r 2;uint32} - ", i)); netwib_er(netwib_buf_append_device_hwtype(ot, &buf)); netwib_er(netwib_buf_append_fmt(&buf, "\n")); if (defaulthwtype == ot) defaultchoice = i; array[i++] = ot; }
160 
161   netwib_hwtype_lih(NETWIB_DEVICE_HWTYPE_ETHER);
162   netwib_hwtype_lih(NETWIB_DEVICE_HWTYPE_LOOPBACK);
163   netwib_hwtype_lih(NETWIB_DEVICE_HWTYPE_PPP);
164   netwib_hwtype_lih(NETWIB_DEVICE_HWTYPE_PLIP);
165   netwib_hwtype_lih(NETWIB_DEVICE_HWTYPE_SLIP);
166 
167   if (i >= NETWIB_PRIV_DEVICE_HWTYPE_MAX) {
168     return(NETWIB_ERR_LOINTERNALERROR);
169   }
170   netwib_er(netwib_buf_display(&buf, NETWIB_ENCODETYPE_DATA));
171   netwib_er(netwib_buf_close(&buf));
172 
173   if (defaulthwtype == NETWIB_DEVICE_HWTYPE_UNKNOWN) {
174     defaultchoice = NETWIB_UINT32_INIT_KBD_NODEF;
175   }
176 
177   netwib_er(netwib_buf_init_ext_string("Your choice", &msg));
178   netwib_er(netwib_uint32_init_kbd(&msg, 0, i-1, defaultchoice,
179                                  &choice));
180 
181   if (phwtype != NULL) *phwtype = array[choice];
182   return(NETWIB_ERR_OK);
183 }
184 
185 /*-------------------------------------------------------------*/
netwib_buf_append_device_dlttype(netwib_device_dlttype type,netwib_buf * pbuf)186 netwib_err netwib_buf_append_device_dlttype(netwib_device_dlttype type,
187                                             netwib_buf *pbuf)
188 {
189   netwib_conststring pc=NULL;
190 
191   switch(type) {
192     case NETWIB_DEVICE_DLTTYPE_UNKNOWN :
193       pc = "unknown";
194       break;
195     case NETWIB_DEVICE_DLTTYPE_NULL :
196       pc = "no link-layer encapsulation";
197       break;
198     case NETWIB_DEVICE_DLTTYPE_EN10MB :
199       pc = "ethernet";
200       break;
201     case NETWIB_DEVICE_DLTTYPE_EN3MB :
202       pc = "experimental ethernet";
203       break;
204     case NETWIB_DEVICE_DLTTYPE_AX25 :
205       pc = "amateur radio";
206       break;
207     case NETWIB_DEVICE_DLTTYPE_PRONET :
208       pc = "ProNET Token Ring";
209       break;
210     case NETWIB_DEVICE_DLTTYPE_CHAOS :
211       pc = "chaos";
212       break;
213     case NETWIB_DEVICE_DLTTYPE_IEEE802 :
214       pc = "IEEE 802";
215       break;
216     case NETWIB_DEVICE_DLTTYPE_ARCNET :
217       pc = "ARCNET";
218       break;
219     case NETWIB_DEVICE_DLTTYPE_SLIP :
220       pc = "slip";
221       break;
222     case NETWIB_DEVICE_DLTTYPE_PPP :
223       pc = "ppp";
224       break;
225     case NETWIB_DEVICE_DLTTYPE_FDDI :
226       pc = "FDDI";
227       break;
228     case NETWIB_DEVICE_DLTTYPE_ATM_RFC1483 :
229       pc = "LLC/SNAP encapsulated atm";
230       break;
231     case NETWIB_DEVICE_DLTTYPE_RAW :
232       pc = "raw IP";
233       break;
234     case NETWIB_DEVICE_DLTTYPE_RAW4 :
235       pc = "raw IPv4";
236       break;
237     case NETWIB_DEVICE_DLTTYPE_RAW6 :
238       pc = "raw IPv6";
239       break;
240     case NETWIB_DEVICE_DLTTYPE_SLIP_BSDOS :
241       pc = "BSD/OS slip";
242       break;
243     case NETWIB_DEVICE_DLTTYPE_PPP_BSDOS :
244       pc = "BSD/OS ppp";
245       break;
246     case NETWIB_DEVICE_DLTTYPE_ATM_CLIP :
247       pc = "IP over ATM";
248       break;
249     case NETWIB_DEVICE_DLTTYPE_PPP_SERIAL :
250       pc = "PPP over serial";
251       break;
252     case NETWIB_DEVICE_DLTTYPE_PPP_ETHER :
253       pc = "PPP over ethernet";
254       break;
255     case NETWIB_DEVICE_DLTTYPE_C_HDLC :
256       pc = "Cisco HDLC";
257       break;
258     case NETWIB_DEVICE_DLTTYPE_IEEE802_11 :
259       pc = "IEEE 802.11";
260       break;
261     case NETWIB_DEVICE_DLTTYPE_LOOP :
262       pc = "loop";
263       break;
264     case NETWIB_DEVICE_DLTTYPE_LINUX_SLL :
265       pc = "linux cooked sockets";
266       break;
267     case NETWIB_DEVICE_DLTTYPE_LTALK :
268       pc = "LocalTalk";
269       break;
270     case NETWIB_DEVICE_DLTTYPE_ECONET :
271       pc = "Econet";
272       break;
273     case NETWIB_DEVICE_DLTTYPE_PRISM_HEADER :
274       pc = "Prism II";
275       break;
276     case NETWIB_DEVICE_DLTTYPE_AIRONET_HEADER :
277       pc = "Aironet";
278       break;
279     default :
280       return(NETWIB_ERR_PAINVALIDTYPE);
281   }
282 
283   netwib_er(netwib_buf_append_string(pc, pbuf));
284   return(NETWIB_ERR_OK);
285 }
286 
287 /*-------------------------------------------------------------*/
288 #define NETWIB_PRIV_DEVICE_DLTTYPE_MAX 40
netwib_device_dlttype_init_kbd(netwib_constbuf * pmessage,netwib_device_dlttype defaultdlttype,netwib_device_dlttype * pdlttype)289 netwib_err netwib_device_dlttype_init_kbd(netwib_constbuf *pmessage,
290                                           netwib_device_dlttype defaultdlttype,
291                                           netwib_device_dlttype *pdlttype)
292 {
293   netwib_device_dlttype array[NETWIB_PRIV_DEVICE_DLTTYPE_MAX];
294   netwib_buf msg, buf;
295   netwib_uint32 i, choice, defaultchoice;
296 
297   netwib_er(netwib_buf_init_mallocdefault(&buf));
298   if (pmessage != NULL) {
299     netwib_er(netwib_buf_append_buf(pmessage, &buf));
300     netwib_er(netwib_buf_append_string("\n", &buf));
301   }
302 
303   i = 0;
304   defaultchoice = NETWIB_UINT32_INIT_KBD_NODEF;
305 
306 #define netwib_lid(ot) {netwib_er(netwib_buf_append_fmt(&buf, " %{r 2;uint32} - ", i)); netwib_er(netwib_buf_append_device_dlttype(ot, &buf)); netwib_er(netwib_buf_append_fmt(&buf, "\n")); if (defaultdlttype == ot) defaultchoice = i; array[i++] = ot; }
307 
308   netwib_lid(NETWIB_DEVICE_DLTTYPE_NULL);
309   netwib_lid(NETWIB_DEVICE_DLTTYPE_ETHER);
310   netwib_lid(NETWIB_DEVICE_DLTTYPE_EN3MB);
311   netwib_lid(NETWIB_DEVICE_DLTTYPE_AX25);
312   netwib_lid(NETWIB_DEVICE_DLTTYPE_PRONET);
313   netwib_lid(NETWIB_DEVICE_DLTTYPE_CHAOS);
314   netwib_lid(NETWIB_DEVICE_DLTTYPE_IEEE802);
315   netwib_lid(NETWIB_DEVICE_DLTTYPE_ARCNET);
316   netwib_lid(NETWIB_DEVICE_DLTTYPE_SLIP);
317   netwib_lid(NETWIB_DEVICE_DLTTYPE_PPP);
318   netwib_lid(NETWIB_DEVICE_DLTTYPE_FDDI);
319   netwib_lid(NETWIB_DEVICE_DLTTYPE_ATM_RFC1483);
320   netwib_lid(NETWIB_DEVICE_DLTTYPE_RAW);
321   netwib_lid(NETWIB_DEVICE_DLTTYPE_RAW4);
322   netwib_lid(NETWIB_DEVICE_DLTTYPE_RAW6);
323   netwib_lid(NETWIB_DEVICE_DLTTYPE_SLIP_BSDOS);
324   netwib_lid(NETWIB_DEVICE_DLTTYPE_PPP_BSDOS);
325   netwib_lid(NETWIB_DEVICE_DLTTYPE_ATM_CLIP);
326   netwib_lid(NETWIB_DEVICE_DLTTYPE_PPP_SERIAL);
327   netwib_lid(NETWIB_DEVICE_DLTTYPE_PPP_ETHER);
328   netwib_lid(NETWIB_DEVICE_DLTTYPE_C_HDLC);
329   netwib_lid(NETWIB_DEVICE_DLTTYPE_IEEE802_11);
330   netwib_lid(NETWIB_DEVICE_DLTTYPE_LOOP);
331   netwib_lid(NETWIB_DEVICE_DLTTYPE_LINUX_SLL);
332   netwib_lid(NETWIB_DEVICE_DLTTYPE_LTALK);
333   netwib_lid(NETWIB_DEVICE_DLTTYPE_ECONET);
334   netwib_lid(NETWIB_DEVICE_DLTTYPE_PRISM_HEADER);
335   netwib_lid(NETWIB_DEVICE_DLTTYPE_AIRONET_HEADER);
336 
337   if (i >= NETWIB_PRIV_DEVICE_DLTTYPE_MAX) {
338     return(NETWIB_ERR_LOINTERNALERROR);
339   }
340   netwib_er(netwib_buf_display(&buf, NETWIB_ENCODETYPE_DATA));
341   netwib_er(netwib_buf_close(&buf));
342 
343   if (defaultdlttype == NETWIB_DEVICE_DLTTYPE_UNKNOWN) {
344     defaultchoice = NETWIB_UINT32_INIT_KBD_NODEF;
345   }
346 
347   netwib_er(netwib_buf_init_ext_string("Your choice", &msg));
348   netwib_er(netwib_uint32_init_kbd(&msg, 0, i-1, defaultchoice,
349                                  &choice));
350 
351   if (pdlttype != NULL) *pdlttype = array[choice];
352   return(NETWIB_ERR_OK);
353 }
354