You are on page 1of 11

1

1.1
1
1 Socket

Socket
Socket

Socket
2 2 Socket
Socket Socket Socket
Socket Socket
Socket
Socket
Socket Socket
socket Socket
Socket
Socket
Socket 3G 4G
Socket
Socket 3G CPU
Socket

Socket Socket
Socket
Socket Socket
Socket 1.1
Socket

- 1 -

Socket

Socket

socket( )
Socket

socket( )

Socket

bind( )

Socket

listen( )

connect( )

accept( )

read( )

write( )

write( )

read( )

close( )Socket

close( )Socket

1.1

1.1 Socket
Socket
1.1

1.1 Socket
Socket
1.1 9
Linux
1.1
int main()
{
int server_fd,client_fd;
int server_len,client_len;
struct sockaddr_in server_address;

- 2 -


struct sockaddr_in client_address;
char back[] = {"Im server"};
/* Socket*/
server_fd = socket(AF_INET,SOCK_STREAM,0);
/* Socket IP IP 192.168.1.1
9266*/
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("192.168.1.1");
server_address.sin_port = htons(9266);
server_len = sizeof(server_address);
/* Socket
*/
bind(server_fd,(struct sockaddr*)&server_address,server_len);
/* Socket 10 Socket */
listen(server_fd,10);
while(1) {
char recv[20];
printf("server is waiting\n");
/* Socket
client_address ID */
client_len = sizeof(client_address);
client_fd = accept(server_fd,(struct sockaddr*)&client_address,&client_len);
/* read write */
read(client_fd,recv,20);
write(client_fd,back,20);
printf("received from client= %s\n",recv);
close(client_fd);
}
close(server_fd);
exit(0);
}
1.1
1.1 1.1

C
TCP/IP

Linux

Linux

- 3 -

Linux Linux

1.2

/IP

1TCP/IP
TCP/IP 1.2 TCP/IP

Telnet Email

TCP UDP

IP

1.2

/IP

1.2
RealTek 8169 DM9000 CS8900

TCP/UDP
TCP (Transmission Control Protocol ) UDP(User Datagram Protocol
)TCP IP

UDP IP TCP
UDP TCP
Telnet(Internet )FTP()SMTP()
UDP NFSSNMPDNS
TFTP
Linux TCP
TCP

- 4 -


TCP

TCP

TCP

IP

IP

1.3

/IP

1.3 TCP/IP 1.1

1.2 TCP

1.3

Linux C GNU glibc


GNU
Linux www.kernel.org glibc 2.3.6
glibc socket
glibc-2.3.6/sysdeps/generic socke.c
1.2

socket()

#include <errno.h>
#include <sys/socket.h>
/* Create a new socket of type TYPE in domain DOMAIN, using
protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
Returns a file descriptor for the new socket, or -1 for errors. */
int
__socket (domain, type, protocol)
int domain;
int type;

- 5 -


int protocol;
{
__set_errno (ENOSYS);
return -1;
}

weak_alias (__socket, socket)


stub_warning (socket)
#include <stub-tag.h>
# define weak_alias(name, aliasname) _weak_alias (name, aliasname)
# define _weak_alias(name, aliasname) \
extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
1.2 socket weak_alias socket
_socket_socket socket
Weak Alias GCC weak glibc
weak_alia glibc weak symbol
_socket_socket
glibc-2.3.6/sysdeps/unix/sysv/linux/i386
socket.S
1.3

_socket

ENTRY (__socket)

movl $SYS_ify(socketcall), %eax

/* System call number in %eax. */

/* Use ## so `socket' is a separate token that might be #define'd. */


movl $P(SOCKOP_,socket), %ebx
lea 4(%esp), %ecx

/* Subcode is first arg to syscall. */

/* Address of args is 2nd arg. */

/* Do the system call trap. */


ENTER_KERNEL

// I386 ENTER_KERNEL SYS_ify


# define ENTER_KERNEL int $0x80
#define SYS_ify(syscall_name) __NR_##syscall_name
#define P(a, b) P2(a, b)
#define P2(a, b) a##b

ENTRY (__socket)

movl $__NR_socketcall, %eax

/* System call number in %eax. */

/* Use ## so `socket' is a separate token that might be #define'd. */


movl $SOCKOP_socket, %ebx

/* Subcode is first arg to syscall. */

- 6 -


lea 4(%esp), %ecx

/* Address of args is 2nd arg. */

/* Do the system call trap. */


int $0x80

glibc Linux __NR_ socketcall include/asm-x86/unistd_32.h

#define __NR_socketcall

102

SOCKOP_socket glibc-2.3.6/sysdeps/unix/sysv/linux/socketcall.h
#define SOCKOP_socket

Socket ebx lea 4(%esp), %ecx


socket ecx
ebx ecx
CPU
AF_INETSOCK_STREAM0
ecx
socket glibc 102
eax int $0x80 system_call
Linux arch/x86/kernel/entry_32.S
1.4

system_call

ENTRY(system_call)

syscall_call:
call *sys_call_table(,%eax,4)

1.4 system_call
call sys_call_table 102 sys_call_table
arch/x86/kernel/syscall_table_32.S
1.5

sys_call_table

ENTRY(sys_call_table)

.long sys_socketcall

/*102*/

1.5 socket sys_socketcall bind()


listenaccept
1.1 1.1 glibc
sys_socketcall IPV4Linux 2.6.26
sys_socketcall Socket
sys_socketcall 1.6
1.6 sys_socketcall
2007 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
2008 {
2009 unsigned long a[6];
- 7 -


2010 unsigned long a0, a1;

2017
2018

if (copy_from_user(a, args, nargs[call]))//


return -EFAULT;

2024

a0 = a[0];

2025

a1 = a[1];

2026
2027 switch (call) {//
2028
2029

case SYS_SOCKET:
err = sys_socket(a0, a1, a[2]);

}
sys_socketcall Linux Socket
ebx ecx call Socket ebx 1
2028 SYS_SOCKET
args ecx Linux

copy_from_user
copy_from_user nargs[] call

1.7 nargs[]
1990 #define AL(x) ((x) * sizeof(unsigned long))
1991 static const unsigned char nargs[18]={
1992

AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),

1993

AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),

1994

AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)

1995 };
call AL 12
copy_from_user args AF_INETSOCK_STREAM0
a
include/linux/net.h call
glibc sysdeps/unix/sysv/linux socketcall.h
Linux glibc Linux glibc
1.8

call

32

#define SYS_SOCKET 1 /* sys_socket(2) */

33

#define SYS_BIND 2 /* sys_bind(2)

34

#define SYS_CONNECT 3 /* sys_connect(2) */

35

#define SYS_LISTEN 4 /* sys_listen(2) */

36

#define SYS_ACCEPT 5 /* sys_accept(2) */

37

#define SYS_GETSOCKNAME 6 /* sys_getsockname(2

38

#define SYS_GETPEERNAME 7 /* sys_getpeername(2

39

#define SYS_SOCKETPAIR 8 /* sys_socketpair(2)

*/

- 8 -


40

#define SYS_SEND 9 /* sys_send(2)

*/

41

#define SYS_RECV 10 /* sys_recv(2)

*/

42

#define SYS_SENDTO 11 /* sys_sendto(2) */

43

#define SYS_RECVFROM 12 /* sys_recvfrom(2)

44

#define SYS_SHUTDOWN 13 /* sys_shutdown(2)

45

#define SYS_SETSOCKOPT 14 /* sys_setsockopt(2)

46

#define SYS_GETSOCKOPT 15 /* sys_getsockopt(2)

47

#define SYS_SENDMSG 16 /* sys_sendmsg(2) */

48

#define SYS_RECVMSG 17 /* sys_recvmsg(2) */


call sys_socketcall switch 1.8

2
sys_socketcall
1.1 socket
sys_socket Socket
-->
sys_socketcall()-->sys_socket()
1216 asmlinkage long sys_socket(int family, int type, int protocol)
1217 {

1221

retval = sock_create(family, type, protocol, &sock);// socket

1225

retval = sock_map_fd(sock);//

1236 }
1221 sock_create Socket
family AF_INETtype SOCK_STREAMprotocol 0
sock_map_fd Socket

1.4

sock_create
sock_fs_type 2.6.26
net/socket.c
1.9
302

static struct file_system_type sock_fs_type = {

303

.name = "sockfs",

304

.get_sb = sockfs_get_sb,

305

.kill_sb = kill_anon_super,

306

sock_fs_type

};
file_system_type Linux

file_system_type sock_fs_type sockfs

- 9 -


file_system_type

Linux init/main.c kernel_init


do_basic_setup do_initcalls init
kernel_init()-->do_basic_setup()-->do_initcalls()-->do_one_initcall()
741 static void __init do_initcalls(void)
742 {

745

for (call = __initcall_start; call < __initcall_end; call++)

746

do_one_initcall(*call);//

750 }
696 static void __init do_one_initcall(initcall_t fn)
697 {

708

result = fn();//

736 }
do_one_initcall initcall initcall
GCC
__initcall_start __initcall_end do_one_initcall initcall
socket.c core_initcall sock_init initcall

1.10

sock_init

2188 core_initcall(sock_init); /* early initcall */


include/init.h core_initcall
1.11

core_initcall

168

#define __define_initcall(level,fn,id) \

169

static initcall_t __initcall_##fn##id __used \

170

__attribute__((__section__(".initcall" level ".init"))) = fn

180

#define core_initcall(fn) __define_initcall("1",fn,1)

191

#define device_initcall(fn)

__define_initcall("6",fn,6)

196

#define __initcall(fn) device_initcall(fn)

251

#define module_init(x) __initcall(x);


GCC sock_init initcall Linux initcall

module_init
initcall sock_init
sock_init
kernel_init()-->do_basic_setup()-->do_initcalls()-->do_one_initcall()-->sock_init()
2157 static int __init sock_init(void)

- 10 -


2158 {

2174

init_inodecache();

2175

register_filesystem(&sock_fs_type);//

2176

sock_mnt = kern_mount(&sock_fs_type);//

2186 }
sock_init sock_fs_type
Socket register_filesystem
Linux kern_mount Linux

sock_fs_type get_sb

1.9 sock_fs_type get_sb


sockfs_get_sb
sock_init()-->kern_mount()-->--> sockfs_get_sb()
292

static int sockfs_get_sb(struct file_system_type *fs_type,

293

int flags, const char *dev_name, void *data,

294
295
296

struct vfsmount *mnt)


{
return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,

297
298

mnt);//
}
get_sb_pseudo sock_fs_type

socket
sockfs_ops sockfs_ops
Socket
1.12
286

sockfs_ops

static struct super_operations sockfs_ops = {

287

.alloc_inode = sock_alloc_inode,

288

.destroy_inode =sock_destroy_inode,

289

.statfs = simple_statfs,

290

};

Linux Socket
sockfs_ops alloc_inode sock_alloc_inode
get_sb_pseudo Socket

- 11 -

You might also like