SCTP 庫的簡述和代碼 (2)

本站首發兼遠創, 全拼, 寫字不易, 程序調試更煩. 因此,   若是轉請保持做者信息和此聯接, 給我動力 :)
咱寫C的, 好多很炫的方法用不上, 只能用土的, 狀態表.cookie

既然是閒扯,咱就扯遠點, 爲何用狀態轉移表呢?調試

首先, 咱們有些什麼模式能夠選呢?rest

1. switch/caseorm

大哥/大姐, 什麼時代了, 寫協議軟件狀態轉移用switch/case? 不轉昏始做蛹者(別和我說是你)的頭, 也搞死維護的兄弟. get

可是,可是, 世事無絕對, switch簡單啊. 好象,我說好象,無根據,別亂猜, 有一個業界有很頗有名的公司是這樣寫sctp協議棧的,不可說,不可說. it

 

2. 狀態轉移表table

先整個表看看,好很差, bug多多, 多多指正.cli

//---------------SCTP message proc table ---------------------------------------------------------------
static fsm_handler_proc sctp_closed_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_CLOSED},      /*(0) 8.4, ootb data, abort */
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_CLOSED},      /*(1)Init */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_CLOSED},      /*(2) Init-Ack, 5.2.3 */
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_CLOSED},      /*(3) 8.4, ootb Sack*/
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_CLOSED},      /*(4) 8.4, ootb heartbeat*/
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_CLOSED},      /*(5) 8.4, ootb heartbeat-ack*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_CLOSED},      /*(6) Discard  abort */
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_CLOSED},      /*(7) Shutdown */
    {(fsm_handler_fn)&sctp_send_shutdown_complete,    SCTP_STATE_CLOSED},      /*(8) Discard  shutdown-ack */
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_CLOSED},      /*(9) Error*/
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED}, /*(10) Cookie-Echo, 5.1.5 */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_CLOSED},      /*(11) 5.2.5, discard cookie-ack */ 
    {(fsm_handler_fn)NULL,                            SCTP_STATE_CLOSED},      /*(12) */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_CLOSED},      /*(13) */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_CLOSED}       /*(14) shutdown complete */
};sed

static fsm_handler_proc sctp_cookie_wait_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_COOKIE_WAIT},   /*(0) 8.4, ootb data, abort */
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_COOKIE_WAIT},   /*(1) 5.2.1, init-ack w/same para*/
    {(fsm_handler_fn)&sctp_accept_init_ack,           SCTP_STATE_COOKIE_ECHOED}, /*(2) Normal client, Init-Ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT},   /*(3) SACK, page 73,discard */
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_COOKIE_WAIT},   /*(4) 8.4, ootb heartbeat*/
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_COOKIE_WAIT},   /*(5) 8.4, ootb heartbeat-ack*/
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},        /*(6) Abort */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT},   /*(7) 9.2, shutdown, discard*/
    {(fsm_handler_fn)&sctp_send_shutdown_complete,    SCTP_STATE_COOKIE_WAIT},   /*(8) 9.2, shutdown-ack*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT},   /*(9) Error, */
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED},   /*(10) Cookie-Echo, 5.1.5 */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT},   /*(11) 5.2.5, cookie-ack, discard*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT},   /*12*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT},   /*13*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_WAIT}    /*(14) shutdown complete */
};軟件

static fsm_handler_proc sctp_cookie_echoed_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_COOKIE_ECHOED}, /*(0) 8.4, ootb data, respond abort */
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_COOKIE_ECHOED}, /*(1) 5.2.1, check new addr & respond */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}, /*(2) 5.2.3, Init-Ack*/
    {(fsm_handler_fn)&sctp_accept_sack,               SCTP_STATE_COOKIE_ECHOED}, /*(3) SACK, common proc ? */
    {(fsm_handler_fn)&sctp_send_heartbeat_ack,        SCTP_STATE_COOKIE_ECHOED}, /*(4) heartbeat 8.3*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}, /*(5) Heartbeat-ack */
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},        /*(6) Abort */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}, /*(7) 9.2, shutdown, discard */
    {(fsm_handler_fn)&sctp_send_shutdown_complete,    SCTP_STATE_COOKIE_ECHOED}, /*(8) 9.2, shutdown-ack*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}, /*(9) Error */
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED},   /*(10) Rcv cookie-echo ? */
    {(fsm_handler_fn)&sctp_accept_cookie_ack,         SCTP_STATE_ESTABLISHED},   /*(11) Rcv Cookie-Ack, 5.2.5 */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}, /*12*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}, /*13*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_COOKIE_ECHOED}  /*(14) shutdown complete */
};

static fsm_handler_proc sctp_established_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_accept_data,               SCTP_STATE_ESTABLISHED},   /*(0) Normal */
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_ESTABLISHED},   /*(1) Handle restart init */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(2) 5.2.3, Init-Ack*/
    {(fsm_handler_fn)&sctp_accept_sack,               SCTP_STATE_ESTABLISHED},   /*(3) SACK, common proc */
    {(fsm_handler_fn)&sctp_send_heartbeat_ack,        SCTP_STATE_ESTABLISHED},   /*(4) 8.3, Heartbeat*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(5) Heartbeat-ack */
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},        /*(6) Abort*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD}, /*(7) shutdown, transfer state */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(8) shutdown-ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(9) Error */
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED},   /*(10) cookie-echo*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(11) 5.2.5,cookie-ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(12) not supported */      
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED},   /*(13) not supported */ 
    {(fsm_handler_fn)NULL,                            SCTP_STATE_ESTABLISHED}    /*(14) shutdown complete */
};

static fsm_handler_proc sctp_shutdown_pending_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_accept_data,               SCTP_STATE_SHUTDOWN_PENDING}, /*(0) Normal */
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_SHUTDOWN_PENDING}, /*(1) Handle restart init */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(2) 5.2.3, Init-Ack*/
    {(fsm_handler_fn)&sctp_accept_sack,               SCTP_STATE_SHUTDOWN_PENDING}, /*(3) SACK, common proc */
    {(fsm_handler_fn)&sctp_send_heartbeat_ack,        SCTP_STATE_SHUTDOWN_PENDING}, /*(4) heartbeat 8.3*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(5) Heartbeat-ack */
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},           /*(6) Abort*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(7) Shutdown */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(8) Normal, shutdown-ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(9) Error */
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED},      /*(10) cookie-echo*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(11) 5.2.5,cookie-ack */  
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(12) not supported */      
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}, /*(13) not supported */      
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_PENDING}  /*(14) shutdown complete */
};

static fsm_handler_proc sctp_shutdown_sent_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_accept_data,               SCTP_STATE_SHUTDOWN_SENT},  /*(0) Should not get data*/
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_SHUTDOWN_SENT},  /*(1) Handle restart init */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},  /*(2) 5.2.3, Init-Ack*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},  /*(3) SACK,? */
    {(fsm_handler_fn)&sctp_send_heartbeat_ack,        SCTP_STATE_SHUTDOWN_SENT},  /*(4) Heartbeat 8.3*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},  /*(5) Heartbeat-ack */
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},         /*(6) Abort*/
    {(fsm_handler_fn)&sctp_send_shutdown_ack,         SCTP_STATE_SHUTDOWN_ACK_SENT},/*(7) Rcv shutdown */
    {(fsm_handler_fn)&sctp_send_shutdown_complete,    SCTP_STATE_SHUTDOWN_SENT},   /*(8) Shutdown ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},   /*(9) do nothing for now */
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED},     /*(10) cookie-echo*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},   /*(11) 5.2.5, discard cookie-ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},   /*12*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT},   /*13*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_SENT}    /*(14)shutdown complete */
};

static fsm_handler_proc sctp_shutdown_rcvd_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_SHUTDOWN_RCVD},    /*(0) Should not get data*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(1) Init */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(2) 5.2.3, Init-Ack*/
    {(fsm_handler_fn)&sctp_accept_sack,               SCTP_STATE_SHUTDOWN_RCVD},    /*(3) SACK, common proc */
    {(fsm_handler_fn)&sctp_send_heartbeat_ack,        SCTP_STATE_SHUTDOWN_RCVD},    /*(4) heartbeat 8.3*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(5) Heartbeat-ack */
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},           /*(6) Abort*/
    {(fsm_handler_fn)&sctp_accept_shutdown,           SCTP_STATE_SHUTDOWN_ACK_SENT},/*(7) 9.2 discard,but we use it*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(8) Shutdown ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(9) do nothing for now */
    {(fsm_handler_fn)&sctp_accept_cookie_echo,        SCTP_STATE_ESTABLISHED},    /*(10) cookie-echo*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*(11) 5.2.5, discard cookie-ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*12*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD},    /*13*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_RCVD}     /*(14)shutdown complete */
};

static fsm_handler_proc sctp_shutdown_ack_sent_msg_proc[SCTP_MSG_MAX] = {
    {(fsm_handler_fn)&sctp_send_abort,                SCTP_STATE_SHUTDOWN_ACK_SENT},/*(0) Should not get data*/
    {(fsm_handler_fn)&sctp_accept_init,               SCTP_STATE_SHUTDOWN_ACK_SENT},/*(1) 5.2.4 */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*(2) 5.2.3, Init-Ack*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*(3) SACK, page73 */
    {(fsm_handler_fn)&sctp_send_heartbeat_ack,        SCTP_STATE_SHUTDOWN_ACK_SENT},/*(4) heartbeat 8.3*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*(5) Heartbeat-ack */
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED},           /*(6) Abort*/
    {(fsm_handler_fn)sctp_accept_shutdown,            SCTP_STATE_CLOSED},           /*(7) 9.2,shutdown-ack, stop T2 */
    {(fsm_handler_fn)&sctp_send_shutdown_complete,    SCTP_STATE_SHUTDOWN_ACK_SENT},/*(8) Shutdown ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*(9) do nothing for now */
    {(fsm_handler_fn)&sctp_shutack_sent_cookie_echo_proc, SCTP_STATE_SHUTDOWN_ACK_SENT},/*(10) cookie-echo*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*(11) 5.2.5, discard cookie-ack */
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*12*/
    {(fsm_handler_fn)NULL,                            SCTP_STATE_SHUTDOWN_ACK_SENT},/*13*/
    {(fsm_handler_fn)&sctp_destroy_assoc,             SCTP_STATE_CLOSED}            /*(14) Normal*/
};

static fsm_handler_proc* g_sctp_fsm[SCTP_STATE_MAX] = {
    (fsm_handler_proc*)&sctp_closed_msg_proc,
    (fsm_handler_proc*)&sctp_cookie_wait_msg_proc,
    (fsm_handler_proc*)&sctp_cookie_echoed_msg_proc,
    (fsm_handler_proc*)&sctp_established_msg_proc,
    (fsm_handler_proc*)&sctp_shutdown_pending_msg_proc,
    (fsm_handler_proc*)&sctp_shutdown_sent_msg_proc,
    (fsm_handler_proc*)&sctp_shutdown_rcvd_msg_proc,
    (fsm_handler_proc*)&sctp_shutdown_ack_sent_msg_proc
};

 

----下次再寫, 要睡覺了 , BTW, 有人知道是否是,不能編輯舊的,只能新發啊

相關文章
相關標籤/搜索