Network Configuration Using a Configuration Editor

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware
Comments
Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Ethernut 1.3 H OK
Binaries
OK
Binaries
OK
Binaries
Ethernut 2.1 B OK
Binaries
OK
Binaries
OK
Binaries
Ethernut 3.0 E OK
Binaries
OK
Binaries
OK
Binaries
EIR 1.0 C Set jumper JP1 to UART mode. Configuration
Error.
Configuration
Error.
OK
Binaries
Sometimes ignores key strokes
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Description

The following application is a simple editor, which allows to modify the network configuration stored in non-volatile memory.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <sys/confnet.h>
  1. include <string.h>
  2. include <stdint.h>
  3. include <stdio.h>
  4. include <io.h>
  5. include <arpa/inet.h>
  6. include <netinet/if_ether.h>

/* Reads line from standard input. */ static int EditLine(char *prompt, char *line, int siz) {

   int ch;
   int pos = strlen(line);
   printf("%s: %s", prompt, line);
   for (;;) {
       ch = getchar();
       if (ch == 8) {
           if (pos) {
               pos--;
               printf("\b \b");
           }
       }
       else if (ch < ' ') {
           break;
       }
       else if (pos + 1 < siz) {
           putchar(ch);
           line[pos++] = ch;
       }
       else {
           putchar('\a');
       }
   }
   line[pos] = 0;
   putchar('\n');
   return 0;

}

/* Editor main routine. */ int main(void) {

   u_long baud = 115200;
   char buf[32];
   uint8_t *cp;
   uint32_t addr;
   char ch;
   /* Assign stdin and stdout to the default UART device. */
   NutRegisterDevice(&DEV_UART, 0, 0);
   freopen(DEV_UART_NAME, "w", stdout);
   freopen(DEV_UART_NAME, "r", stdin);    
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("Network Configuration Editor");
   for (;;) {
       /* Load configuration. */
       if (NutNetLoadConfig(DEV_ETHER_NAME)) {
           puts("\nNo configuration available");
           memcpy(confnet.cd_name, DEV_ETHER_NAME, sizeof(confnet.cd_name));
       } else {
           puts("\nConfiguration loaded");
       }
       /* Edit MAC address. */
       do {
           strcpy(buf, ether_ntoa(confnet.cdn_mac));
           EditLine("MAC Address", buf, 18);
           cp = ether_aton(buf);
       } while (cp == NULL);
       memcpy(confnet.cdn_mac, cp, 6);
       /* Edit IP address. */
       do {
           strcpy(buf, inet_ntoa(confnet.cdn_cip_addr));
           EditLine("IP Address", buf, 16);
           addr = inet_addr(buf);
       } while (addr == -1);
       confnet.cdn_cip_addr = addr;
       /* Edit IP mask. */
       do {
           strcpy(buf, inet_ntoa(confnet.cdn_ip_mask));
           EditLine("IP Mask", buf, 16);
           addr = inet_addr(buf);
       } while (addr == -1);
       confnet.cdn_ip_mask = addr;
       /* Edit IP gate. */
       do {
           strcpy(buf, inet_ntoa(confnet.cdn_gateway));
           EditLine("IP Gate", buf, 16);
           addr = inet_addr(buf);
       } while (addr == -1);
       confnet.cdn_gateway = addr;
       /* Prompt for saving. */
       printf("\nPress S to save this configuration ");
       /* Flush input buffer and read next character. */
       while (kbhit()) {
           ch = getchar();
       }
       ch = getchar();
       /* Save or discard edited configuration. */
       if (ch == 's' || ch == 'S') {
           if (NutNetSaveConfig()) {
               puts("Failed");
           } else {
               puts("Saved");
           }
       } else {
           puts("Discarded");
       }
   }
   return 0;

} </source>

Output

Network Configuration Editor
 
Configuration loaded
MAC Address: 00:06:98:30:02:76
IP Address: 192.168.192.111
IP Mask: 255.255.255.0
IP Mask: 0.0.0.0
 
Press S to save this configuration Saved

Now you can try it out by executing Network Configuration Using Stored Configuration.

See also

External Links

MAC address A Media Access Control address (MAC address) is a unique identifier assigned to network adapters for identification.

IP address A numerical identification that is assigned to devices participating in a computer network utilizing the Internet Protocol.

Dynamic Host Configuration Protocol A protocol used by networked devices to obtain the parameters necessary for operation in an Internet Protocol network.Template:Languages