#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
{
sip_uri from; /* From header */
sip_uri to; /* To header */
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 */
U32 cseq; /* CSeq number */
int timeout_idx;
struct sip_message *request; /* Original request */
struct sip_message *last_response; /* Last response */
sip_transaction_state state;
} sip_transaction;
/* 增强的对话状态 */
typedef enum dialog_state {
DIALOG_EARLY, /* 早期对话(收到1xx) */
DIALOG_CONFIRMED /* 已确认对话(收到2xx) */
} dialog_state;
/* 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_uri remote_uri;
sip_uri local_uri;
} 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 */
#ifndef _SIP_CLIENT_H
#define _SIP_CLIENT_H
#include "common/sip_common.h"
#include "sip_protocol.h"
#include "media.h"
#include "rtmp_sys.h"
#include "sdp.h"
#define MD5_APPEND(md5, buf, len) tpssl_Md5Update(md5, (uint8_t *)buf, (unsigned)len)
#define REGISTER_INTERVAL 30
#define CALL_TIMEOUT 30
#define ESTABLISHED_TIMEOUT 120
#define TERMINATE_TIMEOUT 2
#define MAX_REGISTER_RETRY 10
#define TCP_RECONNECT_INTERVAL 10
/* 限制配置内容不超过对应最大长度 */
#define SIP_MAX_USERNAME 32
#define SIP_MAX_PASSWORD 32
#define SIP_MAX_ADDR_LEN 64
/* SIP客户端状态 */
typedef enum sip_client_state
{
SIP_STATE_IDLE = 0,
SIP_STATE_INVITING,
SIP_STATE_RINGING,
SIP_STATE_ESTABLISHED,
SIP_STATE_ERROR,
SIP_STATE_MAX
} sip_client_state;
/* 客户端事件类型 */
typedef enum sip_event_type
{
/* 本地控制事件 */
EVENT_LOCAL_CALL = 0, // 发起呼叫
EVENT_LOCAL_ANSWER, // 接听呼叫
EVENT_LOCAL_HANGUP, // 挂断通话
EVENT_LOCAL_TIMEOUT, // 事务超时
/* 网络请求事件 */
EVENT_NET_INVITE, // 收到INVITE请求
EVENT_NET_ACK, // 收到ACK请求
EVENT_NET_BYE, // 收到BYE请求
EVENT_NET_CANCEL, // 收到CANCEL请求
/* 核心响应事件 */
EVENT_NET_1XX_PROVISIONAL, // 所有1xx临时响应
EVENT_NET_2XX_SUCCESS, // 200类成功响应
EVENT_NET_3XX_REDIRECTION, // 300类重定向响应
EVENT_NET_4XX_CLIENT_ERROR, // 400类客户端错误
EVENT_NET_5XX_SERVER_ERROR, // 500类服务器错误
/* 关键特殊事件 */
EVENT_NET_180_RINGING, // 180振铃提示
EVENT_NET_AUTH_CHALLENGE, // 认证挑战(401/407)
/* 系统级事件 */
EVENT_ERROR, // 传输错误
EVENT_MAX // 事件类型总数
} sip_event_type;
/* 回调函数类型 */
typedef int (*sip_callback_t)(sip_event_type event, void *sip_msg);
/* 客户端配置 */
typedef struct sip_client_config_t
{
BOOL usetsl;
char server_addr[SIP_MAX_ADDR_LEN];
char username[SIP_MAX_USERNAME];
char password[SIP_MAX_PASSWORD];
char local_addr[SIP_MAX_ADDR_LEN];
U16 server_port;
} sip_client_config; // 200B
/* SIP客户端结构 */
typedef struct SIPCLIENT_S
{
/* 当前事务 */
sip_dialog *current_dialog;
sip_transaction *current_trans;
sip_message cur_register;
auth_info_t auth;
enum sip_client_state state;
sip_client_config config;
int state_timeout_timer_idx;
int registered_expired_timer_idx;
int response_timeout_timer_idx;
int register_timer_idx;
int tcp_reconnect_timer_idx;
/* 网络连接 */
int isock;
S32 sip_socket;
TPSIPSESSION *media_session; // 200B
BOOL registered;
BOOL connected;
sdp_t *sdp_net;
sdp_t *sdp_local;
U32 call_count;
char from_tag[32];
} SIPCLIENT; // 1,956
/* SIP客户端接口 */
/**
* @brief SIP客户端初始化
*
* @param[in] config 客户端配置
*
* @return 错误码
*
*/
int sip_client_init(sip_client_config config);
int sip_client_deinit();
/**
* @brief SIP客户端拨号
*
* @param[in] number 目标号码
*
* @return 错误码
*
* @note 只有在空闲状态下才能发出拨号,其余状态不做处理
*/
int sip_client_make_call(const char *number);
/**
* @brief SIP客户端接听来电
*
* @return 错误码
*
* @note 只有在振铃状态下才会成功接听来电,其余状态不做处理
*/
int sip_client_answer_call();
/**
* @brief SIP客户端挂断电话
*
* @return 错误码
*
* @note 只有在非空闲状态下才能挂断电话,空闲状态不做处理
*/
int sip_client_hangup_call();
/**
* @brief 获取当前SIP客户端结构体指针
*
* @return SIPCLIENT:SIP客户端结构体指针
*
*/
SIPCLIENT *get_sip_client();
/**
* @brief 获取当前SIP客户端配置结构体指针
*
* @return SIPCLIENT:SIP客户端配置结构体指针
*
*/
sip_client_config *get_sip_config_client();
void sip_client_set_idle_cb(sip_callback_t cb);
void sip_client_set_inviting_cb(sip_callback_t cb);
void sip_client_set_ringing_cb(sip_callback_t cb);
void sip_client_set_established_cb(sip_callback_t cb);
#endif /* _SIP_CLIENT_H */
帮我想一下怎么修改设计来使这个sip客户端设计更加合理,能够同时支持多个对话
最新发布