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

📄 extra.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
字号:
/** Copyright (C) 2001 Koninklijke Philips Electronics N.V.* All Rights Reserved.**############################################################** Module name : extra.c %version: 3 %** Last Update : %date_modified: Mon May  6 16:54:02 2002 %** Description :**############################################################*//*** Revision History** Date Developer Description**/#include <stdio.h>#include <stdlib.h>#include <tcp_ipp.h>#include <tm_common.h>#include <socket_app.h>/******************************************************************************* Defines, constants, structures.******************************************************************************/#define	MAXADDR	    20#define NETWORK_DEVICE "DP83815"#define PRIMARY_DNS_SERVER "161.88.59.6"#define SECONDARY_DNS_SERVER "161.88.59.9"/******************************************************************************* Function prototypes******************************************************************************//******************************************************************************* Globals******************************************************************************/UInt32 assigned_ip_addr;   /* our IP address. */UInt32 assigned_ip_mask;   /* our netmask */UInt32 assigned_ip_gw;     /* our default gateway */static Bool network_device_configured = False;char *primary_network_device = NETWORK_DEVICE;static Bool ip_addr_from_dhcp = False;static char	buffer[64]; /* Scratch buffer. *//*** List of network hosts.*/HostDesc HostsTable[] ={  /* name, ip_addr, hw_alen, hw_addr *///  "hsb360", 0xC0A80113, ETH_ALEN, "\x08\x00\x3E\x28\x79\xED",  0,0,0,0 /* last entry */};ui32 DefGateway = 0;ui32 PriDnsServer = 0;ui32 SecDnsServer = 0;static char *h_addr_p[MAXADDR + 1];static struct hostent   host;static UInt8 host_addr[16];	/* IPv4 or IPv6 *//************************************************************************** gethostbyname - Request IP address resolution from DNS server.** Globals:*   None** Returns:*   A pointer to the structure hostent if successful. NULL otherwise.** Description:*   Return IP address from a NULL-terminated host name. Each TCP/IP stack*   seems to have their own favorite API for this. Unix, Fusion, MS VC++,*   Cygwin use this API. Blunk's getHostByName returns the 32-bit IP*   address, not a pointer to the hostent structure. pNA in pSOS is*   unsigned long gethostbyname (char *name, struct hostent *hp). So we*   go with the majority.** Notes:*   None** History:*   11/25/01    FD     Created.*************************************************************************/struct hostent	*gethostbyname (char *name){    struct hostent *hp = Null;    unsigned long result = 0;    if (result = getHostByName(name))    {        memset((void *)&host,0,sizeof(host));        hp = &host;        host.h_addr_list = h_addr_p;        /* Fill in something reasonable. */                hp->h_name = name;        hp->h_length = 4;        hp->h_addrtype = AF_INET;        /*        printf("getHostByName() returned %s\n", inet_ntoa(*((struct in_addr *)&result)));*/		h_addr_p[0] = (char *)host_addr;		h_addr_p[1] = Null;            host_addr[0] = result & 0xFF;        host_addr[1] = (result >> 8)  & 0xFF;        host_addr[2] = (result >> 16) & 0xFF;        host_addr[3] = (result >> 24) & 0xFF;    }    return hp;}/************************************************************************** inet_ntoa - Convert Internet host address to Internet dot notation.** Parameters:*       [i] in - network-format internet address** Globals:*   None** Returns:*   char * - Pointer to buffer containing Internet dot notation.** Description:*   Convert Internet host address to Internet dot notation.** Notes:*   None** History:*   10/25/01    FD     Created.*************************************************************************/char *inet_ntoa(struct in_addr in){	static char b[20];	char *p;	p = (char *)&in;#define	UC(b)	(((int)b)&0xff)	sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));	return (b);}void *SysPassword(void *username){    return (void *) "FIXTHIS";}int configure_network_interface(Bool dynamic){    int status;        printf("Initializing the DP83815 driver\n");    if (network_device_configured)    {        printf("Network device '%s' already configured.\n", primary_network_device);        return -1;    }    /*-----------------------------------------------------------------*/    /* Initialize the DP83815 driver.                                  */    /*-----------------------------------------------------------------*/        if (dynamic)    {        DefGateway = 0;        dp83815Init(0, 0, NIF_USE_DHCP);        ip_addr_from_dhcp = True;    }    else    {        DefGateway = inet_addr(DEFAULT_GATEWAY_ADDRESS);        dp83815Init(inet_addr(PLATFORM_IP_ADDRESS),                     inet_addr(PLATFORM_NET_MASK),                     0);        tcpAddRoute(inet_addr(DEFAULT_GATEWAY_ADDRESS), INADDR_ANY, 0);        ip_addr_from_dhcp = False;    }    /*-----------------------------------------------------------------*/    /* If network interface is not up yet, wait for it.                */    /*-----------------------------------------------------------------*/    if (tcpStatNi(primary_network_device) == 0)    {      printf("Waiting for %s interface initialization\n", primary_network_device);      do      {        tm_wkafter(1 * KC_TICKS2SEC);        printf(".\n");      } while (tcpStatNi(primary_network_device) == 0);    }        if (1)    {        Ni *ni;        extern struct tcp_globals Net;        for (ni = &Net.Local; ni; ni = ni->next)        {            if (!strcmp(primary_network_device, ni->name))            {                assigned_ip_addr = ni->ip_addr;                assigned_ip_mask = ni->ip_mask;                break;            }        }    }	network_device_configured = True;	printf("Device '%s' is configured.\n", primary_network_device);	printf("Our IP address is %s\n", inet_ntoa(*((struct in_addr *)&assigned_ip_addr)));    printf("Our subnet mask is %s\n", inet_ntoa(*((struct in_addr *)&assigned_ip_mask)));    printf("Default gateway is %s\n", inet_ntoa(*((struct in_addr *)&assigned_ip_gw)));	return 0;}void BringUpNetwork(void){	/* Initialize TCP/IP stack. */    printf("Initializing Blunk Microsystems TargetTCP\n");        PriDnsServer = inet_addr(PRIMARY_DNS_SERVER);    SecDnsServer = inet_addr(SECONDARY_DNS_SERVER);    TargetTcpInit();    /*     * Bring up the network interface     */#if	1    printf("Use static or dynamic (DHCP) IP address for this device? (s/d): ");    fflush(stdout);    gets(buffer);#else    buffer[0] = 'd';#endif        if (buffer[0] == 'd' || buffer[0] == 'D')        configure_network_interface(True);    else        configure_network_interface(False);            printf("Dumping TargetTCP Route Table\n") ;    tcpDiag(TCP_DIAG_ROUTE);    if (DefGateway)    {      printf("Pinging the default gateway...\n");      tcpPing(DefGateway, 32, TRUE);    }}

⌨️ 快捷键说明

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