网络协议栈学习之重要的数据结构

linux-1.2.13linux etinetsock.h 文件中定义了sock和 proto两个结构体:

#define SOCK_ARRAY_SIZE    256        /* Think big (also on some systems a byte is faster */


/*
 * This structure really needs to be cleaned up.
 * Most of it is for TCP, and not used by any of
 * the other protocols.
 */
struct sock {
  struct options        *opt;
  volatile unsigned long    wmem_alloc;
  volatile unsigned long    rmem_alloc;
  unsigned long            write_seq;
  unsigned long            sent_seq;
  unsigned long            acked_seq;
  unsigned long            copied_seq;
  unsigned long            rcv_ack_seq;
  unsigned long            window_seq;
  unsigned long            fin_seq;
  unsigned long            urg_seq;
  unsigned long            urg_data;

  /*
   * Not all are volatile, but some are, so we
   * might as well say they all are.
   */
  volatile char                 inuse,
                dead,
                urginline,
                intr,
                blog,
                done,
                reuse,
                keepopen,
                linger,
                delay_acks,
                destroy,
                ack_timed,
                no_check,
                zapped,    /* In ax25 & ipx means not linked */
                broadcast,
                nonagle;
  unsigned long                lingertime;
  int                proc;
  struct sock            *next;
  struct sock            *prev; /* Doubly linked chain.. */
  struct sock            *pair;
  struct sk_buff        * volatile send_head;
  struct sk_buff        * volatile send_tail;
  struct sk_buff_head        back_log;
  struct sk_buff        *partial;
  struct timer_list        partial_timer;
  long                retransmits;
  struct sk_buff_head        write_queue,
                receive_queue;
  struct proto            *prot;
  struct wait_queue        **sleep;
  unsigned long            daddr;
  unsigned long            saddr;
  unsigned short        max_unacked;
  unsigned short        window;
  unsigned short        bytes_rcv;
/* mss is min(mtu, max_window) */
  unsigned short        mtu;       /* mss negotiated in the syn's */
  volatile unsigned short    mss;       /* current eff. mss - can change */
  volatile unsigned short    user_mss;  /* mss requested by user in ioctl */
  volatile unsigned short    max_window;
  unsigned long         window_clamp;
  unsigned short        num;
  volatile unsigned short    cong_window;
  volatile unsigned short    cong_count;
  volatile unsigned short    ssthresh;
  volatile unsigned short    packets_out;
  volatile unsigned short    shutdown;
  volatile unsigned long    rtt;
  volatile unsigned long    mdev;
  volatile unsigned long    rto;
/* currently backoff isn't used, but I'm maintaining it in case
 * we want to go back to a backoff formula that needs it
 */
  volatile unsigned short    backoff;
  volatile short        err;
  unsigned char            protocol;
  volatile unsigned char    state;
  volatile unsigned char    ack_backlog;
  unsigned char            max_ack_backlog;
  unsigned char            priority;
  unsigned char            debug;
  unsigned short        rcvbuf;
  unsigned short        sndbuf;
  unsigned short        type;
  unsigned char            localroute;    /* Route locally only */
#ifdef CONFIG_IPX
  ipx_address            ipx_dest_addr;
  ipx_interface            *ipx_intrfc;
  unsigned short        ipx_port;
  unsigned short        ipx_type;
#endif
#ifdef CONFIG_AX25
/* Really we want to add a per protocol private area */
  ax25_address            ax25_source_addr,ax25_dest_addr;
  struct sk_buff *volatile    ax25_retxq[8];
  char                ax25_state,ax25_vs,ax25_vr,ax25_lastrxnr,ax25_lasttxnr;
  char                ax25_condition;
  char                ax25_retxcnt;
  char                ax25_xx;
  char                ax25_retxqi;
  char                ax25_rrtimer;
  char                ax25_timer;
  unsigned char            ax25_n2;
  unsigned short        ax25_t1,ax25_t2,ax25_t3;
  ax25_digi            *ax25_digipeat;
#endif  
#ifdef CONFIG_ATALK
  struct atalk_sock        at;
#endif

/* IP 'private area' or will be eventually */
  int                ip_ttl;        /* TTL setting */
  int                ip_tos;        /* TOS */
  struct tcphdr            dummy_th;
  struct timer_list        keepalive_timer;    /* TCP keepalive hack */
  struct timer_list        retransmit_timer;    /* TCP retransmit timer */
  struct timer_list        ack_timer;        /* TCP delayed ack timer */
  int                ip_xmit_timeout;    /* Why the timeout is running */
#ifdef CONFIG_IP_MULTICAST  
  int                ip_mc_ttl;            /* Multicasting TTL */
  int                ip_mc_loop;            /* Loopback (not implemented yet) */
  char                ip_mc_name[MAX_ADDR_LEN];    /* Multicast device name */
  struct ip_mc_socklist        *ip_mc_list;            /* Group array */
#endif  

  /* This part is used for the timeout functions (timer.c). */
  int                timeout;    /* What are we waiting for? */
  struct timer_list        timer;        /* This is the TIME_WAIT/receive timer when we are doing IP */
  struct timeval        stamp;

  /* identd */
  struct socket            *socket;
  
  /* Callbacks */
  void                (*state_change)(struct sock *sk);
  void                (*data_ready)(struct sock *sk,int bytes);
  void                (*write_space)(struct sock *sk);
  void                (*error_report)(struct sock *sk);
  
};

struct proto {
  struct sk_buff *    (*wmalloc)(struct sock *sk,
                    unsigned long size, int force,
                    int priority);
  struct sk_buff *    (*rmalloc)(struct sock *sk,
                    unsigned long size, int force,
                    int priority);
  void            (*wfree)(struct sock *sk, struct sk_buff *skb,
                 unsigned long size);
  void            (*rfree)(struct sock *sk, struct sk_buff *skb,
                 unsigned long size);
  unsigned long        (*rspace)(struct sock *sk);
  unsigned long        (*wspace)(struct sock *sk);
  void            (*close)(struct sock *sk, int timeout);
  int            (*read)(struct sock *sk, unsigned char *to,
                int len, int nonblock, unsigned flags);
  int            (*write)(struct sock *sk, unsigned char *to,
                 int len, int nonblock, unsigned flags);
  int            (*sendto)(struct sock *sk,
                  unsigned char *from, int len, int noblock,
                  unsigned flags, struct sockaddr_in *usin,
                  int addr_len);
  int            (*recvfrom)(struct sock *sk,
                    unsigned char *from, int len, int noblock,
                    unsigned flags, struct sockaddr_in *usin,
                    int *addr_len);
  int            (*build_header)(struct sk_buff *skb,
                    unsigned long saddr,
                    unsigned long daddr,
                    struct device **dev, int type,
                    struct options *opt, int len, int tos, int ttl);
  int            (*connect)(struct sock *sk,
                  struct sockaddr_in *usin, int addr_len);
  struct sock *        (*accept) (struct sock *sk, int flags);
  void            (*queue_xmit)(struct sock *sk,
                      struct device *dev, struct sk_buff *skb,
                      int free);
  void            (*retransmit)(struct sock *sk, int all);
  void            (*write_wakeup)(struct sock *sk);
  void            (*read_wakeup)(struct sock *sk);
  int            (*rcv)(struct sk_buff *buff, struct device *dev,
                   struct options *opt, unsigned long daddr,
                   unsigned short len, unsigned long saddr,
                   int redo, struct inet_protocol *protocol);
  int            (*select)(struct sock *sk, int which,
                  select_table *wait);
  int            (*ioctl)(struct sock *sk, int cmd,
                 unsigned long arg);
  int            (*init)(struct sock *sk);
  void            (*shutdown)(struct sock *sk, int how);
  int            (*setsockopt)(struct sock *sk, int level, int optname,
                   char *optval, int optlen);
  int            (*getsockopt)(struct sock *sk, int level, int optname,
                  char *optval, int *option);       
  unsigned short    max_header;
  unsigned long        retransmits;
  struct sock *        sock_array[SOCK_ARRAY_SIZE];
  char            name[80];
  int            inuse, highestinuse;
};

linux-1.2.13linuxincludelinux et.h 定义了socket和proto_ops两个结构体

struct socket {
  short            type;        /* SOCK_STREAM, ...        */
  socket_state        state;
  long            flags;
  struct proto_ops    *ops;        /* protocols do most everything    */
  void            *data;        /* protocol data        */
  struct socket        *conn;        /* server socket connected to    */
  struct socket        *iconn;        /* incomplete client conn.s    */
  struct socket        *next;
  struct wait_queue    **wait;        /* ptr to place to wait on    */
  struct inode        *inode;
  struct fasync_struct  *fasync_list;    /* Asynchronous wake up list    */
};

#define SOCK_INODE(S)    ((S)->inode)

struct proto_ops {
  int    family;

  int    (*create)    (struct socket *sock, int protocol);
  int    (*dup)        (struct socket *newsock, struct socket *oldsock);
  int    (*release)    (struct socket *sock, struct socket *peer);
  int    (*bind)        (struct socket *sock, struct sockaddr *umyaddr,
             int sockaddr_len);
  int    (*connect)    (struct socket *sock, struct sockaddr *uservaddr,
             int sockaddr_len, int flags);
  int    (*socketpair)    (struct socket *sock1, struct socket *sock2);
  int    (*accept)    (struct socket *sock, struct socket *newsock,
             int flags);
  int    (*getname)    (struct socket *sock, struct sockaddr *uaddr,
             int *usockaddr_len, int peer);
  int    (*read)        (struct socket *sock, char *ubuf, int size,
             int nonblock);
  int    (*write)    (struct socket *sock, char *ubuf, int size,
             int nonblock);
  int    (*select)    (struct socket *sock, int sel_type,
             select_table *wait);
  int    (*ioctl)    (struct socket *sock, unsigned int cmd,
             unsigned long arg);
  int    (*listen)    (struct socket *sock, int len);
  int    (*send)        (struct socket *sock, void *buff, int len, int nonblock,
             unsigned flags);
  int    (*recv)        (struct socket *sock, void *buff, int len, int nonblock,
             unsigned flags);
  int    (*sendto)    (struct socket *sock, void *buff, int len, int nonblock,
             unsigned flags, struct sockaddr *, int addr_len);
  int    (*recvfrom)    (struct socket *sock, void *buff, int len, int nonblock,
             unsigned flags, struct sockaddr *, int *addr_len);
  int    (*shutdown)    (struct socket *sock, int flags);
  int    (*setsockopt)    (struct socket *sock, int level, int optname,
             char *optval, int optlen);
  int    (*getsockopt)    (struct socket *sock, int level, int optname,
             char *optval, int *optlen);
  int    (*fcntl)    (struct socket *sock, unsigned int cmd,
             unsigned long arg);    
};

linux-1.2.13linuxincludelinuxfs.h 文件定义 inode 和 file 结构体

struct inode {
    dev_t        i_dev;
    unsigned long    i_ino;
    umode_t        i_mode;
    nlink_t        i_nlink;
    uid_t        i_uid;
    gid_t        i_gid;
    dev_t        i_rdev;
    off_t        i_size;
    time_t        i_atime;
    time_t        i_mtime;
    time_t        i_ctime;
    unsigned long    i_blksize;
    unsigned long    i_blocks;
    unsigned long    i_version;
    struct semaphore i_sem;
    struct inode_operations * i_op;
    struct super_block * i_sb;
    struct wait_queue * i_wait;
    struct file_lock * i_flock;
    struct vm_area_struct * i_mmap;
    struct inode * i_next, * i_prev;
    struct inode * i_hash_next, * i_hash_prev;
    struct inode * i_bound_to, * i_bound_by;
    struct inode * i_mount;
    unsigned short i_count;
    unsigned short i_wcount;
    unsigned short i_flags;
    unsigned char i_lock;
    unsigned char i_dirt;
    unsigned char i_pipe;
    unsigned char i_sock;
    unsigned char i_seek;
    unsigned char i_update;
    union {
        struct pipe_inode_info pipe_i;
        struct minix_inode_info minix_i;
        struct ext_inode_info ext_i;
        struct ext2_inode_info ext2_i;
        struct hpfs_inode_info hpfs_i;
        struct msdos_inode_info msdos_i;
        struct umsdos_inode_info umsdos_i;
        struct iso_inode_info isofs_i;
        struct nfs_inode_info nfs_i;
        struct xiafs_inode_info xiafs_i;
        struct sysv_inode_info sysv_i;
        struct socket socket_i;
        void * generic_ip;
    } u;
};

struct file {
    mode_t f_mode;
    loff_t f_pos;
    unsigned short f_flags;
    unsigned short f_count;
    off_t f_reada;
    struct file *f_next, *f_prev;
    int f_owner;        /* pid or -pgrp where SIGIO should be sent */
    struct inode * f_inode;
    struct file_operations * f_op;
    unsigned long f_version;
    void *private_data;    /* needed for tty driver, and maybe others */
};
原文地址:https://www.cnblogs.com/happygirl-zjj/p/7130421.html