29

Click here to load reader

大型应用软件中期报告 ——TCP/IP 协议模拟

  • Upload
    jalila

  • View
    156

  • Download
    16

Embed Size (px)

DESCRIPTION

大型应用软件中期报告 ——TCP/IP 协议模拟. 5060369032 夏业添 5060369033 蔡欣磊 5060369034 陈君惠 5060369044 钱晓骏. 当前进度. 详细设计阶段 尚未进入编码阶段. IP 模块设计. ipaddr.h. IPv4 地址结构 struct in_addr{ //Temporary ipv4 address,32 bits unsigned char part[4]; }; 字符串与 ip 地址转换函数 - PowerPoint PPT Presentation

Citation preview

Page 1: 大型应用软件中期报告 ——TCP/IP 协议模拟

大型应用软件中期报告 —— TCP/IP 协议模拟

大型应用软件中期报告 —— TCP/IP 协议模拟

5060369032 夏业添5060369033 蔡欣磊5060369034 陈君惠5060369044 钱晓骏

Page 2: 大型应用软件中期报告 ——TCP/IP 协议模拟

当前进度

详细设计阶段尚未进入编码阶段

Page 3: 大型应用软件中期报告 ——TCP/IP 协议模拟

IP 模块设计

Page 4: 大型应用软件中期报告 ——TCP/IP 协议模拟

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);

Page 5: 大型应用软件中期报告 ——TCP/IP 协议模拟

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;

};

Page 6: 大型应用软件中期报告 ——TCP/IP 协议模拟

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);

Page 7: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 8: 大型应用软件中期报告 ——TCP/IP 协议模拟

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);

Page 9: 大型应用软件中期报告 ——TCP/IP 协议模拟

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);

Page 10: 大型应用软件中期报告 ——TCP/IP 协议模拟

tcp 模块

Page 11: 大型应用软件中期报告 ——TCP/IP 协议模拟

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! */

};

Page 12: 大型应用软件中期报告 ——TCP/IP 协议模拟

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头文件

Page 13: 大型应用软件中期报告 ——TCP/IP 协议模拟

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*/

};

Page 14: 大型应用软件中期报告 ——TCP/IP 协议模拟

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;

Page 15: 大型应用软件中期报告 ——TCP/IP 协议模拟

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);

Page 16: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 17: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 18: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 19: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 20: 大型应用软件中期报告 ——TCP/IP 协议模拟

socket 模块

Page 21: 大型应用软件中期报告 ——TCP/IP 协议模拟

mysocket.h

struct sockaddr{

struct in_addr sin_addr;

unsigned short sin_port;

};

//we should discuss this

struct socket{

};

Page 22: 大型应用软件中期报告 ——TCP/IP 协议模拟

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);

Page 23: 大型应用软件中期报告 ——TCP/IP 协议模拟

tcpipsim.h

Page 24: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 25: 大型应用软件中期报告 ——TCP/IP 协议模拟

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();

Page 26: 大型应用软件中期报告 ——TCP/IP 协议模拟

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

};

Page 27: 大型应用软件中期报告 ——TCP/IP 协议模拟

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;

}

这样的结构

Page 28: 大型应用软件中期报告 ——TCP/IP 协议模拟

timer_handler.c

while(1 ) {

//wake up every a seconds

select(NULL,NULL,NULL,&timeout);

遍历链表,计时器 -1

若有 <0 的计时器说明超时,进行处理}

Page 29: 大型应用软件中期报告 ——TCP/IP 协议模拟

THANK YOU!