---
title: Rustで作るTCP
tags: 
author: [ゆうカリ🌿](https://docswell.com/user/Eucalyptus_0403)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/2JVV4VDYJQ.jpg?width=480
description: RustでTCPを実装しました。
published: May 16, 26
canonical: https://docswell.com/s/Eucalyptus_0403/KE1L21-Rust_TCP
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/2JVV4VDYJQ.jpg)

Rustで作るTCP
Eucalyptus_0403
Okayama Revengers LT大会 #3
1


# Page. 2

![Page Image](https://bcdn.docswell.com/page/5EGL1LYWJL.jpg)

自己紹介
➢岡山大学工学部工学科情報・電気・数理
データサイエンス系ネットワーク工学コー
ス4年医用情報ネットワーク学研究室所属
➢岡山大学大学院ヘルスシステム統合科学研
究科医用機器医用材料部門医用情報ネット
ワーク学分野進学予定
➢okarev LT大会 2回目（前回＃2参加）
Okayama Revengers LT大会 #3
Eucalyptus_0403
2


# Page. 3

![Page Image](https://bcdn.docswell.com/page/4JQYDYMQ7P.jpg)

TCPとは
➢Transmission Control Protocol の略
• RFC793で規定（改訂版：RFC9293）
• 高速通信
＜
安全・信頼性
• 高速さを優先： UDP（User Datagram Protocol）
➢宅配便で例えると…
All rights reserved.
• ヤマト運輸（ちょい遅いけど丁寧に届けるよ）→
TCP
• Amazon（たまに荷物投げてまうかも (m´・ω・｀)m）→
UDP
ヤマトも荷物投げることあるみたい？
Okayama Revengers LT大会 #3
Eucalyptus_0403
3


# Page. 4

![Page Image](https://bcdn.docswell.com/page/K74WZWVYE1.jpg)

Rustで実装しよう
➢Rustとは
• Mozillaが支援し2006年に開発スタート
• C, C＋＋ に代わる言語
• 処理速度・並行処理・安全性
Okayama Revengers LT大会 #3
Eucalyptus_0403
4


# Page. 5

![Page Image](https://bcdn.docswell.com/page/LJ1YRY3NEG.jpg)

実験環境
➢仮想的に以下のネットワークを構築
• Ubuntu 24.04 LTS
host1-veth1
host2-veth1
router
RTXはいいゾ！
.1
.254
.254
10.0.0.0/24
Okayama Revengers LT大会 #3
.1
10.0.1.0/24
Eucalyptus_0403
5


# Page. 6

![Page Image](https://bcdn.docswell.com/page/GJWG1GXM72.jpg)

簡易実装で通信開始
➢線鮫で小包を捕獲（hoet1 → host2）
Bogus TCP header length
Okayama Revengers LT大会 #3
Eucalyptus_0403
6


# Page. 7

![Page Image](https://bcdn.docswell.com/page/4EZLPL6M73.jpg)

tcpdumpで見てみる
➢なんかおかしい
• TCPヘッダ長がおかしい
• 簡易的な実装にしているため
➢より本格的な実装へ
• three-way handshaking
• Retransmission
Okayama Revengers LT大会 #3
Eucalyptus_0403
7


# Page. 8

![Page Image](https://bcdn.docswell.com/page/Y76WMW257V.jpg)

TCPの完成
➢three-way handshaking
• TCPコネクションの確立
Okayama Revengers LT大会 #3
Eucalyptus_0403
9


# Page. 9

![Page Image](https://bcdn.docswell.com/page/G75MZM2G74.jpg)

実際に情報を送ってみる
➢Server
Okayama Revengers LT大会 #3
➢Client
Eucalyptus_0403
11


# Page. 10

![Page Image](https://bcdn.docswell.com/page/9J29R94DER.jpg)

今回の実装に関して
➢RSTセグメントを破棄
• 自作TCPが流すSYNセグメントがOSのTCPと競合
➢チェックサムオフロードの無効化
• エラー検出などに透明性をもたせるため
➢IPv4のみ対応
➢輻輳制御，フロー制御，セキュリティの考慮は無し
Okayama Revengers LT大会 #3
Eucalyptus_0403
12


# Page. 11

![Page Image](https://bcdn.docswell.com/page/DEY4D4MMJM.jpg)

ぎてゅｂにあるよ
➢https://github.com/teru01/toytcp
➢そのへんに転がってるUbuntuでできる
➢Rustで始めるTCP自作入門
• 著：小野輝也
➢動かすだけなら簡単
• Rustのインストール
• git clone
→
Okayama Revengers LT大会 #3
cargo build
Eucalyptus_0403
13


