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

N:1 モード用ピアテーブル管理の実装。 More...

#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include <porter_const.h>
#include <porter.h>
#include "potrContext.h"
#include "potrPeerTable.h"
#include "protocol/packet.h"
#include "protocol/window.h"
#include "infra/potrLog.h"
#include "infra/crypto/crypto.h"
Include dependency graph for potrPeerTable.c:

Go to the source code of this file.

Macros

#define POTR_MUTEX_INIT(m)
#define POTR_MUTEX_DESTROY(m)

Functions

static void peer_generate_session (PotrPeerContext *peer)
static PotrPeerId allocate_peer_id (struct PotrContext_ *ctx)
void peer_send_fin (struct PotrContext_ *ctx, PotrPeerContext *peer)
 ピアの全パスへ FIN パケットを送信する。
int peer_table_init (struct PotrContext_ *ctx)
 ピアテーブルを初期化する。
void peer_table_destroy (struct PotrContext_ *ctx)
 ピアテーブルを破棄する。
PotrPeerContextpeer_find_by_session (struct PotrContext_ *ctx, uint32_t session_id, int64_t session_tv_sec, int32_t session_tv_nsec)
 session_triplet でピアを検索する。
PotrPeerContextpeer_find_by_id (struct PotrContext_ *ctx, PotrPeerId peer_id)
 peer_id でピアを検索する。
PotrPeerContextpeer_create (struct PotrContext_ *ctx, const struct sockaddr_in *sender_addr, int path_idx)
 新規ピアを作成する。
void peer_path_clear (struct PotrContext_ *ctx, PotrPeerContext *peer, int path_idx)
 ピアの特定パスをクリアしてスロットを未使用に戻す。
void peer_free (struct PotrContext_ *ctx, PotrPeerContext *peer)
 ピアリソースを解放してスロットをクリアする。

Detailed Description

N:1 モード用ピアテーブル管理の実装。

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

Definition in file potrPeerTable.c.

Macro Definition Documentation

◆ POTR_MUTEX_INIT

#define POTR_MUTEX_INIT ( m)
Value:
pthread_mutex_init((m), NULL)

Definition at line 44 of file potrPeerTable.c.

Referenced by peer_create(), and peer_table_init().

◆ POTR_MUTEX_DESTROY

#define POTR_MUTEX_DESTROY ( m)
Value:
pthread_mutex_destroy(m)

Definition at line 45 of file potrPeerTable.c.

Referenced by peer_create(), peer_free(), and peer_table_destroy().

Function Documentation

◆ peer_generate_session()

void peer_generate_session ( PotrPeerContext * peer)
static

Definition at line 52 of file potrPeerTable.c.

References PotrPeerContext_::session_id, PotrPeerContext_::session_tv_nsec, and PotrPeerContext_::session_tv_sec.

Referenced by peer_create().

Here is the caller graph for this function:

◆ allocate_peer_id()

PotrPeerId allocate_peer_id ( struct PotrContext_ * ctx)
static

Definition at line 79 of file potrPeerTable.c.

References PotrPeerContext_::active, PotrContext_::max_peers, PotrContext_::next_peer_id, PotrPeerContext_::peer_id, PotrContext_::peers, and POTR_PEER_ALL.

Referenced by peer_create().

Here is the caller graph for this function:

◆ peer_send_fin()

void peer_send_fin ( struct PotrContext_ * ctx,
PotrPeerContext * peer )

ピアの全パスへ FIN パケットを送信する。

ピアの dest_addr[] に対して FIN パケットを直接 sendto する。\n 呼び出し元は peers_mutex を取得してから呼び出すこと。

Parameters
[in,out]ctxセッションコンテキスト。
[in]peerFIN を送信するピアコンテキスト。

Definition at line 117 of file potrPeerTable.c.

References PotrPeerContext_::dest_addr, PotrServiceDef::encrypt_enabled, PotrServiceDef::encrypt_key, PotrPacket::flags, packet_build_fin(), PACKET_HEADER_SIZE, packet_wire_size(), PotrPacket::payload_len, POTR_CRYPTO_NONCE_SIZE, POTR_CRYPTO_TAG_SIZE, potr_encrypt(), POTR_FLAG_ENCRYPTED, POTR_INVALID_SOCKET, POTR_MAX_PATH, POTR_SUCCESS, PotrContext_::service, PotrPacketSessionHdr::service_id, PotrServiceDef::service_id, PotrPacket::session_id, PotrPacketSessionHdr::session_id, PotrPeerContext_::session_id, PotrPacketSessionHdr::session_tv_nsec, PotrPeerContext_::session_tv_nsec, PotrPacketSessionHdr::session_tv_sec, PotrPeerContext_::session_tv_sec, and PotrContext_::sock.

Referenced by peer_table_destroy(), and potrDisconnectPeer().

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

◆ peer_table_init()

int peer_table_init ( struct PotrContext_ * ctx)

ピアテーブルを初期化する。

ctx->peers を max_peers 分確保し、peers_mutex を初期化する。
ctx->max_peers, ctx->n_peers, ctx->next_peer_id を設定する。

Parameters
[in,out]ctxセッションコンテキスト。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR。

Definition at line 197 of file potrPeerTable.c.

References PotrPeerContext_::active, PotrContext_::max_peers, PotrContext_::n_peers, PotrContext_::next_peer_id, PotrContext_::peers, PotrContext_::peers_mutex, POTR_ERROR, POTR_LOG, POTR_MUTEX_INIT, POTR_SUCCESS, POTR_TRACE_ERROR, POTR_TRACE_VERBOSE, PotrContext_::service, and PotrServiceDef::service_id.

Referenced by potrOpenService().

Here is the caller graph for this function:

◆ peer_table_destroy()

void peer_table_destroy ( struct PotrContext_ * ctx)

ピアテーブルを破棄する。

全アクティブピアに FIN を送信し、リソースを解放する。
peers_mutex を解放する。

Parameters
[in,out]ctxセッションコンテキスト。

Definition at line 228 of file potrPeerTable.c.

References PotrPeerContext_::active, PotrPeerContext_::frag_buf, PotrContext_::max_peers, PotrContext_::n_peers, peer_send_fin(), PotrContext_::peers, PotrContext_::peers_mutex, POTR_LOG, POTR_MUTEX_DESTROY, POTR_TRACE_VERBOSE, PotrPeerContext_::recv_window, PotrPeerContext_::send_window, PotrPeerContext_::send_window_mutex, PotrContext_::service, PotrServiceDef::service_id, and window_destroy().

Referenced by ctx_cleanup(), and potrCloseService().

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

◆ peer_find_by_session()

PotrPeerContext * peer_find_by_session ( struct PotrContext_ * ctx,
uint32_t session_id,
int64_t session_tv_sec,
int32_t session_tv_nsec )

session_triplet でピアを検索する。

ピアテーブルを線形探索し、session_id / session_tv_sec / session_tv_nsec が 一致するアクティブなエントリを返す。
呼び出し元は peers_mutex を取得してから呼び出すこと。

Parameters
[in]ctxセッションコンテキスト。
[in]session_idピアのセッション識別子。
[in]session_tv_secピアのセッション開始時刻 秒部。
[in]session_tv_nsecピアのセッション開始時刻 ナノ秒部。
Returns
見つかった場合はピアコンテキストへのポインタ、見つからない場合は NULL。

Definition at line 268 of file potrPeerTable.c.

References PotrPeerContext_::active, PotrContext_::max_peers, PotrPeerContext_::peer_session_id, PotrPeerContext_::peer_session_tv_nsec, PotrPeerContext_::peer_session_tv_sec, and PotrContext_::peers.

Referenced by recv_thread_func().

Here is the caller graph for this function:

◆ peer_find_by_id()

PotrPeerContext * peer_find_by_id ( struct PotrContext_ * ctx,
PotrPeerId peer_id )

peer_id でピアを検索する。

呼び出し元は peers_mutex を取得してから呼び出すこと。

Parameters
[in]ctxセッションコンテキスト。
[in]peer_id検索するピア ID。
Returns
見つかった場合はピアコンテキストへのポインタ、見つからない場合は NULL。

Definition at line 292 of file potrPeerTable.c.

References PotrPeerContext_::active, PotrContext_::max_peers, PotrPeerContext_::peer_id, and PotrContext_::peers.

Referenced by potrDisconnectPeer(), potrSend(), and send_packed_peer_mode().

Here is the caller graph for this function:

◆ peer_create()

PotrPeerContext * peer_create ( struct PotrContext_ * ctx,
const struct sockaddr_in * sender_addr,
int path_idx )

新規ピアを作成する。

空きスロットを確保し、session_id/session_tv を生成してウィンドウを初期化する。
送信元アドレスを dest_addr[path_idx] に記録し、n_paths を 1 に設定する。
dest_addr[] のインデックスは ctx->sock[] / src_addr[] と直接対応する。
path_last_recv_sec[path_idx] は後続の n1_update_path_recv() 呼び出しで設定される。
max_peers 超過時はログエラー後 NULL を返す。
呼び出し元は peers_mutex を取得してから呼び出すこと。

Parameters
[in,out]ctxセッションコンテキスト。
[in]sender_addrピアの送信元アドレス (recvfrom で取得したアドレス)。
[in]path_idxパケットを受信したサーバソケットのインデックス (ctx->sock[] の添字)。
Returns
成功時はピアコンテキストへのポインタ、失敗時は NULL。

Definition at line 307 of file potrPeerTable.c.

References PotrPeerContext_::active, allocate_peer_id(), PotrPeerContext_::dest_addr, PotrPeerContext_::frag_buf, PotrPeerContext_::frag_buf_len, PotrPeerContext_::frag_compressed, PotrContext_::global, PotrGlobalConfig::max_message_size, PotrGlobalConfig::max_payload, PotrContext_::max_peers, PotrPeerContext_::n_paths, PotrContext_::n_peers, PotrPeerContext_::path_last_recv_sec, peer_generate_session(), PotrPeerContext_::peer_id, PotrContext_::peers, POTR_LOG, POTR_MUTEX_DESTROY, POTR_MUTEX_INIT, POTR_SUCCESS, POTR_TRACE_ERROR, POTR_TRACE_INFO, PotrPeerContext_::recv_window, PotrPeerContext_::send_window, PotrPeerContext_::send_window_mutex, PotrContext_::service, PotrServiceDef::service_id, window_destroy(), window_init(), and PotrGlobalConfig::window_size.

Referenced by recv_thread_func().

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

◆ peer_path_clear()

void peer_path_clear ( struct PotrContext_ * ctx,
PotrPeerContext * peer,
int path_idx )

ピアの特定パスをクリアしてスロットを未使用に戻す。

dest_addr[path_idx] をゼロクリアし、path_last_recv_sec/nsec をリセット後 n_paths を減算する。
タイムアウト・TCP FIN/RST などパス断の発生源によらず共通して呼び出す。
呼び出し元は peers_mutex を取得してから呼び出すこと。

Parameters
[in,out]ctxセッションコンテキスト。
[in,out]peer対象ピアコンテキスト。
[in]path_idxクリアするパスのインデックス (ctx->sock[] の添字)。

Definition at line 410 of file potrPeerTable.c.

References PotrPeerContext_::dest_addr, PotrPeerContext_::n_paths, PotrPeerContext_::path_last_recv_nsec, PotrPeerContext_::path_last_recv_sec, PotrPeerContext_::peer_id, POTR_LOG, POTR_TRACE_WARNING, PotrContext_::service, and PotrServiceDef::service_id.

Referenced by n1_check_health_timeout().

Here is the caller graph for this function:

◆ peer_free()

void peer_free ( struct PotrContext_ * ctx,
PotrPeerContext * peer )

ピアリソースを解放してスロットをクリアする。

ウィンドウ破棄・frag_buf 解放・send_window_mutex 解放・スロットクリアを行う。
呼び出し元は peers_mutex を取得してから呼び出すこと。
FIN の送信は呼び出し元が行うこと (本関数は送信しない)。

Parameters
[in,out]ctxセッションコンテキスト。
[in,out]peer解放するピアコンテキスト。

Definition at line 428 of file potrPeerTable.c.

References PotrPeerContext_::active, PotrPeerContext_::frag_buf, PotrContext_::n_peers, PotrPeerContext_::peer_id, POTR_LOG, POTR_MUTEX_DESTROY, POTR_TRACE_INFO, PotrPeerContext_::recv_window, PotrPeerContext_::send_window, PotrPeerContext_::send_window_mutex, PotrContext_::service, PotrServiceDef::service_id, and window_destroy().

Referenced by n1_check_health_timeout(), potrDisconnectPeer(), and recv_thread_func().

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