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. |
| 14 | * 3. All advertising materials mentioning features or use of this |
| 15 | * software must display the following acknowledgement: |
| 16 | * This product includes software developed by Adam Dunkels. |
| 17 | * 4. The name of the author may not be used to endorse or promote |
| 18 | * products derived from this software without specific prior |
| 19 | * written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 22 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 25 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * This file is part of the Contiki desktop environment |
| 34 | * |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame^] | 35 | * $Id: netconf.c,v 1.5 2003/04/16 18:27:33 adamdunkels Exp $ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include "uip_main.h" |
| 40 | #include "uip.h" |
| 41 | #include "uip_arp.h" |
| 42 | #include "ctk.h" |
| 43 | #include "dispatcher.h" |
| 44 | |
| 45 | #include "loader.h" |
| 46 | |
| 47 | /* TCP/IP configuration window. */ |
| 48 | static struct ctk_window tcpipwindow; |
| 49 | |
| 50 | #ifdef WITH_ETHERNET |
| 51 | static struct ctk_label ipaddrlabel = |
| 52 | {CTK_LABEL(0, 1, 10, 1, "IP address")}; |
| 53 | static char ipaddr[17]; |
| 54 | static struct ctk_textentry ipaddrtextentry = |
| 55 | {CTK_TEXTENTRY(11, 1, 16, 1, ipaddr, 16)}; |
| 56 | static struct ctk_label netmasklabel = |
| 57 | {CTK_LABEL(0, 3, 10, 1, "Netmask")}; |
| 58 | static char netmask[17]; |
| 59 | static struct ctk_textentry netmasktextentry = |
| 60 | {CTK_TEXTENTRY(11, 3, 16, 1, netmask, 16)}; |
| 61 | static struct ctk_label gatewaylabel = |
| 62 | {CTK_LABEL(0, 5, 10, 1, "Gateway")}; |
| 63 | static char gateway[17]; |
| 64 | static struct ctk_textentry gatewaytextentry = |
| 65 | {CTK_TEXTENTRY(11, 5, 16, 1, gateway, 16)}; |
| 66 | static struct ctk_label dnsserverlabel = |
| 67 | {CTK_LABEL(0, 7, 10, 1, "DNS server")}; |
| 68 | static char dnsserver[17]; |
| 69 | static struct ctk_textentry dnsservertextentry = |
| 70 | {CTK_TEXTENTRY(11, 7, 16, 1, dnsserver, 16)}; |
| 71 | #else /* WITH_ETHERNET */ |
| 72 | static struct ctk_label ipaddrlabel = |
| 73 | {CTK_LABEL(0, 2, 10, 1, "IP address")}; |
| 74 | static char ipaddr[17]; |
| 75 | static struct ctk_textentry ipaddrtextentry = |
| 76 | {CTK_TEXTENTRY(11, 2, 16, 1, ipaddr, 16)}; |
| 77 | static struct ctk_label dnsserverlabel = |
| 78 | {CTK_LABEL(0, 4, 10, 1, "DNS server")}; |
| 79 | static char dnsserver[17]; |
| 80 | static struct ctk_textentry dnsservertextentry = |
| 81 | {CTK_TEXTENTRY(11, 4, 16, 1, dnsserver, 16)}; |
| 82 | #endif /* WITH_ETHERNET */ |
| 83 | |
| 84 | static struct ctk_button tcpipclosebutton = |
| 85 | {CTK_BUTTON(0, 9, 2, "Ok")}; |
| 86 | |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 87 | static DISPATCHER_SIGHANDLER(netconf_sighandler, s, data); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 88 | static struct dispatcher_proc p = |
| 89 | {DISPATCHER_PROC("Network config", NULL, netconf_sighandler, NULL)}; |
| 90 | static ek_id_t id; |
| 91 | |
| 92 | |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame^] | 93 | static void makestrings(void); |
| 94 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 95 | /*-----------------------------------------------------------------------------------*/ |
| 96 | LOADER_INIT_FUNC(netconf_init) |
| 97 | { |
| 98 | if(id == EK_ID_NONE) { |
| 99 | id = dispatcher_start(&p); |
| 100 | |
| 101 | /* Create TCP/IP configuration window. */ |
| 102 | ctk_window_new(&tcpipwindow, 30, 10, "TCP/IP config"); |
adamdunkels | 4b7a7ab | 2003-04-09 19:22:19 +0000 | [diff] [blame] | 103 | if(ctk_draw_width() < 30) { |
| 104 | ctk_window_move(&tcpipwindow, 0, |
| 105 | (ctk_draw_height() - 10) / 2 - 2); |
| 106 | } else { |
| 107 | ctk_window_move(&tcpipwindow, |
| 108 | (ctk_draw_width() - 30) / 2, |
| 109 | (ctk_draw_height() - 10) / 2 - 2); |
| 110 | } |
| 111 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | #ifdef WITH_ETHERNET |
| 115 | CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel); |
| 116 | CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry); |
| 117 | CTK_WIDGET_ADD(&tcpipwindow, &netmasklabel); |
| 118 | CTK_WIDGET_ADD(&tcpipwindow, &netmasktextentry); |
| 119 | CTK_WIDGET_ADD(&tcpipwindow, &gatewaylabel); |
| 120 | CTK_WIDGET_ADD(&tcpipwindow, &gatewaytextentry); |
| 121 | CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel); |
| 122 | CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry); |
| 123 | #else |
| 124 | CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel); |
| 125 | CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry); |
| 126 | CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel); |
| 127 | CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry); |
| 128 | #endif /* WITH_ETHERNET */ |
| 129 | |
| 130 | CTK_WIDGET_ADD(&tcpipwindow, &tcpipclosebutton); |
| 131 | |
| 132 | CTK_WIDGET_FOCUS(&tcpipwindow, &ipaddrtextentry); |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame^] | 133 | |
| 134 | /* Fill the configuration strings with values from the current |
| 135 | configuration */ |
| 136 | makestrings(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 137 | |
| 138 | dispatcher_listen(ctk_signal_button_activate); |
| 139 | dispatcher_listen(ctk_signal_window_close); |
| 140 | } |
| 141 | ctk_window_open(&tcpipwindow); |
| 142 | } |
| 143 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | c4db287 | 2003-04-16 18:27:33 +0000 | [diff] [blame^] | 144 | static char * |
| 145 | makebyte(u8_t byte, char *str) |
| 146 | { |
| 147 | if(byte >= 100) { |
| 148 | *str++ = (byte / 100 ) % 10 + '0'; |
| 149 | } |
| 150 | if(byte >= 10) { |
| 151 | *str++ = (byte / 10) % 10 + '0'; |
| 152 | } |
| 153 | *str++ = (byte % 10) + '0'; |
| 154 | |
| 155 | return str; |
| 156 | } |
| 157 | /*-----------------------------------------------------------------------------------*/ |
| 158 | static void |
| 159 | makeaddr(u16_t *addr, char *str) |
| 160 | { |
| 161 | str = makebyte(HTONS(addr[0]) >> 8, str); |
| 162 | *str++ = '.'; |
| 163 | str = makebyte(HTONS(addr[0]) & 0xff, str); |
| 164 | *str++ = '.'; |
| 165 | str = makebyte(HTONS(addr[1]) >> 8, str); |
| 166 | *str++ = '.'; |
| 167 | str = makebyte(HTONS(addr[1]) & 0xff, str); |
| 168 | *str++ = 0; |
| 169 | } |
| 170 | /*-----------------------------------------------------------------------------------*/ |
| 171 | static void |
| 172 | makestrings(void) |
| 173 | { |
| 174 | u16_t addr[2], *addrptr; |
| 175 | |
| 176 | uip_gethostaddr(addr); |
| 177 | makeaddr(addr, ipaddr); |
| 178 | |
| 179 | #ifdef WITH_ETHERNET |
| 180 | uip_getnetmask(addr); |
| 181 | makeaddr(addr, netmask); |
| 182 | |
| 183 | uip_getdraddr(addr); |
| 184 | makeaddr(addr, gateway); |
| 185 | #endif /* WITH_ETHERNET */ |
| 186 | |
| 187 | addrptr = resolv_getserver(); |
| 188 | if(addrptr != NULL) { |
| 189 | makeaddr(addrptr, dnsserver); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | } |
| 194 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 195 | static void |
| 196 | nullterminate(char *cptr) |
| 197 | { |
| 198 | /* Find the first space character in the ipaddr and put a zero there |
| 199 | to end the string. */ |
| 200 | for(; *cptr != ' ' && *cptr != 0; ++cptr); |
| 201 | *cptr = 0; |
| 202 | } |
| 203 | /*-----------------------------------------------------------------------------------*/ |
| 204 | static void |
| 205 | apply_tcpipconfig(void) |
| 206 | { |
| 207 | u16_t addr[2]; |
| 208 | |
| 209 | #ifdef WITH_UIP |
| 210 | nullterminate(ipaddr); |
| 211 | if(uip_main_ipaddrconv(ipaddr, (unsigned char *)addr)) { |
| 212 | uip_sethostaddr(addr); |
| 213 | } |
| 214 | |
| 215 | #ifdef WITH_ETHERNET |
| 216 | nullterminate(netmask); |
| 217 | if(uip_main_ipaddrconv(netmask, (unsigned char *)addr)) { |
| 218 | uip_setnetmask(addr); |
| 219 | } |
| 220 | |
| 221 | nullterminate(gateway); |
| 222 | if(uip_main_ipaddrconv(gateway, (unsigned char *)addr)) { |
| 223 | uip_setdraddr(addr); |
| 224 | } |
| 225 | #endif /* WITH_ETHERNET */ |
| 226 | |
| 227 | nullterminate(dnsserver); |
| 228 | if(uip_main_ipaddrconv(dnsserver, (unsigned char *)addr)) { |
| 229 | resolv_conf(addr); |
| 230 | } |
| 231 | #endif /* WITH_UIP */ |
| 232 | } |
| 233 | /*-----------------------------------------------------------------------------------*/ |
| 234 | static void |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 235 | netconf_quit(void) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 236 | { |
| 237 | dispatcher_exit(&p); |
| 238 | id = EK_ID_NONE; |
| 239 | LOADER_UNLOAD(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 240 | } |
| 241 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 242 | static |
| 243 | DISPATCHER_SIGHANDLER(netconf_sighandler, s, data) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 244 | { |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 245 | DISPATCHER_SIGHANDLER_ARGS(s, data); |
| 246 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 247 | if(s == ctk_signal_button_activate) { |
| 248 | if(data == (ek_data_t)&tcpipclosebutton) { |
| 249 | apply_tcpipconfig(); |
| 250 | ctk_window_close(&tcpipwindow); |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 251 | netconf_quit(); |
| 252 | ctk_redraw(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 253 | } |
| 254 | } else if(s == ctk_signal_window_close) { |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 255 | netconf_quit(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | /*-----------------------------------------------------------------------------------*/ |