SIP ACK Req_URI

本文探讨了SIP协议中ACK与RE-INVITE请求的处理方式,详细解释了如何根据200 OK响应中的Contact-header确定Req_URI,以及在存在Record-Route时如何使用Route-header。

ACK 的Req_URI是根据200 OKcontact-header来填的。如果200 OK带了record-route, ACK就会带Route header

如果要发的消息里面没有带route header,这个消息就会根据Req_URI来发,否则就根据route header来发


对于re-invite的Req-URI,也应该是根据200 OK的contact来的,他的规则应该和ACK的规则是一样的。

#ifndef _SIP_PROTOCOL_H #define _SIP_PROTOCOL_H #include "common/sip_common.h" /* SIP方法定义 */ typedef enum sip_method { SIP_METHOD_INVITE = 0, SIP_METHOD_ACK, SIP_METHOD_BYE, SIP_METHOD_CANCEL, SIP_METHOD_REGISTER, SIP_METHOD_OPTIONS, SIP_METHOD_MAX }sip_method; /* SIP响应状态码 */ /* SIP响应状态码枚举 (RFC 3261及相关扩展) */ typedef enum sip_status_code { /* 1xx 临时响应 */ SIP_100_TRYING = 100, SIP_180_RINGING = 180, SIP_181_CALL_IS_BEING_FORWARDED = 181, SIP_182_QUEUED = 182, SIP_183_SESSION_PROGRESS = 183, /* 2xx 成功响应 */ SIP_200_OK = 200, SIP_202_ACCEPTED = 202, // RFC3265 /* 3xx 重定向响应 */ SIP_300_MULTIPLE_CHOICES = 300, SIP_301_MOVED_PERMANENTLY = 301, SIP_302_MOVED_TEMPORARILY = 302, SIP_305_USE_PROXY = 305, SIP_380_ALTERNATIVE_SERVICE = 380, /* 4xx 客户端错误 */ SIP_400_BAD_REQUEST = 400, SIP_401_UNAUTHORIZED = 401, SIP_402_PAYMENT_REQUIRED = 402, SIP_403_FORBIDDEN = 403, SIP_404_NOT_FOUND = 404, SIP_405_METHOD_NOT_ALLOWED = 405, SIP_406_NOT_ACCEPTABLE = 406, SIP_407_PROXY_AUTHENTICATION_REQUIRED = 407, SIP_408_REQUEST_TIMEOUT = 408, SIP_410_GONE = 410, SIP_413_REQUEST_ENTITY_TOO_LARGE = 413, SIP_414_REQUEST_URI_TOO_LONG = 414, SIP_415_UNSUPPORTED_MEDIA_TYPE = 415, SIP_416_UNSUPPORTED_URI_SCHEME = 416, SIP_420_BAD_EXTENSION = 420, SIP_421_EXTENSION_REQUIRED = 421, SIP_423_INTERVAL_TOO_BRIEF = 423, SIP_480_TEMPORARILY_UNAVAILABLE = 480, SIP_481_CALL_TRANSACTION_DOES_NOT_EXIST = 481, SIP_482_LOOP_DETECTED = 482, SIP_483_TOO_MANY_HOPS = 483, SIP_484_ADDRESS_INCOMPLETE = 484, SIP_485_AMBIGUOUS = 485, SIP_486_BUSY_HERE = 486, SIP_487_REQUEST_TERMINATED = 487, SIP_488_NOT_ACCEPTABLE_HERE = 488, SIP_489_BAD_EVENT = 489, // RFC3265 SIP_491_REQUEST_PENDING = 491, SIP_493_UNDECIPHERABLE = 493, /* 5xx 服务器错误 */ SIP_500_SERVER_INTERNAL_ERROR = 500, SIP_501_NOT_IMPLEMENTED = 501, SIP_502_BAD_GATEWAY = 502, SIP_503_SERVICE_UNAVAILABLE = 503, SIP_504_SERVER_TIME_OUT = 504, SIP_505_VERSION_NOT_SUPPORTED = 505, SIP_513_MESSAGE_TOO_LARGE = 513, /* 6xx 全局错误 */ SIP_600_BUSY_EVERYWHERE = 600, SIP_603_DECLINE = 603, SIP_604_DOES_NOT_EXIST_ANYWHERE = 604, SIP_606_NOT_ACCEPTABLE = 606 } sip_status_code; /* SIP URI结构 */ typedef struct sip_uri { char scheme[8]; /* "sip" or "sips" */ char user[32]; /* username */ char host[32]; /* domain or IP */ U16 port; /* port number */ char parameters[32]; /* URI parameters */ }sip_uri; /* SIP Via头结构 */ typedef struct sip_via { char protocol[16]; /* "SIP/2.0" */ char transport[8]; /* "UDP", "TCP" */ char host[64]; /* sent-by host */ U16 port; /* sent-by port */ char branch[32]; /* branch parameter */ char received[32]; /* received parameter */ U16 rport; /* rport parameter */ }sip_via; /* 认证信息结构体 */ typedef struct auth_info_t{ char realm[128]; char nonce[128]; char algorithm[32]; char qop[32]; char opaque[128]; int stale; char response[33]; } auth_info_t; /* SIP消息头结构 */ typedef struct sip_headers { struct sip_uri from; /* From header */ struct sip_uri to; /* To header */ struct sip_uri request_uri; /* Request-URI */ struct sip_via via; /* Via header */ char call_id[64]; /* Call-ID header */ U32 cseq; /* CSeq number */ enum sip_method cseq_method;/* CSeq method */ U8 max_forwards; /* Max-Forwards header */ char content_type[64]; /* Content-Type header */ char contact[64]; U32 content_length; /* Content-Length header */ U32 Expires; char user_agent[64]; /* User-Agent header */ auth_info_t auth; }sip_headers; /* SIP消息体结构 */ typedef struct sip_body { char *content; /* Message body content */ U32 length; /* Message body length */ char type[32]; /* Content type */ }sip_body; /* SIP消息结构 */ typedef struct sip_message { U8 type; /* 0: request, 1: response */ enum sip_method method;/* Method (for requests) */ U16 status_code; /* Status code (for responses) */ char reason_phrase[32];/* Reason phrase (for responses) */ struct sip_headers headers; /* SIP headers */ struct sip_body body; /* Message body */ char *raw_data; /* Raw message data */ U32 raw_length; /* Raw message length */ struct sockaddr_in source; /* Message source */ struct list_head list; /* List head for message queue */ }sip_message; /* SIP事务信息 */ typedef struct sip_transaction { char branch[32]; /* Transaction branch */ enum sip_method method; /* Transaction method */ U32 timeout; /* Transaction timeout */ unsigned long start_time; /* Start time in jiffies */ struct sip_message request; /* Original request */ struct sip_message last_response; /* Last response */ void *user_data; /* User-specific data */ U8 state; /*0,off,1 on*/ }sip_transaction; /* 协议栈配置 */ typedef struct sip_protocol_config { char user_agent[32]; U8 max_forwards; U32 t1_timeout; /* T1 timer (RTT estimate) */ U32 t2_timeout; /* T2 timer (64*T1) */ U32 t4_timeout; /* T4 timer (5000ms) */ }sip_protocol_config; /* SIP协议栈接口 */ int sip_message_parse(sip_message *msg, const char *data, U32 length); int sip_message_build(sip_message *msg, char *buffer, U32 size); void sip_message_free(sip_message *msg); // int sip_send_message(S32 sip_socket, struct sip_message *msg,const char *dest_addr, U16 dest_port); int sip_uri_parse(struct sip_uri *uri, const char *uri_str); int sip_uri_build(struct sip_uri *uri, char *buffer, U32 size); struct sip_transaction *sip_transaction_create(struct sip_message *ori_req); void sip_transaction_free(struct sip_transaction *trans); const char *sip_method_to_string(enum sip_method method); sip_method sip_string_to_method(const char *method_str); const char *sip_status_to_reason(U16 status_code); void sip_generate_branch(char *branch, U32 size); void sip_generate_tag(char *tag, U32 size); void sip_generate_call_id(char *buf, int len); #endif /* _SIP_PROTOCOL_H */ 为这段程序所有字符数组最大长度改为宏定义的最大长度
10-28
#ifndef _SIP_PROTOCOL_H #define _SIP_PROTOCOL_H #include "common/sip_common.h" #ifdef SIP_USE_TCP #define SIP_TRANSPORT "TCP" #else #define SIP_TRANSPORT "UDP" #endif /* 字符数组长度宏定义 */ #define SIP_URI_SCHEME_LEN 8 #define SIP_URI_USER_LEN 32 #define SIP_URI_HOST_LEN 32 #define SIP_URI_PARAM_LEN 32 #define SIP_VIA_PROTOCOL_LEN 16 #define SIP_VIA_TRANSPORT_LEN 8 #define SIP_VIA_HOST_LEN 64 #define SIP_VIA_BRANCH_LEN 32 #define SIP_VIA_RECEIVED_LEN 32 #define AUTH_REALM_LEN 64 #define AUTH_NONCE_LEN 64 #define AUTH_ALGORITHM_LEN 32 #define AUTH_QOP_LEN 32 #define AUTH_OPAQUE_LEN 64 #define AUTH_RESPONSE_LEN 33 #define SIP_HEADER_CALLID_LEN 64 #define SIP_HEADER_CONTENT_TYPE_LEN 64 #define SIP_HEADER_CONTACT_LEN 64 #define SIP_HEADER_USER_AGENT_LEN 64 #define SIP_BODY_TYPE_LEN 32 #define SIP_REASON_PHRASE_LEN 32 #define SIP_CONFIG_USER_AGENT_LEN 32 #define SIP_BRANCH_LEN 32 #define SIP_TAG_LEN 32 #define SIP_CALLID_LEN 64 #define T1_TIMEOUT 500 #define T2_TIMEOUT 64 * T1_TIMEOUT #define T4_TIMEOUT 5000 /* SIP方法定义 */ typedef enum sip_method { SIP_METHOD_INVITE = 0, SIP_METHOD_ACK, SIP_METHOD_BYE, SIP_METHOD_CANCEL, SIP_METHOD_REGISTER, SIP_METHOD_OPTIONS, SIP_METHOD_MAX } sip_method; /* SIP响应状态码 */ /* SIP响应状态码枚举 (RFC 3261及相关扩展) */ typedef enum sip_status_code { /* 1xx 临时响应 */ SIP_100_TRYING = 100, SIP_180_RINGING = 180, SIP_181_CALL_IS_BEING_FORWARDED = 181, SIP_182_QUEUED = 182, SIP_183_SESSION_PROGRESS = 183, /* 2xx 成功响应 */ SIP_200_OK = 200, SIP_202_ACCEPTED = 202, // RFC3265 /* 3xx 重定向响应 */ SIP_300_MULTIPLE_CHOICES = 300, SIP_301_MOVED_PERMANENTLY = 301, SIP_302_MOVED_TEMPORARILY = 302, SIP_305_USE_PROXY = 305, SIP_380_ALTERNATIVE_SERVICE = 380, /* 4xx 客户端错误 */ SIP_400_BAD_REQUEST = 400, SIP_401_UNAUTHORIZED = 401, SIP_402_PAYMENT_REQUIRED = 402, SIP_403_FORBIDDEN = 403, SIP_404_NOT_FOUND = 404, SIP_405_METHOD_NOT_ALLOWED = 405, SIP_406_NOT_ACCEPTABLE = 406, SIP_407_PROXY_AUTHENTICATION_REQUIRED = 407, SIP_408_REQUEST_TIMEOUT = 408, SIP_410_GONE = 410, SIP_413_REQUEST_ENTITY_TOO_LARGE = 413, SIP_414_REQUEST_URI_TOO_LONG = 414, SIP_415_UNSUPPORTED_MEDIA_TYPE = 415, SIP_416_UNSUPPORTED_URI_SCHEME = 416, SIP_420_BAD_EXTENSION = 420, SIP_421_EXTENSION_REQUIRED = 421, SIP_423_INTERVAL_TOO_BRIEF = 423, SIP_480_TEMPORARILY_UNAVAILABLE = 480, SIP_481_CALL_TRANSACTION_DOES_NOT_EXIST = 481, SIP_482_LOOP_DETECTED = 482, SIP_483_TOO_MANY_HOPS = 483, SIP_484_ADDRESS_INCOMPLETE = 484, SIP_485_AMBIGUOUS = 485, SIP_486_BUSY_HERE = 486, SIP_487_REQUEST_TERMINATED = 487, SIP_488_NOT_ACCEPTABLE_HERE = 488, SIP_489_BAD_EVENT = 489, // RFC3265 SIP_491_REQUEST_PENDING = 491, SIP_493_UNDECIPHERABLE = 493, /* 5xx 服务器错误 */ SIP_500_SERVER_INTERNAL_ERROR = 500, SIP_501_NOT_IMPLEMENTED = 501, SIP_502_BAD_GATEWAY = 502, SIP_503_SERVICE_UNAVAILABLE = 503, SIP_504_SERVER_TIME_OUT = 504, SIP_505_VERSION_NOT_SUPPORTED = 505, SIP_513_MESSAGE_TOO_LARGE = 513, /* 6xx 全局错误 */ SIP_600_BUSY_EVERYWHERE = 600, SIP_603_DECLINE = 603, SIP_604_DOES_NOT_EXIST_ANYWHERE = 604, SIP_606_NOT_ACCEPTABLE = 606 } sip_status_code; /* SIP事务状态定义 */ typedef enum sip_transaction_state { Calling = 0, Trying, Proceeding, Completed, Terminated } sip_transaction_state; /* SIP URI结构 */ typedef struct sip_uri { char scheme[SIP_URI_SCHEME_LEN]; /* "sip" or "sips" */ char user[SIP_URI_USER_LEN]; /* username */ char host[SIP_URI_HOST_LEN]; /* domain or IP */ U16 port; /* port number */ char parameters[SIP_URI_PARAM_LEN]; /* URI parameters */ } sip_uri; /* SIP Via头结构 */ typedef struct sip_via { char protocol[SIP_VIA_PROTOCOL_LEN]; /* "SIP/2.0" */ char transport[SIP_VIA_TRANSPORT_LEN]; /* "UDP", "TCP" */ char host[SIP_VIA_HOST_LEN]; /* sent-by host */ U16 port; /* sent-by port */ char branch[SIP_VIA_BRANCH_LEN]; /* branch parameter */ char received[SIP_VIA_RECEIVED_LEN]; /* received parameter */ U16 rport; /* rport parameter */ } sip_via; /* 认证信息结构体 */ typedef struct auth_info_t { char realm[AUTH_REALM_LEN]; char nonce[AUTH_NONCE_LEN]; char algorithm[AUTH_ALGORITHM_LEN]; char qop[AUTH_QOP_LEN]; char opaque[AUTH_OPAQUE_LEN]; int stale; char response[AUTH_RESPONSE_LEN]; } auth_info_t; /* SIP消息头结构 */ typedef struct sip_headers { struct sip_uri from; /* From header */ struct sip_uri to; /* To header */ struct sip_via via; /* Via header */ char call_id[SIP_HEADER_CALLID_LEN]; /* Call-ID header */ U32 cseq; /* CSeq number */ enum sip_method cseq_method; /* CSeq method */ U8 max_forwards; /* Max-Forwards header */ char content_type[SIP_HEADER_CONTENT_TYPE_LEN]; /* Content-Type */ char contact[SIP_HEADER_CONTACT_LEN]; /* Contact header */ U32 content_length; /* Content-Length header */ U32 Expires; char user_agent[SIP_HEADER_USER_AGENT_LEN]; /* User-Agent */ auth_info_t auth; } sip_headers; /* SIP消息体结构 */ typedef struct sip_body { char *content; /* Message body content */ U32 length; /* Message body length */ char type[SIP_BODY_TYPE_LEN]; /* Content type */ } sip_body; /* SIP消息结构 */ typedef struct sip_message { U8 type; /* 0:request, 1:response */ enum sip_method method; /* Method (requests) */ U16 status_code; /* Status code (responses) */ char reason_phrase[SIP_REASON_PHRASE_LEN]; /* Reason phrase */ struct sip_uri request_uri; /* Request-URI */ struct sip_headers headers; /* SIP headers */ struct sip_body body; /* Message body */ struct sockaddr_in source; /* Message source */ struct list_head list; /* List head for queue */ } sip_message; /* SIP事务信息 */ typedef struct sip_transaction { char branch[SIP_BRANCH_LEN]; /* Transaction branch */ enum sip_method method; /* Transaction method */ int timeout_idx; struct sip_message *request; /* Original request */ struct sip_message *last_response; /* Last response */ sip_transaction_state state; /*0:off, 1:on*/ } sip_transaction; /* SIP对话信息 */ typedef struct sip_dialog { char remote_tag[SIP_URI_PARAM_LEN]; /* From */ char local_tag[SIP_URI_PARAM_LEN]; /* To */ char call_id[SIP_HEADER_CALLID_LEN]; } sip_dialog; /* 协议栈配置 */ typedef struct sip_protocol_config { char user_agent[SIP_CONFIG_USER_AGENT_LEN]; U8 max_forwards; U32 t1_timeout; /* T1 timer (RTT) */ U32 t2_timeout; /* T2 timer (64*T1) */ U32 t4_timeout; /* T4 timer (5000ms) */ } sip_protocol_config; /* SIP协议栈接口 */ /** * @brief SIP消息解析 * * @param[out] msg 解析出的sip消息 * * @param[in] data sip消息原始数据 * * @param[in] length 数据长度 * * @return 错误码 * */ int sip_message_parse(sip_message *msg, const char *data, U32 length); /** * @brief SIP消息解析 * * @param[in] msg sip消息结构体 * * @param[out] buffer 构造出的sip消息 * * @param[in] size 报文大小 * * @return 错误码 * */ int sip_message_build(sip_message *msg, char *buffer, U32 size); void sip_message_free(sip_message *msg); /** * @brief SIP消息拷贝 * * @param[in] src 源消息指针 * * @return sip消息指针 * */ sip_message *sip_message_copy(sip_message *src); int sip_uri_parse(sip_uri *uri, const char *uri_str); int sip_uri_build(sip_uri *uri, char *buffer, U32 size); /** * @brief 创建SIP事务 * * @param[ori_req] 源请求 * * @return SIP事务指针 * */ sip_transaction *sip_transaction_create(sip_message *ori_req, void *timeout_hanler); void sip_transaction_terminate(sip_transaction *trans); void sip_transaction_free(sip_transaction *trans); sip_dialog *sip_dialog_create(char *to_tag, char *from_tag, char *call_id); void sip_dialog_free(sip_dialog *dialog); const char *sip_method_to_string(enum sip_method method); sip_method sip_string_to_method(const char *method_str); const char *sip_status_to_reason(U16 status_code); void sip_generate_branch(char *branch, U32 size); void sip_generate_tag(char *tag, U32 size); void sip_generate_call_id(char *buf, int len); #endif /* _SIP_PROTOCOL_H */ 帮我改进一下对话和事务的结构体,使逻辑更合理
最新发布
11-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值