⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wininit.c

📁 picoos源码。The RTOS and the TCP/IP stack will be built automatically.
💻 C
字号:
/*
 *  Copyright (c) 2005, Dennis Kuschel.
 *  All rights reserved. 
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. The name of the author may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission. 
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 *  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 *  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

#include "../../lwipport/netif/pktifw32/pktdrv.h"



void printIPaddr(unsigned int *ipaddr)
{
  unsigned char *adr = (unsigned char*) ipaddr;
  printf("%u.%u.%u.%u",
    (unsigned)adr[0], (unsigned)adr[1], (unsigned)adr[2], (unsigned)adr[3]);
}


int network_setup(unsigned int *ipaddr,
                  unsigned int *netmask,
                  unsigned int *gatew)
{
  HKEY  regh;
  LONG  err;
  DWORD vtype, vsize;
  unsigned i1, i2, i3, i4;
  int  adapter, regopen;
  int  havedefault;
  char buffer[20];
  char tmp[4];
  char c;

  printf("\nATTENTION!  You should run this program on Windows NT / 2000 / XP only!\n");
  printf("            You must have administrator rights on the machine!\n");

  havedefault = 0;
  
  /* try to read defaults from the registry */
  regopen = 0;
  err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\LWIP", 0, 
                     KEY_CREATE_SUB_KEY | KEY_READ | KEY_WRITE, &regh);
  if (err == ERROR_SUCCESS)
  {
    regopen = 1;
    vsize = 4;
    err = RegQueryValueEx(regh, "adapter", NULL,
                          &vtype, (unsigned char*)&adapter, &vsize);
    if ((vtype != REG_DWORD) || (vsize != 4)) err = 1;
  }
  if (err == ERROR_SUCCESS)
  {
    err = RegQueryValueEx(regh, "ipaddr", NULL,
                          &vtype, (unsigned char*)ipaddr, &vsize);
    if ((vtype != REG_DWORD) || (vsize != 4)) err = 1;
  }
  if (err == ERROR_SUCCESS)
  {
    err = RegQueryValueEx(regh, "netmask", NULL,
                          &vtype, (unsigned char*)netmask, &vsize);
    if ((vtype != REG_DWORD) || (vsize != 4)) err = 1;
  }
  if (err == ERROR_SUCCESS)
  {
    err = RegQueryValueEx(regh, "gateway", NULL,
                          &vtype, (unsigned char*)gatew, &vsize);
    if ((vtype != REG_DWORD) || (vsize != 4)) err = 1;
  }
  if (err == ERROR_SUCCESS) {
    havedefault = 1;  
  } else {
    adapter = -1;
  }
  
  if (havedefault)
  {
    printf("\n\nFollowing default values were found:\n\n");
    printf("Adapter %i  IP ", adapter);
    printIPaddr(ipaddr);
    printf("  Mask ");
    printIPaddr(netmask);
    printf("  GW ");
    printIPaddr(gatew);
    printf("\n\nPress [ENTER] to use this values or any other key\n");
    printf("to enter different settings: ");
    c = _getch();
    if (c == 13)
    {
      printf("(use default settings)\n\n");
    }
    else
    {
      printf("\n");
      havedefault = 0;
    }
  }

  if (havedefault)
  {
    if (pktdrv_start(adapter) < 0)
      return -1;
  }
  else
  {
    /* initialize the network adapter */
    adapter = pktdrv_start(-1);
    if (adapter < 0)
      return -1;

    do
    {
      printf("\n\nPlease enter the IP-address that shall be assgined to lwIP.\n");
      printf("The IP address must be in the same subnet this host machine is,\n");
      printf("but it must not be the same IP address the host machine has.\n\n");
      printf("IP address  : ");
      buffer[0] = 18;  /* Maximum characters in 1st byte */
      do {
        i1 = i2 = i3 = i4 = 0;
        sscanf(_cgets(buffer), "%u.%u.%u.%u", &i1, &i2, &i3, &i4);
        tmp[0]=(char)i1; tmp[1]=(char)i2; tmp[2]=(char)i3; tmp[3]=(char)i4;
        *ipaddr = *((unsigned int*)tmp);
      } while (buffer[1] == 0);
      printf("Network Mask: ");
      do {
        i1 = i2 = i3 = i4 = 0;
        sscanf(_cgets(buffer), "%u.%u.%u.%u", &i1, &i2, &i3, &i4);
        tmp[0]=(char)i1; tmp[1]=(char)i2; tmp[2]=(char)i3; tmp[3]=(char)i4;
        *netmask = *((unsigned int*)tmp);
      } while (buffer[1] == 0);
      printf("Gateway IP  : ");
      do {
        i1 = i2 = i3 = i4 = 0;
        sscanf(_cgets(buffer), "%u.%u.%u.%u", &i1, &i2, &i3, &i4);
        tmp[0]=(char)i1; tmp[1]=(char)i2; tmp[2]=(char)i3; tmp[3]=(char)i4;
        *gatew = *((unsigned int*)tmp);
      } while (buffer[1] == 0);
  
      printf("\n\nYou have entered IP ");
      printIPaddr(ipaddr);
      printf("  Mask ");
      printIPaddr(netmask);
      printf("  GW ");
      printIPaddr(gatew);
      printf("\n\nIs this correct? "
             "Enter [Y]=Yes, [S]=Yes & Store as default, [N]=No : ");
      do
      {
        c = toupper(_getch());
      }
      while ((c != 'Y') && (c != 'S') && (c != 'N'));
      printf("%c\n\n", c);
    }
    while (c == 'N');
  
    if (c == 'S')
    {
      /* save data to the registry */
      err = ERROR_SUCCESS;
      if (!regopen)
      {
        err = RegCreateKeyEx(
                HKEY_LOCAL_MACHINE, "SOFTWARE\\LWIP", 0, "",
                REG_OPTION_NON_VOLATILE,
                KEY_CREATE_SUB_KEY | KEY_READ | KEY_WRITE,
                NULL, &regh, NULL);
        if (err == ERROR_SUCCESS)
          regopen = 1;
      }

      if (err == ERROR_SUCCESS)
        err = RegSetValueEx(regh, "adapter", 0, REG_DWORD,
                            (const unsigned char*) &adapter, 4);

      if (err == ERROR_SUCCESS)
        err = RegSetValueEx(regh, "ipaddr", 0, REG_DWORD,
                            (const unsigned char*) ipaddr, 4);

      if (err == ERROR_SUCCESS)
        err = RegSetValueEx(regh, "netmask", 0, REG_DWORD,
                            (const unsigned char*) netmask, 4);

      if (err == ERROR_SUCCESS)
        err = RegSetValueEx(regh, "gateway", 0, REG_DWORD,
                            (const unsigned char*)  gatew, 4);

      if (err != ERROR_SUCCESS)
      {
        printf("Failed to create or write registry key!\n"
               "You need to run this program with admin rights!\n");
        if (regopen)
          RegCloseKey(regh);
        Sleep(10000);
        return -1;
      }
    }
  }

  if (regopen)
    RegCloseKey(regh);
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -