实例介绍
【实例截图】
【核心代码】
/* * Copyright (c) 2001, Adam Dunkels. * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Adam Dunkels. * 4. 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. * * This file is part of the uIP TCP/IP stack. * * $Id: main.c,v 1.16 2006/06/11 21:55:03 adam Exp $ * */ #include "stdio.h" #include "uip.h" #include "uip_arp.h" #include "tapdev.h" #include "timer.h" #include "web_server.h" #include "dm9k_uip.h" #include "string.h" #include "led.h" #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) #ifndef NULL #define NULL (void *)0 #endif /* NULL */ #define ETH_HEADER_LEN 14 // values of certain bytes: #define ETHTYPE_ARP_H_V 0x08 #define ETHTYPE_ARP_L_V 0x06 #define ETHTYPE_IP_H_V 0x08 #define ETHTYPE_IP_L_V 0x00 // byte positions in the ethernet frame: // // Ethernet type field (2bytes): #define ETH_TYPE_H_P 12 #define ETH_TYPE_L_P 13 // #define ETH_DST_MAC 0 #define ETH_SRC_MAC 6 // ******* ARP ******* #define ETH_ARP_OPCODE_REPLY_H_V 0x0 #define ETH_ARP_OPCODE_REPLY_L_V 0x02 // #define ETHTYPE_ARP_L_V 0x06 // arp.dst.ip #define ETH_ARP_DST_IP_P 0x26 // arp.opcode #define ETH_ARP_OPCODE_H_P 0x14 #define ETH_ARP_OPCODE_L_P 0x15 // arp.src.mac #define ETH_ARP_SRC_MAC_P 0x16 #define ETH_ARP_SRC_IP_P 0x1c #define ETH_ARP_DST_MAC_P 0x20 #define ETH_ARP_DST_IP_P 0x26 // ******* IP ******* #define IP_HEADER_LEN 0x14 // ip.src #define IP_SRC_P 0x1a #define IP_DST_P 0x1e #define IP_HEADER_LEN_VER_P 0xe #define IP_CHECKSUM_P 0x18 #define IP_TTL_P 0x16 #define IP_FLAGS_P 0x14 #define IP_P 0x0e #define IP_TOTLEN_H_P 0x10 #define IP_TOTLEN_L_P 0x11 #define IP_PROTO_P 0x17 #define IP_PROTO_ICMP_V 1 #define IP_PROTO_TCP_V 6 // 17=0x11 #define IP_PROTO_UDP_V 17 // ******* ICMP ******* #define ICMP_TYPE_ECHOREPLY_V 0 #define ICMP_TYPE_ECHOREQUEST_V 8 // #define ICMP_TYPE_P 0x22 #define ICMP_CHECKSUM_P 0x24 // ******* UDP ******* #define UDP_HEADER_LEN 8 // #define UDP_SRC_PORT_H_P 0x22 #define UDP_SRC_PORT_L_P 0x23 #define UDP_DST_PORT_H_P 0x24 #define UDP_DST_PORT_L_P 0x25 // #define UDP_LEN_H_P 0x26 #define UDP_LEN_L_P 0x27 #define UDP_CHECKSUM_H_P 0x28 #define UDP_CHECKSUM_L_P 0x29 #define UDP_DATA_P 0x2a // ******* TCP ******* #define TCP_SRC_PORT_H_P 0x22 #define TCP_SRC_PORT_L_P 0x23 #define TCP_DST_PORT_H_P 0x24 #define TCP_DST_PORT_L_P 0x25 // the tcp seq number is 4 bytes 0x26-0x29 #define TCP_SEQ_H_P 0x26 #define TCP_SEQACK_H_P 0x2a // flags: SYN=2 #define TCP_FLAGS_P 0x2f #define TCP_FLAGS_SYN_V 2 #define TCP_FLAGS_FIN_V 1 #define TCP_FLAGS_PUSH_V 8 #define TCP_FLAGS_SYNACK_V 0x12 #define TCP_FLAGS_ACK_V 0x10 #define TCP_FLAGS_PSHACK_V 0x18 // plain len without the options: #define TCP_HEADER_LEN_PLAIN 20 #define TCP_HEADER_LEN_P 0x2e #define TCP_CHECKSUM_H_P 0x32 #define TCP_CHECKSUM_L_P 0x33 #define TCP_OPTIONS_P 0x36 static unsigned char ipaddr[4] = {192, 168, 1, 253}; static unsigned char macaddr[6] = {0x00,0x1a,0x2a,0x3a,0x4a,0x52}; //uint8_t RemoteIP[4] = {103,21,136,175}; uint8_t RemoteIP[4] = {192, 168, 1, 12}; uint16_t LocalPort = 8200; //???? uint16_t RemotePort = 8752; //???? void make_eth(unsigned char* buf); u8 tx_buf[25] = {0xA0, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00 , 0x00 , 0x10 , 0x81 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x32}; uint8_t enc_send_size = 10; //需要发送的字节数 uint8_t enc_send_buf[256] = {0}; //???????256 u8 enc_recive_buf[256] = {0}; //????????256 struct uip_udp_conn *my_udp_conn; //udp?? struct uip_conn *my_tcp_conn; //tcp????? u8_t tcp_server_flag = 0;//tcp?????????? int flag_t = 0; //**************??????*********************************************** void tcp_newdata() { char *nptr; enc_send_size = uip_datalen(); //?????? nptr = (char *)uip_appdata; memcpy(enc_send_buf, nptr, enc_send_size); } void tcp_senddata() { uip_send(enc_send_buf, enc_send_size); } void udp_newdata() { char *nptr; enc_send_size = uip_datalen(); //?????? nptr = (char *)uip_appdata; memcpy(enc_send_buf, nptr, enc_send_size); } void udp_senddata() { char *nptr; nptr = (char *)uip_appdata; memcpy(nptr, enc_send_buf, enc_send_size); uip_udp_send(enc_send_size); } //??tcp???????? void tcp_server_appcall(void) { u16_t datlen; //********?????????????********************** /* if (uip_aborted()){tcp_aborted();} if (uip_timedout()){tcp_timedout();} if (uip_closed()){tcp_closed();} if (uip_connected()){tcp_connected();} if (uip_acked()){tcp_acked();} */ /* ???????TCP???,???????? */ if (uip_newdata()) { //tcp_newdata(); if((uip_len > 0) && (uip_len <= 1446)) { if(1) { if(memcmp(uip_appdata,"Hello,",4) == 0) // ??IP { datlen = strlen("Hello! This is TCP Server Test!\n"); memcpy(uip_appdata, "Hello! This is TCP Server Test!\n", datlen); //uip_send(uip_appdata, datlen); tcp_newdata(); } } } } /* ???????????,??uip???? */ if (uip_rexmit() || uip_newdata() ) { tcp_senddata(); } } //??tcp???????? void tcp_client_appcall(void) { uint16_t datlen; u8_t *rx; //********?????????????********************** /* if (uip_aborted()){tcp_aborted();} if (uip_timedout()){tcp_timedout();} if (uip_closed()){tcp_closed();} if (uip_connected()){tcp_connected();} if (uip_acked()){tcp_acked();} */ /* ???????TCP???,???????? */ if(uip_connected()) { tcp_server_flag = 0; } if (uip_newdata()) { //tcp_newdata(); //tcp_newdata(); if((uip_len > 0) && (uip_len <= 1446)) { if(1) { if(memcmp(uip_appdata,"Hello", 5) == 0) // test { datlen = strlen("This is TCP Client Test!\n"); uip_len = datlen; memcpy(uip_appdata, "This is TCP Client Test!\n", datlen); //uip_send(uip_appdata, datlen); tcp_newdata(); } else if(memcmp(uip_appdata,"LED1 On", 7) == 0) // ?? { LED1_ON(); uip_len = strlen("LED is On!\n"); memcpy(uip_appdata, "LED is On!\n", uip_len); //uip_send(uip_appdata, datlen); tcp_newdata(); } else if(memcmp(uip_appdata,"LED1 Off", 8) == 0) // ?? { LED1_OFF(); uip_len = strlen("LED is Off!\n"); memcpy(uip_appdata, "LED is Off!\n", uip_len); //uip_send(uip_appdata, datlen); tcp_newdata(); } else if(memcmp(uip_appdata,"?", 8) == 0) // ?????? { uip_len = strlen("LED is Off!\n"); memcpy(uip_appdata, "LED is Off!\n", uip_len); //uip_send(uip_appdata, datlen); tcp_newdata(); } else { if(uip_len == 19) { rx = (char *)uip_appdata; memset(enc_recive_buf, 0, 256); memcpy(enc_recive_buf, rx, uip_len); if((enc_recive_buf[0] == 0xa0) && (enc_recive_buf[5] == 0x01) && (enc_recive_buf[9] == 0x09)) { LED1_FLASH(); } } } } } } if(flag_t >= 3) { enc_send_size = 25; memcpy(enc_send_buf, tx_buf, 25); tcp_senddata(); flag_t = 0; } /* ???????????,??uip???? */ if (uip_rexmit() || uip_newdata()) { tcp_senddata(); } if(uip_closed()) { tcp_server_flag ; if(tcp_server_flag == 2) { uip_ipaddr_t ipaddr; //??IP???? uip_ipaddr(ipaddr, RemoteIP[0], RemoteIP[1], RemoteIP[2], RemoteIP[3]); //??IP my_tcp_conn = uip_connect(&ipaddr, HTONS(RemotePort)); //??????? tcp_server_flag = 0; } } if( uip_aborted()) { uip_ipaddr_t ipaddr; //??IP???? uip_ipaddr(ipaddr, RemoteIP[0], RemoteIP[1], RemoteIP[2], RemoteIP[3]); //??IP my_tcp_conn = uip_connect(&ipaddr, HTONS(RemotePort)); //??????? } } void make_arp_answer_from_request(unsigned char* buf) { unsigned char i = 0; // make_eth(buf); buf[ETH_ARP_OPCODE_H_P] = ETH_ARP_OPCODE_REPLY_H_V; buf[ETH_ARP_OPCODE_L_P] = ETH_ARP_OPCODE_REPLY_L_V; // fill the mac addresses: while (i < 6) { buf[ETH_ARP_DST_MAC_P i] = buf[ETH_ARP_SRC_MAC_P i]; buf[ETH_ARP_SRC_MAC_P i] = macaddr[i]; i ; } i = 0; while (i < 4) { buf[ETH_ARP_DST_IP_P i] = buf[ETH_ARP_SRC_IP_P i]; buf[ETH_ARP_SRC_IP_P i] = ipaddr[i]; i ; } // eth arp is 42 bytes: //enc28j60PacketSend(42, buf); etherdev_send(buf, 42); } // make a return eth header from a received eth packet void make_eth(unsigned char* buf) { unsigned char i = 0; // //copy the destination mac from the source and fill my mac into src while (i < 6) { buf[ETH_DST_MAC i] = buf[ETH_SRC_MAC i]; buf[ETH_SRC_MAC i] = macaddr[i]; i ; } } unsigned char eth_type_is_arp_and_my_ip(unsigned char* buf, unsigned int len) { unsigned char i = 0; // if (len < 41) { return(0); } if (buf[ETH_TYPE_H_P] != ETHTYPE_ARP_H_V || buf[ETH_TYPE_L_P] != ETHTYPE_ARP_L_V) { return(0); } while (i < 4) { if (buf[ETH_ARP_DST_IP_P i] != ipaddr[i]) { return(0); } i ; } return(1); } //udp????? void udp_appcall(void) { //********?????????????********************** /* if (uip_aborted()){udp_aborted();} if (uip_timedout()){udp_timedout();} if (uip_closed()){udp_closed();} if (uip_connected()){udp_connected();} if (uip_acked()){udp_acked();} */ /* ???????TCP???,???????? */ if (uip_newdata()) { udp_newdata(); } /* ???????????????????????,??uip???? */ if (uip_rexmit() || uip_newdata()) { udp_senddata(); } } //??UDP?????***************************** void Udp_Init(void) { uip_ipaddr_t ipaddr;//??IP???? uip_ipaddr(ipaddr, RemoteIP[0], RemoteIP[1], RemoteIP[2], RemoteIP[3]); //??IP if(my_udp_conn != NULL) { uip_udp_remove(my_udp_conn);//????????,???? } my_udp_conn = uip_udp_new(&ipaddr, HTONS(RemotePort));//??????? if(my_udp_conn != NULL) { uip_udp_bind(my_udp_conn, HTONS(LocalPort));//?????? } } //??tcp?????? void Tcp_Init(void) { uip_ipaddr_t ipaddr; //??IP???? uip_ipaddr(ipaddr, RemoteIP[0], RemoteIP[1], RemoteIP[2], RemoteIP[3]); //??IP my_tcp_conn = uip_connect(&ipaddr, HTONS(RemotePort)); //??????? } int WebServer(void) { int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; /*设置TCP超时处理时间和ARP老化时间*/ timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); printf("Start WebServer...\n\r"); tapdev_init(); printf("uip_init\n\r"); uip_init(); printf("uip ip address : 192,168,1,253\n\r"); uip_ipaddr(ipaddr, 192,168,1,253); uip_sethostaddr(ipaddr); printf("uip route address : 192,168,1,1\n\r"); uip_ipaddr(ipaddr, 192,168,1,1); uip_setdraddr(ipaddr); printf("uip net mask : 255,255,255,0\n\r"); uip_ipaddr(ipaddr, 255,255,255,0); uip_setnetmask(ipaddr); // httpd_init(); uip_listen(HTONS(8756)); // tcp Udp_Init(); // udp Tcp_Init(); // tcp client printf("Enter listen status...\n\r"); // printf("Press Right key to Quit\n\r"); /* telnetd_init();*/ /* hello_world_init();*/ /* { u8_t mac[6] = {1,2,3,4,5,6}; dhcpc_init(&mac, 6); }*/ /*uip_ipaddr(ipaddr, 127,0,0,1); smtp_configure("localhost", ipaddr); SMTP_SEND("adam@sics.se", NULL, "uip-testing@example.com", "Testing SMTP from uIP", "Test message sent by uIP\r\n");*/ /* webclient_init(); resolv_init(); uip_ipaddr(ipaddr, 195,54,122,204); resolv_conf(ipaddr); resolv_query("www.sics.se");*/ while(0) { uip_len = tapdev_read(); if (uip_len == 0) { continue; } if (eth_type_is_arp_and_my_ip(uip_buf, uip_len)) { make_arp_answer_from_request(uip_buf); //USART_DMASendText(USART1,"make_arp_answer_from_request\n"); continue; } } while(1) { uip_len = tapdev_read(); if(uip_len > 0) { /*收到的是IP数据,调用uip_input()处理*/ if(BUF->type == htons(UIP_ETHTYPE_IP)) { uip_arp_ipin(); uip_input(); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ /*处理完成后,如果uip_buf中有数据,则调用etherdev_send 发送出去*/ if (uip_len > 0) { //printf("DM9000AE Detect Ok, vid&pid = %08X\n\r", uip_len); uip_arp_out(); tapdev_send(); } } else if (BUF->type == htons(UIP_ETHTYPE_ARP)) { uip_arp_arpin(); /* If the above function invocation resulted in data that should 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)) { //printf(" 0.5s periodic timer expired\n\r"); timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i ) { uip_periodic(i); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); //发送一个arp请求 tapdev_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i ) { uip_udp_periodic(i); /* If the above function invocation resulted in data that should 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(); } } #endif /* UIP_UDP */ /* Call the ARP timer function every 10 seconds. */ if (timer_expired(&arp_timer)) { //printf(" 10s periodic timer expired\n\r"); timer_reset(&arp_timer); uip_arp_timer(); } } else { /* 添加退出的操作 if (ReadKey() == RIGHT) { return 0; } */ } } return 0; } //uip-tcp?????? void tcp_appcall(void) { if(uip_conn->lport == HTONS(80)) { httpd_appcall(); } if(uip_conn->lport == HTONS(LocalPort)) { tcp_server_appcall(); } if(uip_conn->rport == HTONS(RemotePort)) { tcp_client_appcall(); } } /*---------------------------------------------------------------------------*/ void uip_log(char *m) { printf("uIP log message: %s\n", m); } void resolv_found(char *name, u16_t *ipaddr) { // 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); /* webclient_get("www.sics.se", 80, "/~adam/uip");*/ } } #ifdef __DHCPC_H__ void dhcpc_configured(const struct dhcpc_state *s) { uip_sethostaddr(s->ipaddr); uip_setnetmask(s->netmask); uip_setdraddr(s->default_router); resolv_conf(s->dnsaddr); } #endif /* __DHCPC_H__ */ void smtp_done(unsigned char code) { printf("SMTP done with code %d\n", code); } 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); } /*---------------------------------------------------------------------------*/
标签: 网络
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论