通过Uip WebClient 实现中应用DNS解析
生活随笔
收集整理的這篇文章主要介紹了
通过Uip WebClient 实现中应用DNS解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Uip WebClient 實現
前一篇:Uip + Stm32移植問題總結
后一篇:Uip WebServer 實現
Uip WebClient 實現的功能是接入互聯網,通過http協議訪問某個網站。HTTP是一種應用層協議。基于TCP/IP。??TCP/IP作為傳輸層協議解決數據如何在網絡中傳輸,HTTP作為應用層協議,解決如何包裝數據。默認的HTTP訪問端口為80端口。 Uip + stm32 的移植參見?Uip + Stm32移植問題總結? 相關文件: Apps/resolv.c 文件實現的是DNS,動態域名解析等。 Apps/webclient.c主要實現HTTP的協議的解析。 首先需要修改User/uip-con.h配置文件: #define UIP_CONF_LOGGING 0 //logging off//typedef int uip_tcp_appstate_t; //出錯可注釋 typedef int uip_udp_appstate_t; //出錯可注釋/*#include "smtp.h"*/ /*#include "hello-world.h"*/ /*#include "telnetd.h"*/ /*#include "webserver.h"*/ /*#include "dhcpc.h"*/ /*#include "resolv.h"*/ #include "webclient.h" //包含WebClient 文件#include "app_call.h" //加入一個Uip的數據接口文件 修改User/mainc ?調用相關WebClient函數 配置DNS以及設定頁面地址#include "stm32f10x.h" #include "stdio.h" #include "string.h"#include "uip.h" #include "uip_arp.h" #include "tapdev.h" #include "timer.h" #include "ENC28J60.h" #include "SPI.h"#define PRINTF_ON 1#define BUF ((struct uip_eth_hdr *)&uip_buf[0])#ifndef NULL #define NULL (void *)0 #endif /* NULL */static unsigned char mymac[6] = {0x04,0x02,0x35,0x00,0x00,0x01};void RCC_Configuration(void); void GPIO_Configuration(void); void USART_Configuration(void);int main(void) {int i;uip_ipaddr_t ipaddr;struct timer periodic_timer, arp_timer;RCC_Configuration();GPIO_Configuration();USART_Configuration();SPInet_Init();timer_set(&periodic_timer, CLOCK_SECOND / 2);timer_set(&arp_timer, CLOCK_SECOND * 10);SysTick_Config(72000); //以太網控制器驅動初始化tapdev_init(mymac);//Uip 協議棧初始化uip_init(); uip_ipaddr(ipaddr, 192, 168, 1, 15); //配置Ipuip_sethostaddr(ipaddr);uip_ipaddr(ipaddr, 192, 168, 1, 1); //配置網關uip_setdraddr(ipaddr);uip_ipaddr(ipaddr, 255, 255, 255, 0); //配置子網掩碼uip_setnetmask(ipaddr);webclient_init();resolv_init();uip_ipaddr(ipaddr, 8,8,8,8); //DNS server ,Google DNS Serverresolv_conf(ipaddr);resolv_query("www.ichanging.org");while(1){uip_len = tapdev_read(); //從網卡讀取數據if(uip_len > 0) { //如果數據存在則按協議處理if(BUF->type == htons(UIP_ETHTYPE_IP)) { //如果收到的是IP數據,調用uip_input()處理uip_arp_ipin(); uip_input();/* If the above function invocation resulted in data thatshould be sent out on the network, the global variable uip_len is set to a value > 0. */if(uip_len > 0) {uip_arp_out();tapdev_send();}}else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ //如果收到的是ARP數據,調用uip_arp_arpin處理uip_arp_arpin();/* If the above function invocation resulted in data thatshould be sent out on the network, the global variable uip_len is set to a value > 0. */if(uip_len > 0) {tapdev_send();}}}else if(timer_expired(&periodic_timer)){ //查看0.5s是否到了,調用uip_periodic處理TCP超時程序timer_reset(&periodic_timer);for(i = 0; i < UIP_CONNS; i++) {uip_periodic(i);/* If the above function invocation resulted in data thatshould be sent out on the network, the global variable uip_len is set to a value > 0. */if(uip_len > 0) {uip_arp_out();tapdev_send();}}for(i = 0; i < UIP_UDP_CONNS; i++) {uip_udp_periodic(i); //處理udp超時程序/* If the above function invocation resulted in data thatshould be sent out on the network, the global variable uip_len is set to a value > 0. */if(uip_len > 0) {uip_arp_out();tapdev_send();}}/* Call the ARP timer function every 10 seconds. */ //10s到了就處理ARPif(timer_expired(&arp_timer)) {timer_reset(&arp_timer);uip_arp_timer();}}}}/*******************************WebClient Set***************************************/void resolv_found(char *name, u16_t *ipaddr) //DNS 找到對應服務器IP {//u16_t *ipaddr2;if(ipaddr == NULL) {printf("Host '%s' not found.\n", name);} else {printf("Found name '%s' = %d.%d.%d.%d\n", name,htons(ipaddr[0]) >> 8,htons(ipaddr[0]) & 0xff,htons(ipaddr[1]) >> 8,htons(ipaddr[1]) & 0xff);if(webclient_get("www.ichanging.org", 80, "/index.php")) {printf("the connection was initiated");}else{printf("the host name could not be found in the cache or TCP connection could not be created.");}} }void webclient_closed(void) {//printf("Webclient: connection closed\n"); } void webclient_aborted(void) {//printf("Webclient: connection aborted\n"); } void webclient_timedout(void) {//printf("Webclient: connection timed out\n"); } void webclient_connected(void) {//printf("Webclient: connected, waiting for data...\n"); } void webclient_datahandler(char *data, u16_t len) {//printf("Webclient: got %d bytes of data.\n", len); }/*******************************Stm32 Set***************************************/void GPIO_Configuration(void) {GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA , &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA , &GPIO_InitStructure); }void RCC_Configuration(void) {/* 定義枚舉類型變量 HSEStartUpStatus */ErrorStatus HSEStartUpStatus;/* 復位系統時鐘設置*/RCC_DeInit();/* 開啟HSE*/RCC_HSEConfig(RCC_HSE_ON);/* 等待HSE起振并穩定*/HSEStartUpStatus = RCC_WaitForHSEStartUp();/* 判斷HSE起是否振成功,是則進入if()內部 */if(HSEStartUpStatus == SUCCESS){/* 選擇HCLK(AHB)時鐘源為SYSCLK 1分頻 */RCC_HCLKConfig(RCC_SYSCLK_Div1); /* 選擇PCLK2時鐘源為 HCLK(AHB) 1分頻 */RCC_PCLK2Config(RCC_HCLK_Div1); /* 選擇PCLK1時鐘源為 HCLK(AHB) 2分頻 */RCC_PCLK1Config(RCC_HCLK_Div2);/* 設置FLASH延時周期數為2 */FLASH_SetLatency(FLASH_Latency_2);/* 使能FLASH預取緩存 */FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/* 選擇鎖相環(PLL)時鐘源為HSE 1分頻,倍頻數為9,則PLL輸出頻率為 8MHz * 9 = 72MHz */RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);/* 使能PLL */ RCC_PLLCmd(ENABLE);/* 等待PLL輸出穩定 */while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);/* 選擇SYSCLK時鐘源為PLL */RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/* 等待PLL成為SYSCLK時鐘源 */while(RCC_GetSYSCLKSource() != 0x08);} /* 打開APB2總線上的GPIOA時鐘*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);}void USART_Configuration(void) {USART_InitTypeDef USART_InitStructure;USART_ClockInitTypeDef USART_ClockInitStructure;USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;USART_ClockInit(USART1 , &USART_ClockInitStructure);USART_InitStructure.USART_BaudRate = 9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;USART_Init(USART1,&USART_InitStructure);USART_Cmd(USART1,ENABLE); }#if PRINTF_ONint fputc(int ch,FILE *f) {USART_SendData(USART1,(u8) ch);while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);return ch; }#endif
總結
以上是生活随笔為你收集整理的通过Uip WebClient 实现中应用DNS解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python语句判断能否构成三角形的完整
- 下一篇: 如何在HTML标题中添加或改变图片