290 likes | 436 Views
大型应用软件中期报告 ——TCP/IP 协议模拟. 5060369032 夏业添 5060369033 蔡欣磊 5060369034 陈君惠 5060369044 钱晓骏. 当前进度. 详细设计阶段 尚未进入编码阶段. IP 模块设计. ipaddr.h. IPv4 地址结构 struct in_addr{ //Temporary ipv4 address,32 bits unsigned char part[4]; }; 字符串与 ip 地址转换函数
E N D
大型应用软件中期报告——TCP/IP协议模拟 5060369032夏业添 5060369033蔡欣磊 5060369034陈君惠 5060369044钱晓骏
当前进度 • 详细设计阶段 • 尚未进入编码阶段
ipaddr.h • IPv4地址结构 • struct in_addr{ • //Temporary ipv4 address,32 bits • unsigned char part[4]; • }; • 字符串与ip地址转换函数 • int inet_aton(const char *strptr,struct in_addr *addrptr); • char *inet_ntoa(char *strptr,struct in_addr inaddr);
myip.h • IPv4头部 • struct iphdr{ • struct in_addr des; • struct in_addr src; • struct ip_info{ • //should be 4-bit version • int ip_ver:4; • //should be 4-bit length of header • int ip_hl:4; • }; • //should be 8-bit type of serve • unsigned char ip_tos; • //should be 16-bit length of the whole ip packet • unsigned short ip_len; • //should be 16-bit identification • unsigned short ip_id; • //should be 8-bit ttl • unsigned char ip_ttl; • //should be 8-bit protocal • unsigned char ip_pro; • //should be 8-bit ip header checksum • unsigned char ip_sum; • };
myip.h • /* this function send "ip package" to the virtual ip pipe • * Parameter: • * pheader:a pointer to the ip header of the package • * ptcpheader:a pointer to the tcp header which the packge contains • * buf:a buffer which contains data • * buflen:the length of buffer • * Return Values: • * it returns -1 for some error,and 0 is returned if succeed • */ • int ip_output(struct iphdr *pheader,struct tcphdr *ptcpheader,char *buf,int buflen);
myip.h • /* this function handle ip package which is recieved from the • * virtual ip tunnel • * Parameter: • * its parameter should be related to the implement of our • * tcp/ip simulation.So we will discuss it in some time. • * Return values: • * it returns -1 for some error,and 0 is returned if succeed • */ • int ip_input();
myip.h • /* this function calculates the check sum of a ip header • * Parameter: • * pheader:a pointer to the ip header • * Return Values: • * it returns the calculated check_sum of a ip header • */ • unsigned ip_cal_sum(struct iphdr *pheader);
myip.h • /* this function check the check sum of a ip header • * Parameter: • * pheader:a pointer to the ip header • * Return Values: • * it returns -1 if the check sum is wrong,and 0 is returned • * if correct. • */ • int ip_check_sum(struct iphdr *pheader);
mytcpstate.h • enum { • TCP_ESTABLISHED = 1, • TCP_SYN_SENT, • TCP_SYN_RECV, • TCP_FIN_WAIT1, • TCP_FIN_WAIT2, • TCP_TIME_WAIT, • TCP_CLOSE, • TCP_CLOSE_WAIT, • TCP_LAST_ACK, • TCP_LISTEN, • TCP_CLOSING, /* Now a valid state */ • TCP_MAX_STATES /* Leave at the end! */ • };
mytcp.h • struct tcphdr{ • //should be 16-bit source port • unsigned short tcp_sport; • //should be 16-bit destination port • unsigned short tcp_dport; • //should be 32-bit sequence number • unsigned long tcp_seq; • //should be 32-bit acknowledge number • unsigned long tcp_ack; • struct tcp_hl{ • //should be 4-bit length of tcp header • unsigned char hl:4; • unsigned char :4; • }; • struct tcp_flag{ • unsigned char :2; • unsigned char urg:1; • unsigned char ack:1; • unsigned char psh:1; • unsigned char rst:1; • unsigned char syn:1; • unsigned char fin:1; • }; • //should be 16-bit window size • unsigned short tcp_win; • //should be 16-bit checksum • unsigned short tcp_sum; • //should be 16-bit urgent pointer • //but in our design, it should always 0 • unsigned short tcp_up; • }; tcp头文件
mytcp.h • ip伪头部 • struct ip_fake_hdr{ • struct in_addr des; /*destination ip address*/ • struct in_addr src; /*source ip address*/ • unsigned char mbz; /*null*/ • unsigned char pro; /*protocal*/ • unsigned short tcp_l /*length of tcp package*/ • };
mytcp.h • tcp控制块 • struct tcpcb{ • pid_t pid; /*pid of associated process*/ • int sock; /*socket of associated*/ • int tcp_state; /*the state of tcp*/ • struct tcpcb *next; • int timer[4]; • unsigned short port; • }; • /*a header of tcpcb list*/ • struct tcpcb *head;
mytcp.h • /* this function send tcp data to ip layer.it will change • * tcp_state in tcpcb if some condition is satisfied. • * Parameter: • * ptcpheader:a pointer to the tcp header which the packge contains • * buf:a buffer which contains data • * buflen:the length of buffer • * Return Values: • * it returns -1 for some error,and 0 is returned if succeed • */ • int tcp_output(struct tcphdr *ptcpheader,char *buf,len buflen);
mytcp.h • /* this function handles tcp data which is received from ip • * layer, it should check these data, make sure check_sum as • * well as and sequence is right and send data to user level. • * it will change tcp_state in tcpcb if some condition is • * satisfied. • * Parameter: • * its parameter should be related to the implement of our • * tcp/ip simulation.So we will discuss it in some time. • * Return values: • * it returns -1 for some error,and 0 is returned if succeed • */ • int tcp_input();
mytcp.h • /* this function watches all the network timers which is in • * every tcpcb nodes.when a time out is happen, it will handle • * the work to tell user(socket).It wakes up in every servral • * secones,and then reduce tcpcb timers in every tcpcb. • * when a timer reduces to 0, it means some work is time out. • * it will change tcp_state in tcpcb if some condition is • * satisfied. • */ • int tcp_timer();
mytcp.h • /* this function check a tcp check sum whether is correct. • * Parameter: • * its parameter should be related to the implement of our • * tcp/ip simulation.So we will discuss it in some time. • * Return Values: • * it returns -1 if the check sum is wrong, and 0 is returned • * if correct. • */ • int tcp_checksum();
mytcp.h • /* this function create a new tcpcb nodes. • * Parameter: • * some elements in struct tcpcb is not decided, this part should • * be determined when struct tcpcb is certain. • * Return Values: • * it will return a NULL pointer if creation is failed, and a • * pointer to the new tcpcb is returned if succeed. • */ • struct tcpcb* tcp_new_tcpcb();
mysocket.h • struct sockaddr{ • struct in_addr sin_addr; • unsigned short sin_port; • }; • //we should discuss this • struct socket{ • };
mysocket.h • int socket(); • int close(int sockfd); • int read(int sockfd,char *buf,int bufsize); • int write(int sockfd,char *buf,int buflen); • int bind(int sockfd,const struct sockaddr *myaddr,int addrlen); • int connect(int sockfd,const struct sockaddr *servaddr,int addrlen); • int listen(int sockfd,int backlog); • int accept(int sockfd,struct sockaddr *cliaddr,int *addrlen);
tcpipsim.h • /* this function initialize our tcp/ip stack • * at least it should include: • * 1.show its pid • * 2.set another tcp/ip stack ip-pid pair(ip route) • * Return Values: • * it returns -1 for some error,and 0 is returned if succeed • */ • int init();
tcpipsim.h • /* this function handle tcp timer.it calls tcp_timer() to • * achieve its goals • */ • void timer_thread_handler(); • /* this function handle network stuff. • * All network activities, such as sending/recieving package, • * changing tcp state and interactive works with users,should • * be under its control. • */ • void network_thread_handler();
msgtype.h • enum { • MSG_IP_TUNNEL=1, • MSG_USR_READ, • MSG_USR_WRITE, • MSG_USR_CLOSE, • MSG_USR_BIND, • MSG_USR_LISTEN, • MSG_USR_ACCETP, • MSG_USR_CONNECT • };
network_thread_handler.c • 等待消息,做出反应 • switch(msg){ • case MSG_IP_TUNNEL • //ip_input() • break; • case MSG_USR_WRITE • tcp_output(); • break; • ... • case MSG_USR_CONNECT • tcp_output(); • break; • } • 这样的结构
timer_handler.c • while(1){ • //wake up every a seconds • select(NULL,NULL,NULL,&timeout); • 遍历链表,计时器-1 • 若有<0的计时器说明超时,进行处理 • }