Deleted Added
full compact
1/*
2 * $Id$
3 *
4 * Copyright (c) 1995
5 * Gary J Palmer. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * verbatim and that no modifications are made prior to this
13 * point in the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gary J Palmer
20 * for the FreeBSD Project.
21 * 4. The name of Gary J Palmer or the FreeBSD Project may not be used to
22 * endorse or promote products derived from this software without specific
23 * prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY GARY J PALMER ``AS IS'' AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL GARY J PALMER BE LIABLE FOR ANY
29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <sys/param.h>
43#include <string.h>
44#include <dialog.h>
45#include "ui_objects.h"
46#include "dir.h"
47#include "dialog.priv.h"
48#include "colors.h"
49#include "rc.h"
50
51static char hostname[256], domainname[256],
52 ipaddr[32], netmask[32], gateway[32],
53 dns[32], extras[256];
54static int okbutton, cancelbutton;
55
56#define TCP_DIALOG_Y 0
57#define TCP_DIALOG_X 8
58#define TCP_DIALOG_WIDTH COLS - 16
59#define TCP_DIALOG_HEIGHT LINES - 2
60
61/* The names of the available interfaces, for the list */
62char *iface_names[MAX_INTERFACE];
63
64typedef struct _interface {
65 char *name;
66 Device *dev;
67} Interface;
68
69typedef struct _layout {
70 int y;
71 int x;
72 int len;
73 int maxlen;
74 char *prompt;
75 char *help;
76 void *var;
77 int type;
78 void *obj;
79} Layout;
80
81static Layout layout[] = {
82{ 2, 2, 25, 255,
83 "Host name:", "The name of your machine on a network, e.g. foo.bar.com",
84 hostname, STRINGOBJ, NULL },
85{ 2, 35, 20, 255,
86 "Domain name:",
87 "The name of the domain that your machine is in, e.g. bar.com",
88 domainname, STRINGOBJ, NULL },
89{ 5, 2, 18, 15,
90 "Gateway:",
91 "IP address of host forwarding packets to non-local hosts or nets",
92 gateway, STRINGOBJ, NULL },
93{ 5, 35, 18, 15,
94 "Name server:", "IP address of your local DNS server",
95 dns, STRINGOBJ, NULL },
96{ 8, 2, 10, 6,
97 "Interface:", "One of potentially several network interfaces",
98 ifaces, LISTOBJ, NULL },
99{ 14, 2, 18, 15,
100 "IP Address:",
101 "The IP address to be used for your host - use 127.0.0.1 for loopback",
102 ipaddr, STRINGOBJ, NULL },
103{ 14, 35, 18, 15,
104 "Netmask:",
105 "The netmask for your network, e.g. 0xffffff00 for a class C network",
106 netmask, STRINGOBJ, NULL },
107{ 16, 2, 50, 255,
108 "Extra options:",
109 "Any options to ifconfig you'd like to specify manually",
110 extras, STRINGOBJ, NULL },
111{ 18, 2, 0, 0,
112 "OK", "Select this if you are happy with these settings",
113 &okbutton, BUTTONOBJ, NULL },
114{ 18, 15, 0, 0,
115 "CANCEL", "Select this if you wish to cancel this screen",
116 &cancelbutton, BUTTONOBJ, NULL },
117{ NULL },
118};
119
120#define _validByte(b) ((b) > 0 && (b) < 255)
121
122static void
123feepout(char *msg)
124{
125 beep();
126 msgConfirm(msg);
127 dialog_clear();
128 refresh();
129}
130
131static int
132verifyIP(char *ip)
133{
134 int a, b, c, d;
135
136 if (ip && sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
137 _validByte(a) && _validByte(b) && _validByte(c) &&
138 _validByte(d))
139 return 1;
140 else
141 return 0;
142}
143
144static int
145verifySettings(void)
146{
147 if (!hostname[0])
148 feepout("Must specify a host name of some sort!");
149 else if (!verifyIP(ipaddr))
150 feepout("Invalid or missing value for IP address");
151 else if (gateway[0] && !verifyIP(gateway))
152 feepout("Invalid gateway IP address specified");
153 else if (dns[0] && !verifyIP(dns))
154 feepout("Invalid name server IP address specified");
155 else if (netmask[0] < '0' || netmask[0] > '9')
156 feepout("Invalid or missing netmask");
157 else
158 return 1;
159 return 0;
160}
161
162/* Call this to initialize the TCP dialog */
163void
164/* This is it - how to get TCP setup values */
165void
166tcpOpenDialog(void)
167{
168 WINDOW *ds_win;
169 ComposeObj *obj = NULL;
170 ComposeObj *first, *last;
171 int n=0, quit=FALSE, cancel=FALSE, ret,
172 max;
173 char *tmp;
174
175 ds_win = newwin(LINES, COLS, 0, 0);
176 if (ds_win == 0) {
177 msgFatal("Cannot open TCP/IP dialog window!!");
178 exit(1);
179 }
180 draw_box(ds_win, TCP_DIALOG_Y, TCP_DIALOG_X,
181 TCP_DIALOG_HEIGHT, TCP_DIALOG_WIDTH,
182 dialog_attr, border_attr);
183 wattrset(ds_win, dialog_attr);
184 mvwaddstr(ds_win, TCP_DIALOG_Y, TCP_DIALOG_X + 20,
185 " Network Configuration ");
186
187 bzero(ipaddr, sizeof(ipaddr));
188 bzero(netmask, sizeof(netmask));
189 bzero(extras, sizeof(extras));
190
191 tmp = getenv(VAR_HOSTNAME);
192 if (tmp)
193 strcpy(hostname, tmp);
194 else
195 bzero(hostname, sizeof(hostname));
196 tmp = getenv(VAR_DOMAINNAME);
197 if (tmp)
198 strcpy(domainname, tmp);
199 else
200 bzero(domainname, sizeof(domainname));
201 tmp = getenv(VAR_GATEWAY);
202 if (tmp)
203 strcpy(gateway, tmp);
204 else
205 bzero(gateway, sizeof(gateway));
206 tmp = getenv(VAR_NAMESERVER);
207 if (tmp)
208 strcpy(dns, tmp);
209 else
210 bzero(dns, sizeof(dns));
211
212#define lt layout[n]
213 while (lt.help != NULL) {
214 switch (lt.type) {
215 case STRINGOBJ:
216 lt.obj = NewStringObj(ds_win, lt.prompt, lt.var,
217 lt.y + TCP_DIALOG_Y, lt.x + TCP_DIALOG_X,
218 lt.len, lt.maxlen);
219 break;
220
221 case BUTTONOBJ:
222 lt.obj = NewButtonObj(ds_win, lt.prompt, lt.var,
223 lt.y + TCP_DIALOG_Y, lt.x + TCP_DIALOG_X);
224 break;
225
226 case LISTOBJ:
227 lt.obj = NewListObj(ds_win, lt.prompt, lt.var, "lo0",
228 lt.y + TCP_DIALOG_Y, lt.x + TCP_DIALOG_X,
229 4, 12, 1);
230 default:
231 printf("Don't support this object yet!\n");
232 end_dialog();
233 exit(1);
234 }
235 AddObj(&obj, lt.type, (void *) lt.obj);
236 n++;
237 }
238 max = n-1;
239
240 last = obj;
241 while (last->next)
242 last = last->next;
243
244 /* find the first object in the list */
245 first = obj;
246 while (first->prev)
247 first = first->prev;
248
249 n = 0;
250 while (quit != TRUE) {
251 char help_line[80];
252 int i, len = strlen(lt.help);
253
254 for (i = 0; i < 79; i++)
255 help_line[i] = (i < len) ? lt.help[i] : ' ';
256 help_line[i] = '\0';
257 use_helpline(help_line);
258 display_helpline(ds_win, LINES - 1, COLS - 1);
259
260 ret = PollObj(&obj);
261
262 switch (ret) {
263 case SEL_ESC:
264 quit=TRUE;
265 cancel=TRUE;
266 break;
267
268 case KEY_UP:
269 if (obj->prev !=NULL ) {
270 obj = obj->prev;
271 --n;
272 }
273 else {
274 obj = last;
275 n = max;
276 }
277 break;
278
279 case KEY_DOWN:
280 if (obj->next != NULL) {
281 obj = obj->next;
282 ++n;
283 }
284 else {
285 obj = first;
286 n=0;
287 }
288 break;
289
290 case SEL_BUTTON:
291 if (verifySettings())
292 quit=TRUE;
293 break;
294
295 case SEL_CR:
296 case SEL_TAB:
297 if (n < max)
298 ++n;
299 else
300 n = 0;
301 break;
302
303 case SEL_BACKTAB:
304 if (n)
305 --n;
306 else
307 n = max;
308 break;
309
310 default:
311 beep();
312 }
313
314 if (n == 1) {
315 if ((tmp = index(hostname, '.')) != NULL) {
316 strncpy(domainname, tmp + 1, strlen(tmp + 1));
317 RefreshStringObj(layout[1].obj);
318 }
319 }
320 }
321 if (!cancel) {
322 variable_set2("hostname", hostname);
323 variable_set2("domainname", domainname);
324 variable_set2("ip_addr", ipaddr);
325 variable_set2("ip_gateway", gateway);
326}