Ejemplo n.º 1
0
static VOID __rtEntryPrint (PLW_RT_ENTRY prte, PCHAR  pcBuffer, size_t  stSize, size_t *pstOffset)
{
    CHAR    cIpDest[INET_ADDRSTRLEN];
    CHAR    cGateway[INET_ADDRSTRLEN] = "*";
    CHAR    cMask[INET_ADDRSTRLEN]    = "*";
    CHAR    cFlag[6] = "\0";
    
    ipaddr_ntoa_r(&prte->RTE_ipaddrDest, cIpDest, INET_ADDRSTRLEN);
    if (prte->RTE_pnetif) {
        ipaddr_ntoa_r(&prte->RTE_pnetif->gw, cGateway, INET_ADDRSTRLEN);
        ipaddr_ntoa_r(&prte->RTE_pnetif->netmask, cMask, INET_ADDRSTRLEN);
    }
    
    if (prte->RTE_uiFlag & LW_RT_FLAG_U) {
        lib_strcat(cFlag, "U");
    }
    if (prte->RTE_uiFlag & LW_RT_FLAG_G) {
        lib_strcat(cFlag, "G");
    } else {
        lib_strcpy(cGateway, "*");                                      /*  直接路由 (不需要打印网关)   */
    }
    if (prte->RTE_uiFlag & LW_RT_FLAG_H) {
        lib_strcat(cFlag, "H");
    }
    if (prte->RTE_uiFlag & LW_RT_FLAG_D) {
        lib_strcat(cFlag, "D");
    }
    if (prte->RTE_uiFlag & LW_RT_FLAG_M) {
        lib_strcat(cFlag, "M");
    }
    
    *pstOffset = bnprintf(pcBuffer, stSize, *pstOffset,
                          "%-18s %-18s %-18s %-8s %-3s\n",
                          cIpDest, cGateway, cMask, cFlag, prte->RTE_cNetifName);
}
Ejemplo n.º 2
0
static void ip_addr_changed(const struct netif *nwif)
{
	char tmp_buff[16];

	if (!nwif->ip_addr.addr) return;

	DEBUGOUT("IP_ADDR    : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &nwif->ip_addr, tmp_buff, 16));
	DEBUGOUT("NET_MASK   : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &nwif->netmask, tmp_buff, 16));
	DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &nwif->gw, tmp_buff, 16));
}
Ejemplo n.º 3
0
 IPAddress::IPAddressStr MTD_FLASHMEM IPAddress::get_str() const
 {
     IPAddressStr str;
     ip_addr_t a = get_ip_addr_t();
     ipaddr_ntoa_r(&a, (char*)str, 16);
     return str;
 }
Ejemplo n.º 4
0
/*********************************************************************************************************
** 函数名称: __aodvEntryPrint
** 功能描述: 打印 aodv 路由信息
** 输 入  : rt        aodv 路由条目
**           pcBuffer  缓冲区
**           stSize    缓冲区大小
**           pstOffset 偏移量
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
static VOID __aodvEntryPrint (struct aodv_rtnode *rt, PCHAR  pcBuffer, size_t  stSize, size_t *pstOffset)
{
    CHAR    cIpDest[INET_ADDRSTRLEN];
    CHAR    cNextHop[INET_ADDRSTRLEN];
    CHAR    cMask[INET_ADDRSTRLEN];
    CHAR    cFlag[16] = "\0";
    CHAR    cIfName[IF_NAMESIZE] = "\0";
    
    inet_ntoa_r(rt->dest_addr, cIpDest, INET_ADDRSTRLEN);
    inet_ntoa_r(rt->next_hop, cNextHop, INET_ADDRSTRLEN);
    
    if (rt->state & AODV_VALID) {
        lib_strcat(cFlag, "U");
    }
    if (rt->hcnt > 0) {
        lib_strcat(cFlag, "G");
    }
    if ((rt->flags & AODV_RT_GATEWAY) == 0) {
        lib_strcat(cFlag, "H");
    }
    if ((rt->flags & AODV_RT_UNIDIR) == 0) {                            /*  单向连接                    */
        lib_strcat(cFlag, "-ud");
    }
    
    /*
     *  aodv 路由节点网络接口一定有效
     */
    ipaddr_ntoa_r(&rt->netif->netmask, cMask, INET_ADDRSTRLEN);
    if_indextoname(rt->netif->num, cIfName);
    
    *pstOffset = bnprintf(pcBuffer, stSize, *pstOffset,
                          "%-18s %-18s %-18s %-8s %8d %-3s\n",
                          cIpDest, cNextHop, cMask, cFlag, rt->hcnt, cIfName);
}
Ejemplo n.º 5
0
void list_netifs(void)
{
    struct netif *n; /* used for iteration. */
    for (n = netif_list; n != NULL; n = n->next) {
        /* Converts the IP adress to a human readable format. */
        char buf[16+1];
        ipaddr_ntoa_r(&n->ip_addr, buf, 17);
        printf("%s: %s\n", n->name, buf);
    }
}
Ejemplo n.º 6
0
static int convergence_layer_udp_dgram_send_discovery(const uint8_t* const payload, const size_t length)
{
#if (UDP_DGRAM_DISCOVERY_ANNOUNCEMENT == 1)
	char addr_str[IP_ADDR_STRING_LENGTH];
	ipaddr_ntoa_r(&udp_mcast_addr, addr_str, sizeof(addr_str));
	LOG(LOGD_DTN, LOG_CL_UDP, LOGL_DBG, "Sending discovery to %s", addr_str);

	return convergence_layer_udp_dgram_send(&udp_mcast_addr, HEADER_BROADCAST, 0, SEGMENT_MIDDLE, payload, length, NULL);
#else
	return 0;
#endif
}
Ejemplo n.º 7
0
/**
 *  \brief Status callback used to print address given by DHCP.
 *
 * \param netif Instance to network interface.
 */
void ethernet_status_callback(struct netif *netif)
{
	char c_mess[20];		// 15 for IP address, 1 for \n, 1 for null, so 3 spare
	if (netif_is_up(netif))
	{
		RepRapNetworkMessage("Network up, IP=");
		ipaddr_ntoa_r(&(netif->ip_addr), c_mess, sizeof(c_mess));
		strncat(c_mess, sizeof(c_mess) - 1, "\n");
		RepRapNetworkMessage(c_mess);
		netif->flags |= NETIF_FLAG_LINK_UP;
	}
	else
	{
		RepRapNetworkMessage("Network down\n");
	}
}
Ejemplo n.º 8
0
/*********************************************************************************************************
** 函数名称: __buildinRtPrint
** 功能描述: 打印 lwip 内建路由信息
** 输 入  : rt            aodv 路由条目
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
static VOID __buildinRtPrint (PCHAR  pcBuffer, size_t  stSize, size_t *pstOffset)
{
    struct netif *netif;
    ip_addr_t     ipaddr;
    
    CHAR    cIpDest[INET_ADDRSTRLEN];
    CHAR    cGateway[INET_ADDRSTRLEN] = "*";
    CHAR    cMask[INET_ADDRSTRLEN];
    CHAR    cFlag[6];
    CHAR    cIfName[IF_NAMESIZE] = "\0";
    
    for (netif = netif_list; netif != NULL; netif = netif->next) {
        if (netif->flags & NETIF_FLAG_POINTTOPOINT) {                   /*  PPP / SLIP 连接             */
            ipaddr.addr = netif->gw.addr;
            ipaddr_ntoa_r(&ipaddr, cIpDest, INET_ADDRSTRLEN);
            ipaddr_ntoa_r(&netif->netmask, cMask, INET_ADDRSTRLEN);
            if_indextoname(netif->num, cIfName);
            lib_strcpy(cFlag, "UH");
        
        } else {                                                        /*  普通链接                    */
            ipaddr.addr = netif->ip_addr.addr & netif->netmask.addr;
            ipaddr_ntoa_r(&ipaddr, cIpDest, INET_ADDRSTRLEN);
            ipaddr_ntoa_r(&netif->netmask, cMask, INET_ADDRSTRLEN);
            if_indextoname(netif->num, cIfName);
            lib_strcpy(cFlag, "U");
        }
        
        *pstOffset = bnprintf(pcBuffer, stSize, *pstOffset,
                              "%-18s %-18s %-18s %-8s %-3s\n",
                              cIpDest, cGateway, cMask, cFlag, cIfName);

        ipaddr_ntoa_r(&netif->ip_addr, cIpDest, INET_ADDRSTRLEN);
        lib_strcpy(cFlag, "UH");
        
        *pstOffset = bnprintf(pcBuffer, stSize, *pstOffset,
                              "%-18s %-18s %-18s %-8s %-3s\n",
                              cIpDest, cGateway, cMask, cFlag, cIfName);
    }
    
    if (netif_default) {
        ipaddr_ntoa_r(&netif_default->gw, cGateway, INET_ADDRSTRLEN);
        ipaddr_ntoa_r(&netif_default->netmask, cMask, INET_ADDRSTRLEN);
        if_indextoname(netif_default->num, cIfName);
        lib_strcpy(cFlag, "UG");

        *pstOffset = bnprintf(pcBuffer, stSize, *pstOffset,
                              "%-18s %-18s %-18s %-8s %-3s\n",
                              "default", cGateway, cMask, cFlag, cIfName);
    }
}
Ejemplo n.º 9
0
/**
 *  \brief Status callback used to print address given by DHCP.
 *
 * \param netif Instance to network interface.
 */
void ethernet_status_callback(struct netif *netif)
{
	char c_mess[20];		// 15 for IP address, 1 for \n, 1 for null, so 3 spare
	if (netif_is_up(netif))
	{
		RepRapNetworkMessage("Network up, IP=");
		ipaddr_ntoa_r(&(netif->ip_addr), c_mess, sizeof(c_mess));
		/* Nasty bug: concatenates whatever is at the address 19
		 * to the end of c_mess[] AND declares the length of
		 * c_mess[] to be whatever the address of the temp stack
		 * string "\n" is.
		 *
		 *  strncat(c_mess, sizeof(c_mess) - 1, "\n");
		 */
		strncat(c_mess, "\n", sizeof(c_mess) - 1);
		RepRapNetworkMessage(c_mess);
		netif->flags |= NETIF_FLAG_LINK_UP;
	}
	else
	{
		RepRapNetworkMessage("Network down\n");
	}
}
Ejemplo n.º 10
0
/* Ping using the raw ip */
static u8_t ICACHE_FLASH_ATTR
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr)
{
  struct icmp_echo_hdr *iecho = NULL;
  static u16_t seqno = 0;
//  struct ping_msg *pingmsg = (struct ping_msg*)arg;

  LWIP_UNUSED_ARG(arg);
  LWIP_UNUSED_ARG(pcb);
  LWIP_UNUSED_ARG(addr);
  LWIP_ASSERT("p != NULL", p != NULL);

  if (pbuf_header( p, -PBUF_IP_HLEN)==0) {
    iecho = (struct icmp_echo_hdr *)p->payload;

    if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)) && iecho->type == ICMP_ER) {
      LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
      ip_addr_debug_print(PING_DEBUG, addr);
      LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
	  if (iecho->seqno != seqno){
		  /* do some ping result processing */
		  {
			  struct ip_hdr *iphdr = NULL;
			  char ipaddrstr[16];
			  ip_addr_t source_ip;
			  sys_untimeout(ping_timeout, pingmsg);
			  os_bzero(&source_ip, sizeof(ip_addr_t));
			  os_bzero(ipaddrstr, sizeof(ipaddrstr));
			  uint32 delay = system_relative_time(pingmsg->ping_sent);
			  delay /= PING_COARSE;
			  iphdr = (struct ip_hdr*)((u8*)iecho - PBUF_IP_HLEN);
			  source_ip.addr = iphdr->src.addr;
			  ipaddr_ntoa_r(&source_ip,ipaddrstr, sizeof(ipaddrstr));
			  if (pingmsg->ping_opt->recv_function == NULL){
				  os_printf("recv %s: byte = %d, time = %d ms, seq = %d\n",ipaddrstr, PING_DATA_SIZE, delay, ntohs(iecho->seqno));
			  } else {
				  struct ping_resp pingresp;
				  os_bzero(&pingresp, sizeof(struct ping_resp));
				  pingresp.bytes = PING_DATA_SIZE;
				  pingresp.resp_time = delay;
				  pingresp.seqno = ntohs(iecho->seqno);
				  pingresp.ping_err = 0;
				  pingmsg->ping_opt->recv_function(pingmsg->ping_opt,(void*) &pingresp);
			  }
		  }
		  seqno = iecho->seqno;
	  }

      PING_RESULT(1);
      pbuf_free(p);
      return 1; /* eat the packet */
    }
//    } else if(iecho->type == ICMP_ECHO){
//        struct pbuf *q = NULL;
//        os_printf("receive ping request:seq=%d\n", ntohs(iecho->seqno));
//        q = pbuf_alloc(PBUF_IP, (u16_t)p->tot_len, PBUF_RAM);
//        if (q!=NULL) {
//            pbuf_copy(q, p);
//            iecho = (struct icmp_echo_hdr *)q->payload;
//            ping_prepare_er(iecho, q->tot_len);
//            raw_sendto(pcb, q, addr);
//            pbuf_free(q);
//        }
//        pbuf_free(p);
//        return 1;
//    }
  }

  return 0; /* don't eat the packet */
}
Ejemplo n.º 11
0
/**
 * Convert numeric IP address into decimal dotted ASCII representation.
 * returns ptr to static buffer; not reentrant!
 *
 * @param addr ip address in network order to convert
 * @return pointer to a global static (!) buffer that holds the ASCII
 *         represenation of addr
 */
char *
ipaddr_ntoa(const ip_addr_t *addr)
{
  static char str[16];
  return ipaddr_ntoa_r(addr, str, 16);
}
Ejemplo n.º 12
0
/* LWIP kickoff and PHY link monitor thread */
static void vSetupIFTask(void *pvParameters) {
	ip_addr_t ipaddr, netmask, gw;
	volatile s32_t tcpipdone = 0;
	uint32_t physts;
	static int prt_ip = 0;
	
	DEBUGSTR("LWIP HTTP Web Server FreeRTOS Demo...\r\n");
	
	/* Wait until the TCP/IP thread is finished before
	   continuing or wierd things may happen */
	DEBUGSTR("Waiting for TCPIP thread to initialize...\r\n");
	tcpip_init(tcpip_init_done_signal, (void *) &tcpipdone);
	while (!tcpipdone) {
		msDelay(1);
	}

	DEBUGSTR("Starting LWIP HTTP server...\r\n");

	/* Static IP assignment */
#if LWIP_DHCP
	IP4_ADDR(&gw, 0, 0, 0, 0);
	IP4_ADDR(&ipaddr, 0, 0, 0, 0);
	IP4_ADDR(&netmask, 0, 0, 0, 0);
#else
	IP4_ADDR(&gw, 10, 1, 10, 1);
	IP4_ADDR(&ipaddr, 10, 1, 10, 234);
	IP4_ADDR(&netmask, 255, 255, 255, 0);
#endif

	/* Add netif interface for lpc17xx_8x */
	memset(&lpc_netif, 0, sizeof(lpc_netif));
	if (!netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,
				   tcpip_input)) {
		DEBUGSTR("Net interface failed to initialize\r\n");
		while(1);			   
	}
	netif_set_default(&lpc_netif);
	netif_set_up(&lpc_netif);

	/* Enable MAC interrupts only after LWIP is ready */
	NVIC_SetPriority(ETHERNET_IRQn, config_ETHERNET_INTERRUPT_PRIORITY);
	NVIC_EnableIRQ(ETHERNET_IRQn);

#if LWIP_DHCP
	dhcp_start(&lpc_netif);
#endif
	
	/* Initialize and start application */
	http_server_netconn_init();
	
	/* This loop monitors the PHY link and will handle cable events
	   via the PHY driver. */
	while (1) {
		/* Call the PHY status update state machine once in a while
		   to keep the link status up-to-date */
		physts = lpcPHYStsPoll();

		/* Only check for connection state when the PHY status has changed */
		if (physts & PHY_LINK_CHANGED) {
			if (physts & PHY_LINK_CONNECTED) {
				Board_LED_Set(0, true);
				prt_ip = 0;

				/* Set interface speed and duplex */
				if (physts & PHY_LINK_SPEED100) {
					Chip_ENET_Set100Mbps(LPC_ETHERNET);
					NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
				}
				else {
					Chip_ENET_Set10Mbps(LPC_ETHERNET);
					NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
				}
				if (physts & PHY_LINK_FULLDUPLX) {
					Chip_ENET_SetFullDuplex(LPC_ETHERNET);
				}
				else {
					Chip_ENET_SetHalfDuplex(LPC_ETHERNET);
				}

				tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
										  (void *) &lpc_netif, 1);
			}
			else {
				Board_LED_Set(0, false);
				tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
										  (void *) &lpc_netif, 1);
			}

			/* Delay for link detection (250mS) */
			vTaskDelay(configTICK_RATE_HZ / 4);
		}

		/* Print IP address info */
		if (!prt_ip) {
			if (lpc_netif.ip_addr.addr) {
				static char tmp_buff[16];
				DEBUGOUT("IP_ADDR    : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
				DEBUGOUT("NET_MASK   : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
				DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
				prt_ip = 1;
			}
		}
	}
}
Ejemplo n.º 13
0
/**
 * @brief	main routine for example_lwip_tcpecho_sa_18xx43xx
 * @return	Function should not exit.
 */
int main(void)
{
	uint32_t physts, rp = 0;
	ip_addr_t ipaddr, netmask, gw;

	prvSetupHardware();

	/* Initialize LWIP */
	lwip_init();

	LWIP_DEBUGF(LWIP_DBG_ON, ("Starting LWIP TCP echo server...\n"));

	/* Static IP assignment */
#if LWIP_DHCP
	IP4_ADDR(&gw, 0, 0, 0, 0);
	IP4_ADDR(&ipaddr, 0, 0, 0, 0);
	IP4_ADDR(&netmask, 0, 0, 0, 0);
#else
	IP4_ADDR(&gw, 10, 1, 10, 1);
	IP4_ADDR(&ipaddr, 10, 1, 10, 234);
	IP4_ADDR(&netmask, 255, 255, 255, 0);
	APP_PRINT_IP(&ipaddr);
#endif

	/* Add netif interface for lpc17xx_8x */
	netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,
			  ethernet_input);
	netif_set_default(&lpc_netif);
	netif_set_up(&lpc_netif);

#if LWIP_DHCP
	dhcp_start(&lpc_netif);
#endif

	/* Initialize and start application */
	echo_init();

	/* This could be done in the sysTick ISR, but may stay in IRQ context
	   too long, so do this stuff with a background loop. */
	while (1) {
		/* Handle packets as part of this loop, not in the IRQ handler */
		lpc_enetif_input(&lpc_netif);

		/* lpc_rx_queue will re-qeueu receive buffers. This normally occurs
		   automatically, but in systems were memory is constrained, pbufs
		   may not always be able to get allocated, so this function can be
		   optionally enabled to re-queue receive buffers. */
#if 0
		while (lpc_rx_queue(&lpc_netif)) {}
#endif

		/* Free TX buffers that are done sending */
		lpc_tx_reclaim(&lpc_netif);

		/* LWIP timers - ARP, DHCP, TCP, etc. */
		sys_check_timeouts();

		/* Call the PHY status update state machine once in a while
		   to keep the link status up-to-date */
		physts = lpcPHYStsPoll();

		/* Only check for connection state when the PHY status has changed */
		if (physts & PHY_LINK_CHANGED) {
			if (physts & PHY_LINK_CONNECTED) {
				Board_LED_Set(0, true);

				/* Set interface speed and duplex */
				if (physts & PHY_LINK_SPEED100) {
					Chip_ENET_SetSpeed(LPC_ETHERNET, 1);
					NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
				}
				else {
					Chip_ENET_SetSpeed(LPC_ETHERNET, 0);
					NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
				}
				if (physts & PHY_LINK_FULLDUPLX) {
					Chip_ENET_SetDuplex(LPC_ETHERNET, true);
				}
				else {
					Chip_ENET_SetDuplex(LPC_ETHERNET, false);
				}

				netif_set_link_up(&lpc_netif);
				rp = 0;
			}
			else {
				Board_LED_Set(0, false);
				netif_set_link_down(&lpc_netif);
			}

			DEBUGOUT("Link connect status: %d\r\n", ((physts & PHY_LINK_CONNECTED) != 0));
		}
		if (!rp && lpc_netif.ip_addr.addr) {
			static char tmp_buff[16];
			DEBUGOUT("IP_ADDR    : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
			DEBUGOUT("NET_MASK   : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
			DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
			Board_LED_Set(1, true);
			rp = 1;
		}
	}

	/* Never returns, for warning only */
	return 0;
}
Ejemplo n.º 14
0
/**
 * @brief	main routine for example_lwip_tcpecho_sa_17xx40xx
 * @return	Function should not exit.
 */
int main(void)
{
	uint32_t physts;
	ip_addr_t ipaddr, netmask, gw;
	static int prt_ip = 0;

	prvSetupHardware();






	/* Initialize LWIP */
	lwip_init();



	///LWIP_DEBUGF(LWIP_DBG_ON, ("Starting LWIP TCP echo server...\n"));

	/* Static IP assignment */

	IP4_ADDR(&gw, 0, 0, 0, 0);
	IP4_ADDR(&ipaddr, 0, 0, 0, 0);
	IP4_ADDR(&netmask, 0, 0, 0, 0);

	/* Add netif interface for lpc17xx_8x */


	netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,  ethernet_input);
	netif_set_default(&lpc_netif);
	netif_set_up(&lpc_netif);


	dhcp_start(&lpc_netif);



	uint8_t fl = 0 ;



	while (1)

	{



		/* Handle packets as part of this loop, not in the IRQ handler */



				lpc_enetif_input(&lpc_netif);



				/* lpc_rx_queue will re-qeueu receive buffers. This normally occurs
				   automatically, but in systems were memory is constrained, pbufs
				   may not always be able to get allocated, so this function can be
				   optionally enabled to re-queue receive buffers. */

		#if 0
				while (lpc_rx_queue(&lpc_netif)) {}
		#endif

				/* Free TX buffers that are done sending */
				lpc_tx_reclaim(&lpc_netif);

				/* LWIP timers - ARP, DHCP, TCP, etc. */
				sys_check_timeouts();

				/* Call the PHY status update state machine once in a while
				   to keep the link status up-to-date */
				physts = lpcPHYStsPoll();

				/* Only check for connection state when the PHY status has changed */

				if (physts & PHY_LINK_CHANGED)
				{
					if (physts & PHY_LINK_CONNECTED)
					{



						prt_ip = 0;

						/* Set interface speed and duplex */
						if (physts & PHY_LINK_SPEED100)
						{
							Chip_ENET_Set100Mbps(LPC_ETHERNET);
							NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
						}
						else
						{
							Chip_ENET_Set10Mbps(LPC_ETHERNET);
							NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
						}
						if (physts & PHY_LINK_FULLDUPLX) {
							Chip_ENET_SetFullDuplex(LPC_ETHERNET);
						}
						else {
							Chip_ENET_SetHalfDuplex(LPC_ETHERNET);
						}

						netif_set_link_up(&lpc_netif);
					}
					else
					{



						netif_set_link_down(&lpc_netif);
					}

					DEBUGOUT("Link connect status: %d\r\n", ((physts & PHY_LINK_CONNECTED) != 0));
				}

				/* Print IP address info */

				if (!prt_ip)
				{
					if (lpc_netif.ip_addr.addr)
					{
						static char tmp_buff[16];
						DEBUGOUT("IP_ADDR    : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
						DEBUGOUT("NET_MASK   : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
						DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
						prt_ip = 1;




						mqttAppInit();

						mqttAppConnect();



						//////   mqttAppDisconnect();


					}
				}

				if(mqttIsConnected()==1)
				{
					mqttAppHandle();


					if (fl == 0)
					{



						mqttAppSubscribe("lpc/#");
						mqttAppSubscribe("lpc/teste");


						fl = 1;

					}

				}








	}



	return 0;
}
Ejemplo n.º 15
0
/**
 * Convert numeric IP address into decimal dotted ASCII representation.
 * returns ptr to static buffer; not reentrant!
 *
 * @param addr ip address in network order to convert
 * @return pointer to a global static (!) buffer that holds the ASCII
 *         represenation of addr
 */
char * ICACHE_FLASH_ATTR
ipaddr_ntoa(const ip_addr_t *addr)
{
  static char str[16];
  return ipaddr_ntoa_r(addr, str, 16);
}
Ejemplo n.º 16
0
//Cgi that check the request has the correct HOST header
//Using it we can ensure our server has a domain of our choice
int cgi_check_host(http_connection *connData)
{
	http_server_config *config = (http_server_config*)connData->cgi.argument;
	if(config==NULL)
		return HTTPD_CGI_NEXT_RULE;

	if(config->host_domain==NULL)
		return HTTPD_CGI_NEXT_RULE;


	if(connData->state==HTTPD_STATE_ON_URL)
	{
		http_set_save_header(connData,HTTP_HOST);
		return HTTPD_CGI_NEXT_RULE;
	}

	if(connData->state==HTTPD_STATE_HEADERS_END)
	{
		header *hostHeader = http_get_header(connData,HTTP_HOST);
		if(hostHeader==NULL)
		{
         NODE_ERR("Host header not found\n");
			http_response_BAD_REQUEST(connData);
			return HTTPD_CGI_DONE;
		}
		const char * domain = config->host_domain;

      HTTP_CGI_DBG("Host header: %s, domain: %s\n",hostHeader->value,domain);

		if(strncmp(hostHeader->value,domain,strlen(domain))==0) //compare ignoring http:// and last /
		{
         HTTP_CGI_DBG("Domain match\n");
			return HTTPD_CGI_NEXT_RULE;
		}
		else{
			uint8_t op = wifi_get_opmode();
			char ipaddrstr[17];
			os_bzero(ipaddrstr, sizeof(ipaddrstr));
			struct ip_info ipConfig;
			switch (op)
			{
				case STATIONAP_MODE:
				{
					wifi_get_ip_info(SOFTAP_IF,&ipConfig); //0x01
					ipaddr_ntoa_r(&ipConfig.ip,ipaddrstr, sizeof(ipaddrstr));

					if(strncmp(hostHeader->value,ipaddrstr,strlen(ipaddrstr))==0)
						{ HTTP_CGI_DBG("SoftAp ip match"); return HTTPD_CGI_NEXT_RULE; }
				}
				case STATION_MODE:
				{
					os_bzero(ipaddrstr, sizeof(ipaddrstr));
					wifi_get_ip_info(STATION_IF,&ipConfig); //0x00
					ipaddr_ntoa_r(&ipConfig.ip,ipaddrstr, sizeof(ipaddrstr));

					if(strncmp(hostHeader->value,ipaddrstr,strlen(ipaddrstr))==0)
						{ HTTP_CGI_DBG("Station ip match"); return HTTPD_CGI_NEXT_RULE; }
				}
			}
         HTTP_CGI_DBG("Hosts don't match\n");

			if(config->enable_captive)
			{
				//to enable a captive portal we should redirect here
				char * redirectUrl = (char *)os_zalloc(strlen(domain)+9); // domain length + http:// + / + \0
				strcpy(redirectUrl,"http://");
				os_strcat(redirectUrl,domain);
				os_strcat(redirectUrl,"/");
				http_response_REDIRECT(connData, redirectUrl);
				os_free(redirectUrl);
            HTTP_CGI_DBG("Redirect URL = %s\n", redirectUrl);

			} else {
			//bad request else
			http_response_BAD_REQUEST(connData);
			}
		return HTTPD_CGI_DONE;
		}
	}
	return HTTPD_CGI_NEXT_RULE;
}