blob: 5a0795e25bb0300d182c900c749f0fdd970e0f7f [file] [log] [blame]
adamdunkels4292c862003-04-08 17:56:43 +00001/*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
adamdunkels06f897e2004-06-06 05:59:20 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkels4292c862003-04-08 17:56:43 +000015 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the Contiki desktop environment
31 *
adamdunkelsf2f8cb22004-07-04 11:35:07 +000032 * $Id: netconf.c,v 1.13 2004/07/04 11:35:07 adamdunkels Exp $
adamdunkels4292c862003-04-08 17:56:43 +000033 *
34 */
35
adamdunkelsf2f8cb22004-07-04 11:35:07 +000036#include "ek.h"
adamdunkels4292c862003-04-08 17:56:43 +000037#include "uip.h"
adamdunkelsdb300d22004-02-24 09:57:49 +000038#include "uiplib.h"
adamdunkels4292c862003-04-08 17:56:43 +000039#include "uip_arp.h"
adamdunkelsef2704d2003-07-31 23:25:34 +000040#include "resolv.h"
adamdunkels4292c862003-04-08 17:56:43 +000041#include "ctk.h"
adamdunkels43c3d1d2003-04-17 19:00:00 +000042#include "ctk-draw.h"
adamdunkels4292c862003-04-08 17:56:43 +000043
44#include "loader.h"
45
46/* TCP/IP configuration window. */
47static struct ctk_window tcpipwindow;
48
49#ifdef WITH_ETHERNET
50static struct ctk_label ipaddrlabel =
51 {CTK_LABEL(0, 1, 10, 1, "IP address")};
52static char ipaddr[17];
53static struct ctk_textentry ipaddrtextentry =
54 {CTK_TEXTENTRY(11, 1, 16, 1, ipaddr, 16)};
55static struct ctk_label netmasklabel =
56 {CTK_LABEL(0, 3, 10, 1, "Netmask")};
57static char netmask[17];
58static struct ctk_textentry netmasktextentry =
59 {CTK_TEXTENTRY(11, 3, 16, 1, netmask, 16)};
60static struct ctk_label gatewaylabel =
61 {CTK_LABEL(0, 5, 10, 1, "Gateway")};
62static char gateway[17];
63static struct ctk_textentry gatewaytextentry =
64 {CTK_TEXTENTRY(11, 5, 16, 1, gateway, 16)};
65static struct ctk_label dnsserverlabel =
66 {CTK_LABEL(0, 7, 10, 1, "DNS server")};
67static char dnsserver[17];
68static struct ctk_textentry dnsservertextentry =
69 {CTK_TEXTENTRY(11, 7, 16, 1, dnsserver, 16)};
70#else /* WITH_ETHERNET */
71static struct ctk_label ipaddrlabel =
72 {CTK_LABEL(0, 2, 10, 1, "IP address")};
73static char ipaddr[17];
74static struct ctk_textentry ipaddrtextentry =
75 {CTK_TEXTENTRY(11, 2, 16, 1, ipaddr, 16)};
76static struct ctk_label dnsserverlabel =
77 {CTK_LABEL(0, 4, 10, 1, "DNS server")};
78static char dnsserver[17];
79static struct ctk_textentry dnsservertextentry =
80 {CTK_TEXTENTRY(11, 4, 16, 1, dnsserver, 16)};
81#endif /* WITH_ETHERNET */
82
83static struct ctk_button tcpipclosebutton =
84 {CTK_BUTTON(0, 9, 2, "Ok")};
85
adamdunkelsf2f8cb22004-07-04 11:35:07 +000086EK_EVENTHANDLER(netconf_eventhandler, ev, data);
87EK_PROCESS(p, "Network configuration", EK_PRIO_NORMAL,
88 netconf_eventhandler, NULL, NULL);
89static ek_id_t id = EK_ID_NONE;
90
91/*static DISPATCHER_SIGHANDLER(netconf_sighandler, s, data);
adamdunkels4292c862003-04-08 17:56:43 +000092static struct dispatcher_proc p =
93 {DISPATCHER_PROC("Network config", NULL, netconf_sighandler, NULL)};
adamdunkelsf2f8cb22004-07-04 11:35:07 +000094 static ek_id_t id;*/
adamdunkels4292c862003-04-08 17:56:43 +000095
96
adamdunkelsc4db2872003-04-16 18:27:33 +000097static void makestrings(void);
98
adamdunkels4292c862003-04-08 17:56:43 +000099/*-----------------------------------------------------------------------------------*/
adamdunkels8bb5cca2003-08-24 22:41:31 +0000100LOADER_INIT_FUNC(netconf_init, arg)
adamdunkels4292c862003-04-08 17:56:43 +0000101{
adamdunkels8bb5cca2003-08-24 22:41:31 +0000102 arg_free(arg);
103
adamdunkels4292c862003-04-08 17:56:43 +0000104 if(id == EK_ID_NONE) {
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000105 id = ek_start(&p);
adamdunkels4292c862003-04-08 17:56:43 +0000106 }
adamdunkels4292c862003-04-08 17:56:43 +0000107}
108/*-----------------------------------------------------------------------------------*/
adamdunkelsc4db2872003-04-16 18:27:33 +0000109static char *
110makebyte(u8_t byte, char *str)
111{
112 if(byte >= 100) {
113 *str++ = (byte / 100 ) % 10 + '0';
114 }
115 if(byte >= 10) {
116 *str++ = (byte / 10) % 10 + '0';
117 }
118 *str++ = (byte % 10) + '0';
119
120 return str;
121}
122/*-----------------------------------------------------------------------------------*/
123static void
124makeaddr(u16_t *addr, char *str)
125{
126 str = makebyte(HTONS(addr[0]) >> 8, str);
127 *str++ = '.';
128 str = makebyte(HTONS(addr[0]) & 0xff, str);
129 *str++ = '.';
130 str = makebyte(HTONS(addr[1]) >> 8, str);
131 *str++ = '.';
132 str = makebyte(HTONS(addr[1]) & 0xff, str);
133 *str++ = 0;
134}
135/*-----------------------------------------------------------------------------------*/
136static void
137makestrings(void)
138{
139 u16_t addr[2], *addrptr;
adamdunkels16906852003-04-24 17:16:41 +0000140
141#ifdef WITH_UIP
adamdunkelsc4db2872003-04-16 18:27:33 +0000142 uip_gethostaddr(addr);
143 makeaddr(addr, ipaddr);
144
145#ifdef WITH_ETHERNET
146 uip_getnetmask(addr);
147 makeaddr(addr, netmask);
148
149 uip_getdraddr(addr);
150 makeaddr(addr, gateway);
151#endif /* WITH_ETHERNET */
152
153 addrptr = resolv_getserver();
154 if(addrptr != NULL) {
155 makeaddr(addrptr, dnsserver);
156 }
157
adamdunkels16906852003-04-24 17:16:41 +0000158#endif /* WITH_UIP */
159
adamdunkelsc4db2872003-04-16 18:27:33 +0000160}
161/*-----------------------------------------------------------------------------------*/
adamdunkels4292c862003-04-08 17:56:43 +0000162static void
163nullterminate(char *cptr)
164{
165 /* Find the first space character in the ipaddr and put a zero there
166 to end the string. */
167 for(; *cptr != ' ' && *cptr != 0; ++cptr);
168 *cptr = 0;
169}
170/*-----------------------------------------------------------------------------------*/
171static void
172apply_tcpipconfig(void)
173{
174 u16_t addr[2];
175
176#ifdef WITH_UIP
177 nullterminate(ipaddr);
adamdunkelsdb300d22004-02-24 09:57:49 +0000178 if(uiplib_ipaddrconv(ipaddr, (unsigned char *)addr)) {
adamdunkels4292c862003-04-08 17:56:43 +0000179 uip_sethostaddr(addr);
180 }
181
182#ifdef WITH_ETHERNET
183 nullterminate(netmask);
adamdunkelsdb300d22004-02-24 09:57:49 +0000184 if(uiplib_ipaddrconv(netmask, (unsigned char *)addr)) {
adamdunkels4292c862003-04-08 17:56:43 +0000185 uip_setnetmask(addr);
186 }
187
188 nullterminate(gateway);
adamdunkelsdb300d22004-02-24 09:57:49 +0000189 if(uiplib_ipaddrconv(gateway, (unsigned char *)addr)) {
adamdunkels4292c862003-04-08 17:56:43 +0000190 uip_setdraddr(addr);
191 }
192#endif /* WITH_ETHERNET */
193
194 nullterminate(dnsserver);
adamdunkelsdb300d22004-02-24 09:57:49 +0000195 if(uiplib_ipaddrconv(dnsserver, (unsigned char *)addr)) {
adamdunkels4292c862003-04-08 17:56:43 +0000196 resolv_conf(addr);
197 }
198#endif /* WITH_UIP */
199}
200/*-----------------------------------------------------------------------------------*/
201static void
adamdunkels0137b442003-04-08 23:27:33 +0000202netconf_quit(void)
adamdunkels4292c862003-04-08 17:56:43 +0000203{
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000204 ek_exit();
adamdunkels4292c862003-04-08 17:56:43 +0000205 id = EK_ID_NONE;
206 LOADER_UNLOAD();
adamdunkels4292c862003-04-08 17:56:43 +0000207}
208/*-----------------------------------------------------------------------------------*/
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000209EK_EVENTHANDLER(netconf_eventhandler, ev, data)
adamdunkels4292c862003-04-08 17:56:43 +0000210{
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000211 EK_EVENTHANDLER_ARGS(ev, data);
212
213 if(ev == EK_EVENT_INIT) {
214 /* Create TCP/IP configuration window. */
215 ctk_window_new(&tcpipwindow, 30, 10, "TCP/IP config");
216 /* if(ctk_desktop_width(tcpipwindow.desktop) < 30) {
217 ctk_window_move(&tcpipwindow, 0,
218 (ctk_desktop_height(tcpipwindow.desktop) - 10) / 2 - 2);
219 } else {
220 ctk_window_move(&tcpipwindow,
221 (ctk_desktop_width(tcpipwindow.desktop) - 30) / 2,
222 (ctk_desktop_height(tcpipwindow.desktop) - 10) / 2 - 2);
223 }*/
224
225#ifdef WITH_ETHERNET
226 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel);
227 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry);
228 CTK_WIDGET_ADD(&tcpipwindow, &netmasklabel);
229 CTK_WIDGET_ADD(&tcpipwindow, &netmasktextentry);
230 CTK_WIDGET_ADD(&tcpipwindow, &gatewaylabel);
231 CTK_WIDGET_ADD(&tcpipwindow, &gatewaytextentry);
232 CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel);
233 CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry);
234#else
235 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel);
236 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry);
237 CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel);
238 CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry);
239#endif /* WITH_ETHERNET */
240
241 CTK_WIDGET_ADD(&tcpipwindow, &tcpipclosebutton);
242
243 CTK_WIDGET_FOCUS(&tcpipwindow, &ipaddrtextentry);
244
245 /* Fill the configuration strings with values from the current
246 configuration */
247 makestrings();
248
249 /* dispatcher_listen(ctk_signal_button_activate);
250 dispatcher_listen(ctk_signal_window_close);*/
251 ctk_window_open(&tcpipwindow);
252 } else if(ev == ctk_signal_button_activate) {
adamdunkels4292c862003-04-08 17:56:43 +0000253 if(data == (ek_data_t)&tcpipclosebutton) {
254 apply_tcpipconfig();
255 ctk_window_close(&tcpipwindow);
adamdunkels0137b442003-04-08 23:27:33 +0000256 netconf_quit();
adamdunkelsef2704d2003-07-31 23:25:34 +0000257 /* ctk_desktop_redraw(tcpipwindow.desktop);*/
adamdunkels4292c862003-04-08 17:56:43 +0000258 }
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000259 } else if(ev == ctk_signal_window_close ||
260 ev == EK_EVENT_REQUEST_EXIT) {
adamdunkelsef2704d2003-07-31 23:25:34 +0000261 ctk_window_close(&tcpipwindow);
adamdunkels0137b442003-04-08 23:27:33 +0000262 netconf_quit();
adamdunkels4292c862003-04-08 17:56:43 +0000263 }
264}
265/*-----------------------------------------------------------------------------------*/