naobe @ ウィキ
Linux Kernel
最終更新:
Bot(ページ名リンク)
-
view
Linuxに戻る
目標
TCPの実装理解。
- 状態遷移
- パケットの送信と受信
参考URL
TCPのソースの読み進め方針
クライアント、サーバのsocket関係のコードを順に追ってkernelの中にまで入っていきたい。
クライアント
socket TCP/UDPを指定してsocketのfile descriptorを得る connect socketとサーバアドレスを接続する。synを送信する。 send socketを通じてサーバにデータを送る recv 受信バッファにデータがあれば、読み込む。 shutdown 回線を遮断する。バッファに残ったデータは捨てる。 close 回線を閉じる
サーバ
socket bind socketをサーバアドレス/ポートと接続する。 listen socketを登録する。 accept clientからの接続があるまで待機する。新たなsocketを作成する。ackを送信する。 recv send shutdown close
socket
参考URLにあるように、kernel内部の構造体を返すようだ。
connect
ソース検索
$ grep -r asmlinkage . | grep connect ./arch/arm/kernel/sys_oabi-compat.c:asmlinkage long sys_oabi_connect(int fd, str uct sockaddr __user *addr, int addrlen) ./include/linux/syscalls.h:asmlinkage long sys_connect(int, struct sockaddr __user *, int);
ソースにはconnect関数はない。
socketと同様に
#include <sys/types.h> #include <sys/socket.h> int main(int argc, char *argv[]) { struct sockaddr *serv_addr; socklen_t addrlen; int ret = connect(10, serv_addr, addrlen); }
[suna@centos c]$ gcc connect.c -o connects [suna@centos c]$ nm connects 08049594 b completed.5788 U connect@@GLIBC_2.0
connectはglibc2.0のライブラリ関数であることがわかります
[suna@centos c]$ gcc -static -g connects.c [suna@centos c]$ l a.out* connects* connects.c tst* tst.c [suna@centos c]$ [[gdb]] a.out GNU gdb Red Hat Linux (6.5-37.el5rh) Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1". (gdb) disassemble connect Dump of assembler code for function connect: 0x08050810 <connect+0>: cmpl $0x0,%gs:0xc 0x08050818 <connect+8>: jne 0x8050838 <connect+40> 0x0805081a <connect+10>: mov %ebx,%edx 0x0805081c <connect+12>: mov $0x66,%eax 0x08050821 <connect+17>: mov $0x3,%ebx 0x08050826 <connect+22>: lea 0x4(%esp),%ecx 0x0805082a <connect+26>: int $0x80 0x0805082c <connect+28>: mov %edx,%ebx 0x0805082e <connect+30>: cmp $0xffffff83,%eax 0x08050831 <connect+33>: jae 0x80518b0 <__syscall_error> 0x08050837 <connect+39>: ret 0x08050838 <connect+40>: push %esi 0x08050839 <connect+41>: call 0x80509a0 <__libc_enable_asynccancel> 0x0805083e <connect+46>: mov %eax,%esi 0x08050840 <connect+48>: mov %ebx,%edx 0x08050842 <connect+50>: mov $0x66,%eax 0x08050847 <connect+55>: mov $0x3,%ebx 0x0805084c <connect+60>: lea 0x8(%esp),%ecx 0x08050850 <connect+64>: int $0x80 0x08050852 <connect+66>: mov %edx,%ebx 0x08050854 <connect+68>: xchg %eax,%esi 0x08050855 <connect+69>: call 0x8050960 <__libc_disable_asynccancel> 0x0805085a <connect+74>: mov %esi,%eax 0x0805085c <connect+76>: pop %esi 0x0805085d <connect+77>: cmp $0xffffff83,%eax 0x08050860 <connect+80>: jae 0x80518b0 <__syscall_error> 0x08050866 <connect+86>: ret 0x08050867 <connect+87>: nop