Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
packet.c File Reference

パケット構築・解析モジュール。 More...

#include <stddef.h>
#include <string.h>
#include <arpa/inet.h>
#include <porter_const.h>
#include <porter_type.h>
#include "packet.h"
Include dependency graph for packet.c:

Go to the source code of this file.

Functions

static uint64_t hton64 (uint64_t v)
static uint64_t ntoh64 (uint64_t v)
static void fill_session_hdr (PotrPacket *packet, const PotrPacketSessionHdr *shdr)
int packet_build_nack (PotrPacket *packet, const PotrPacketSessionHdr *shdr, uint32_t nack_num)
 NACK パケットを構築します。
int packet_build_ping (PotrPacket *packet, const PotrPacketSessionHdr *shdr, uint32_t seq_num, uint32_t ack_num)
 PING パケットを構築します。
int packet_build_reject (PotrPacket *packet, const PotrPacketSessionHdr *shdr, uint32_t seq_num)
 再送不能通知 (REJECT) パケットを構築します。
int packet_build_fin (PotrPacket *packet, const PotrPacketSessionHdr *shdr)
 正常終了通知 (FIN) パケットを構築します。
int packet_build_packed (PotrPacket *out, const PotrPacketSessionHdr *shdr, uint32_t seq_num, const void *packed_payload, size_t payload_len)
 データパケット (パックコンテナ) を構築します。
int packet_unpack_next (const PotrPacket *container, size_t *offset, PotrPacket *elem_out)
 データパケットから次のペイロードエレメントを取り出します。
int packet_parse (PotrPacket *packet, const void *buf, size_t buf_len)
 受信バイト列をパケット構造体に解析します。
size_t packet_wire_size (const PotrPacket *packet)
 パケットのヘッダー + ペイロードの合計バイト数を返します。

Detailed Description

パケット構築・解析モジュール。

Author
c-modernization-kit sample team
Date
2026/03/04
Version
1.0.0

Definition in file packet.c.

Function Documentation

◆ hton64()

uint64_t hton64 ( uint64_t v)
static

Definition at line 31 of file packet.c.

Referenced by fill_session_hdr(), and ntoh64().

Here is the caller graph for this function:

◆ ntoh64()

uint64_t ntoh64 ( uint64_t v)
static

Definition at line 39 of file packet.c.

References hton64().

Referenced by packet_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_session_hdr()

void fill_session_hdr ( PotrPacket * packet,
const PotrPacketSessionHdr * shdr )
static

Definition at line 45 of file packet.c.

References hton64(), PotrPacket::service_id, PotrPacketSessionHdr::service_id, PotrPacket::session_id, PotrPacketSessionHdr::session_id, PotrPacket::session_tv_nsec, PotrPacketSessionHdr::session_tv_nsec, PotrPacket::session_tv_sec, and PotrPacketSessionHdr::session_tv_sec.

Referenced by packet_build_fin(), packet_build_nack(), packet_build_packed(), packet_build_ping(), and packet_build_reject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_build_nack()

int packet_build_nack ( PotrPacket * packet,
const PotrPacketSessionHdr * shdr,
uint32_t nack_num )

NACK パケットを構築します。

Parameters
[out]packet構築結果を格納するパケット構造体へのポインタ。
[in]shdrセッション識別ヘッダーへのポインタ。
[in]nack_num再送要求する通番。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

Definition at line 62 of file packet.c.

References PotrPacket::ack_num, fill_session_hdr(), PotrPacket::flags, PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_FLAG_NACK, POTR_SUCCESS, and PotrPacket::seq_num.

Referenced by n1_send_nack(), and send_nack().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_build_ping()

int packet_build_ping ( PotrPacket * packet,
const PotrPacketSessionHdr * shdr,
uint32_t seq_num,
uint32_t ack_num )

PING パケットを構築します。

Parameters
[out]packet構築結果を格納するパケット構造体へのポインタ。
[in]shdrセッション識別ヘッダーへのポインタ。
[in]seq_num通番 (ウィンドウ管理に使用)。
[in]ack_numPING 要求では 0、PING 応答では要求の seq_num を指定する。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

ヘルスチェックパケットです。ペイロードなし (payload_len=0)。
通番には送信側の next_seq(次に送出する DATA に割り当てる通番)を格納します。
PING はウィンドウに登録されません(NACK・再送の対象外)。
ack_num == 0 は PING 要求。受信者は seq_num を上限として欠番を一括 NACK します。
ack_num != 0 は PING 応答 (unicast_bidir 専用)。受信者は gap スキャンを行いません。
応答時は ack_num = req_seq_num + 1 を指定することで、req_seq_num=0 でも ack_num=0 になりません。

Definition at line 99 of file packet.c.

References PotrPacket::ack_num, fill_session_hdr(), PotrPacket::flags, PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_FLAG_PING, POTR_SUCCESS, and PotrPacket::seq_num.

Referenced by health_thread_func(), n1_send_ping_reply(), send_ping_reply(), tcp_health_thread_func(), and tcp_send_ping_reply().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_build_reject()

int packet_build_reject ( PotrPacket * packet,
const PotrPacketSessionHdr * shdr,
uint32_t seq_num )

再送不能通知 (REJECT) パケットを構築します。

Parameters
[out]packet構築結果を格納するパケット構造体へのポインタ。
[in]shdrセッション識別ヘッダーへのポインタ。
[in]seq_num再送不能な通番。ack_num フィールドに格納します。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

受信者から NACK を受け取ったが、送信ウィンドウに該当パケットが存在しない場合に 送信者が返すパケットです。受信者はこのパケットを受け取ると即時 DISCONNECTED を 発火し、欠落通番をスキップして後続パケットの配信を継続します。

Definition at line 132 of file packet.c.

References PotrPacket::ack_num, fill_session_hdr(), PotrPacket::flags, PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_FLAG_REJECT, POTR_SUCCESS, and PotrPacket::seq_num.

Referenced by n1_send_reject(), and send_reject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_build_fin()

int packet_build_fin ( PotrPacket * packet,
const PotrPacketSessionHdr * shdr )

正常終了通知 (FIN) パケットを構築します。

Parameters
[out]packet構築結果を格納するパケット構造体へのポインタ。
[in]shdrセッション識別ヘッダーへのポインタ。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

送信者が potrCloseService 時に送出する終了通知パケットです。ペイロードなし。
受信者はこのパケットを受け取ると即座に POTR_EVENT_DISCONNECTED を発火します。

Definition at line 163 of file packet.c.

References PotrPacket::ack_num, fill_session_hdr(), PotrPacket::flags, PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_FLAG_FIN, POTR_SUCCESS, and PotrPacket::seq_num.

Referenced by peer_send_fin(), and send_fin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_build_packed()

int packet_build_packed ( PotrPacket * out,
const PotrPacketSessionHdr * shdr,
uint32_t seq_num,
const void * packed_payload,
size_t payload_len )

データパケット (パックコンテナ) を構築します。

Parameters
[out]out構築結果を格納するパケット構造体へのポインタ。
[in]shdrセッション識別ヘッダーへのポインタ。
[in]seq_num外側パケットの通番。再送・順序整列に使用する。
[in]packed_payload送信スレッドが構築したペイロードエレメント列。
[in]payload_lenpacked_payload のバイト数。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

すべてのデータパケットはパックコンテナ形式で送受信します。
ペイロードエレメントが 1 件のみの場合も同じ形式を使用します。
再送・順序整列の単位は外側パケット (本関数が構築する UDP ペイロード) であり、 通番は外側パケットの seq_num フィールドで管理します。
ペイロードエレメントの形式は flags(2) + payload_len(4) + payload(N) です。
受信者は POTR_FLAG_DATA を検出後 packet_unpack_next() でペイロードエレメントを展開します。

Definition at line 200 of file packet.c.

References PotrPacket::ack_num, fill_session_hdr(), PotrPacket::flags, PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_FLAG_DATA, POTR_MAX_PAYLOAD, POTR_SUCCESS, and PotrPacket::seq_num.

Referenced by flush_packed(), and flush_packed_peer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_unpack_next()

int packet_unpack_next ( const PotrPacket * container,
size_t * offset,
PotrPacket * elem_out )

データパケットから次のペイロードエレメントを取り出します。

Parameters
[in]containerpacket_parse() 済みのデータパケット (POTR_FLAG_DATA)。
[in,out]offsetコンテナ payload 内の読み取り位置。呼び出し毎に更新。
[out]elem_out取り出したペイロードエレメントを格納する構造体へのポインタ。
Returns
ペイロードエレメントを取り出せた場合は POTR_SUCCESS、 末尾に達した場合またはエラーの場合は POTR_ERROR を返します。

ペイロードエレメントの形式は flags(2) + payload_len(4) + payload(N) です。
通番は外側パケットで管理するためペイロードエレメントには含まれません。
container->payload_len はホストバイトオーダー (packet_parse() 変換済み) で参照します。
elem_out の session 情報は container から引き継ぎます。

Definition at line 238 of file packet.c.

References PotrPacket::ack_num, PotrPacket::flags, PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_MAX_PAYLOAD, POTR_PAYLOAD_ELEM_HDR_SIZE, POTR_SUCCESS, PotrPacket::service_id, PotrPacket::session_id, PotrPacket::session_tv_nsec, and PotrPacket::session_tv_sec.

Referenced by drain_recv_window(), n1_drain_recv_window(), and tcp_recv_thread_func().

Here is the caller graph for this function:

◆ packet_parse()

int packet_parse ( PotrPacket * packet,
const void * buf,
size_t buf_len )

受信バイト列をパケット構造体に解析します。

Parameters
[out]packet解析結果を格納するパケット構造体へのポインタ。
[in]buf受信バイト列へのポインタ。
[in]buf_len受信バイト列の長さ。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

各フィールドをホストバイトオーダーに変換して構造体に格納します。

Definition at line 299 of file packet.c.

References PotrPacket::ack_num, PotrPacket::flags, ntoh64(), PACKET_HEADER_SIZE, PotrPacket::payload, PotrPacket::payload_len, POTR_ERROR, POTR_MAX_PAYLOAD, POTR_SUCCESS, PotrPacket::seq_num, PotrPacket::service_id, PotrPacket::session_id, PotrPacket::session_tv_nsec, and PotrPacket::session_tv_sec.

Referenced by receiver_accept_loop(), recv_thread_func(), and tcp_recv_thread_func().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ packet_wire_size()

size_t packet_wire_size ( const PotrPacket * packet)

パケットのヘッダー + ペイロードの合計バイト数を返します。

Parameters
[in]packet対象のパケット構造体へのポインタ。 packet_build_*() で構築した NBO パケットを渡すこと。 packet_parse() 済み (ホストバイトオーダー) のパケットを 渡すと payload_len の ntohs 変換が二重になり誤値を返す。
Returns
パケットの送信サイズ (バイト)。packet が NULL の場合は 0。

UDP 送信時に sendto() へ渡すバイト数を求めるために使用します。
内部で ntohs(packet->payload_len) を呼ぶため、引数は必ず NBO 状態で渡してください。

Definition at line 347 of file packet.c.

References PACKET_HEADER_SIZE, and PotrPacket::payload_len.

Referenced by health_thread_func(), n1_send_nack(), n1_send_ping_reply(), n1_send_reject(), peer_send_fin(), recv_thread_func(), send_fin(), send_nack(), send_ping_reply(), send_reject(), and tcp_health_thread_func().

Here is the caller graph for this function: