|
| 1 | +// |
| 2 | +// Created by hrw on 2021/03/05. |
| 3 | +// |
| 4 | +#include <sys/epoll.h> |
| 5 | +#include <sys/types.h> /* basic system data types */ |
| 6 | +#include <sys/socket.h> /* basic socket definitions */ |
| 7 | +#include <sys/time.h> /* timeval{} for select() */ |
| 8 | +#include <time.h> /* timespec{} for pselect() */ |
| 9 | +#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */ |
| 10 | +#include <arpa/inet.h> /* inet(3) functions */ |
| 11 | +#include <errno.h> |
| 12 | +#include <fcntl.h> /* for nonblocking */ |
| 13 | +#include <netdb.h> |
| 14 | +#include <signal.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <string.h> |
| 18 | +#include <sys/stat.h> /* for S_xxx file mode constants */ |
| 19 | +#include <sys/uio.h> /* for iovec{} and readv/writev */ |
| 20 | +#include <unistd.h> |
| 21 | +#include <sys/wait.h> |
| 22 | +#include <sys/un.h> /* for Unix domain sockets */ |
| 23 | + |
| 24 | +#include <sys/select.h> /* for convenience */ |
| 25 | +#include <sys/sysctl.h> |
| 26 | +#include <poll.h> /* for convenience */ |
| 27 | +#include <strings.h> /* for convenience */ |
| 28 | +#include <sys/ioctl.h> |
| 29 | +#include <pthread.h> |
| 30 | +#include <assert.h> |
| 31 | +#include <linux/types.h> |
| 32 | + |
| 33 | +#ifndef MAX_EVENT_NUMBER |
| 34 | +#define MAX_EVENT_NUMBER 1024 |
| 35 | + |
| 36 | +// 普通的 BSD 标准 socket 结构体 |
| 37 | +// socket_state: socket 状态, 连接?不连接? |
| 38 | +// type: socket type (%SOCK_STREAM, etc) |
| 39 | +// flags: socket flags (%SOCK_NOSPACE, etc) |
| 40 | +// ops: 专用协议的socket的操作 |
| 41 | +// file: 与socket 有关的指针列表 |
| 42 | +// sk: 负责协议相关结构体,这样就让这个这个结构体和协议分开。 |
| 43 | +// wq: 等待队列 |
| 44 | +struct socket { |
| 45 | + socket_state state; |
| 46 | + kmemcheck_bitfield_begin(type); |
| 47 | + short type; |
| 48 | + kmemcheck_bitfield_end(type); |
| 49 | + unsigned long flags; |
| 50 | + struct socket_wa __rcu *wq; |
| 51 | + struct file *file; |
| 52 | + struct sock *sk; |
| 53 | + const struct proto_ops *ops; |
| 54 | +}; |
| 55 | + |
| 56 | +//file:net/socket.c |
| 57 | +SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) |
| 58 | +{ |
| 59 | +//...... |
| 60 | + retval = sock_create(family, type, protocol, &sock); |
| 61 | +} |
| 62 | + |
| 63 | +//sock_create 是创建 socket 的主要位置。其中 sock_create 又调用了 __sock_create。 |
| 64 | + |
| 65 | +//file:net/socket.c |
| 66 | +int __sock_create(struct net *net, int family, int type, int protocol, |
| 67 | + struct socket **res, int kern) |
| 68 | +{ |
| 69 | + struct socket *sock; |
| 70 | + const struct net_proto_family *pf; |
| 71 | + |
| 72 | + //...... |
| 73 | + |
| 74 | + //分配 socket 对象 |
| 75 | + sock = sock_alloc(); |
| 76 | + |
| 77 | + //获得每个协议族的操作表 |
| 78 | + pf = rcu_dereference(net_families[family]); |
| 79 | + |
| 80 | + //调用每个协议族的创建函数, 对于 AF_INET 对应的是 |
| 81 | + err = pf->create(net, sock, protocol, kern); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
0 commit comments