adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 1 | /* |
| 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. |
adamdunkels | 06f897e | 2004-06-06 05:59:20 +0000 | [diff] [blame] | 14 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 15 | * 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 | * |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 32 | * $Id: netconf.c,v 1.13 2004/07/04 11:35:07 adamdunkels Exp $ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 36 | #include "ek.h" |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 37 | #include "uip.h" |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 38 | #include "uiplib.h" |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 39 | #include "uip_arp.h" |
adamdunkels | ef2704d | 2003-07-31 23:25:34 +0000 | [diff] [blame] | 40 | #include "resolv.h" |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 41 | #include "ctk.h" |
adamdunkels | 43c3d1d | 2003-04-17 19:00:00 +0000 | [diff] [blame] | 42 | #include "ctk-draw.h" |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 43 | |
| 44 | #include "loader.h" |
| 45 | |
| 46 | /* TCP/IP configuration window. */ |
| 47 | static struct ctk_window tcpipwindow; |
| 48 | |
| 49 | #ifdef WITH_ETHERNET |
| 50 | static struct ctk_label ipaddrlabel = |
| 51 | {CTK_LABEL(0, 1, 10, 1, "IP address")}; |
| 52 | static char ipaddr[17]; |
| 53 | static struct ctk_textentry ipaddrtextentry = |
| 54 | {CTK_TEXTENTRY(11, 1, 16, 1, ipaddr, 16)}; |
| 55 | static struct ctk_label netmasklabel = |
| 56 | {CTK_LABEL(0, 3, 10, 1, "Netmask")}; |
| 57 | static char netmask[17]; |
| 58 | static struct ctk_textentry netmasktextentry = |
| 59 | {CTK_TEXTENTRY(11, 3, 16, 1, netmask, 16)}; |
| 60 | static struct ctk_label gatewaylabel = |
| 61 | {CTK_LABEL(0, 5, 10, 1, "Gateway")}; |
| 62 | static char gateway[17]; |
| 63 | static struct ctk_textentry gatewaytextentry = |
| 64 | {CTK_TEXTENTRY(11, 5, 16, 1, gateway, 16)}; |
| 65 | static struct ctk_label dnsserverlabel = |
| 66 | {CTK_LABEL(0, 7, 10, 1, "DNS server")}; |
| 67 | static char dnsserver[17]; |
| 68 | static struct ctk_textentry dnsservertextentry = |
| 69 | {CTK_TEXTENTRY(11, 7, 16, 1, dnsserver, 16)}; |
| 70 | #else /* WITH_ETHERNET */ |
| 71 | static struct ctk_label ipaddrlabel = |
| 72 | {CTK_LABEL(0, 2, 10, 1, "IP address")}; |
| 73 | static char ipaddr[17]; |
| 74 | static struct ctk_textentry ipaddrtextentry = |
| 75 | {CTK_TEXTENTRY(11, 2, 16, 1, ipaddr, 16)}; |
| 76 | static struct ctk_label dnsserverlabel = |
| 77 | {CTK_LABEL(0, 4, 10, 1, "DNS server")}; |
| 78 | static char dnsserver[17]; |
| 79 | static struct ctk_textentry dnsservertextentry = |
| 80 | {CTK_TEXTENTRY(11, 4, 16, 1, dnsserver, 16)}; |
| 81 | #endif /* WITH_ETHERNET */ |
| 82 | |
| 83 | static struct ctk_button tcpipclosebutton = |
| 84 | {CTK_BUTTON(0, 9, 2, "Ok")}; |
| 85 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 86 | EK_EVENTHANDLER(netconf_eventhandler, ev, data); |
| 87 | EK_PROCESS(p, "Network configuration", EK_PRIO_NORMAL, |
| 88 | netconf_eventhandler, NULL, NULL); |
| 89 | static ek_id_t id = EK_ID_NONE; |
| 90 | |
| 91 | /*static DISPATCHER_SIGHANDLER(netconf_sighandler, s, data); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 92 | static struct dispatcher_proc p = |
| 93 | {DISPATCHER_PROC("Network config", NULL, netconf_sighandler, NULL)}; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 94 | static ek_id_t id;*/ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 95 | |
| 96 | |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame] | 97 | static void makestrings(void); |
| 98 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 99 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 100 | LOADER_INIT_FUNC(netconf_init, arg) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 101 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 102 | arg_free(arg); |
| 103 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 104 | if(id == EK_ID_NONE) { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 105 | id = ek_start(&p); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 106 | } |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 107 | } |
| 108 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame] | 109 | static char * |
| 110 | makebyte(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 | /*-----------------------------------------------------------------------------------*/ |
| 123 | static void |
| 124 | makeaddr(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 | /*-----------------------------------------------------------------------------------*/ |
| 136 | static void |
| 137 | makestrings(void) |
| 138 | { |
| 139 | u16_t addr[2], *addrptr; |
adamdunkels | 1690685 | 2003-04-24 17:16:41 +0000 | [diff] [blame] | 140 | |
| 141 | #ifdef WITH_UIP |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame] | 142 | 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 | |
adamdunkels | 1690685 | 2003-04-24 17:16:41 +0000 | [diff] [blame] | 158 | #endif /* WITH_UIP */ |
| 159 | |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame] | 160 | } |
| 161 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 162 | static void |
| 163 | nullterminate(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 | /*-----------------------------------------------------------------------------------*/ |
| 171 | static void |
| 172 | apply_tcpipconfig(void) |
| 173 | { |
| 174 | u16_t addr[2]; |
| 175 | |
| 176 | #ifdef WITH_UIP |
| 177 | nullterminate(ipaddr); |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 178 | if(uiplib_ipaddrconv(ipaddr, (unsigned char *)addr)) { |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 179 | uip_sethostaddr(addr); |
| 180 | } |
| 181 | |
| 182 | #ifdef WITH_ETHERNET |
| 183 | nullterminate(netmask); |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 184 | if(uiplib_ipaddrconv(netmask, (unsigned char *)addr)) { |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 185 | uip_setnetmask(addr); |
| 186 | } |
| 187 | |
| 188 | nullterminate(gateway); |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 189 | if(uiplib_ipaddrconv(gateway, (unsigned char *)addr)) { |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 190 | uip_setdraddr(addr); |
| 191 | } |
| 192 | #endif /* WITH_ETHERNET */ |
| 193 | |
| 194 | nullterminate(dnsserver); |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 195 | if(uiplib_ipaddrconv(dnsserver, (unsigned char *)addr)) { |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 196 | resolv_conf(addr); |
| 197 | } |
| 198 | #endif /* WITH_UIP */ |
| 199 | } |
| 200 | /*-----------------------------------------------------------------------------------*/ |
| 201 | static void |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 202 | netconf_quit(void) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 203 | { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 204 | ek_exit(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 205 | id = EK_ID_NONE; |
| 206 | LOADER_UNLOAD(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 207 | } |
| 208 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 209 | EK_EVENTHANDLER(netconf_eventhandler, ev, data) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 210 | { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 211 | 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) { |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 253 | if(data == (ek_data_t)&tcpipclosebutton) { |
| 254 | apply_tcpipconfig(); |
| 255 | ctk_window_close(&tcpipwindow); |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 256 | netconf_quit(); |
adamdunkels | ef2704d | 2003-07-31 23:25:34 +0000 | [diff] [blame] | 257 | /* ctk_desktop_redraw(tcpipwindow.desktop);*/ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 258 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 259 | } else if(ev == ctk_signal_window_close || |
| 260 | ev == EK_EVENT_REQUEST_EXIT) { |
adamdunkels | ef2704d | 2003-07-31 23:25:34 +0000 | [diff] [blame] | 261 | ctk_window_close(&tcpipwindow); |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 262 | netconf_quit(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | /*-----------------------------------------------------------------------------------*/ |