⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sip_message.c

📁 linux下的sip voip程序
💻 C
字号:
#include"sip_util.h"#include"sip_message.h"extern SAVE_MSG     g_SipMsg;extern STACK_INFO   g_StackInfo;/* SIPメッセ〖ジを浮瑚しメッセ〖ジのメソッド急侍戎规を手す */enum method Search_Method(char* msg){    char line[MAX_FIELD];    memset(line, 0, MAX_FIELD);    get_Startline(msg, line);    if(strstr(line, METHOD_INVITE)){             /* INVITE */        return Method_Invite;    }else if(strstr(line, METHOD_TRYING)){       /* 100 Trying */        return Method_Trying;    }else if(strstr(line, METHOD_RINGING)){      /* 180 Ringing */        return Method_Ringing;    }else if(strstr(line, METHOD_OK)){           /* 200 OK */        return Method_OK;    }else if(strstr(line, METHOD_ACK)){          /* ACK */        return Method_Ack;    }else if(strstr(line, METHOD_BYE)){          /* BYE */        return Method_Bye;    }else if(strstr(line, METHOD_CANCEL)){       /* CANCEL */        return Method_Cancel;    }else if(strstr(line, METHOD_BUSY)){         /* 486 Busy Here */        return Method_Busy;    }else if(strstr(line, METHOD_TERMINATE)){    /* 487 Request Terminate */        return Method_RequestTerminate;    }    return -1;}/* SIPメッセ〖ジのスタ〖トラインを侯喇する */int set_Startline(enum method emethod, char* msg, char* username){    struct hostent* phost;    char hostname[MAX_NAME];    gethostname(hostname,256);    phost = gethostbyname(hostname);    switch(emethod){        case Method_Register:                    /* REGISTER */            sprintf(msg, "%s sip:%s %s\r\n",                    METHOD_REGISTER, username, SIP_VERSION);            break;        case Method_Invite:                      /* INVITE */            sprintf(msg, "%s sip:%s %s\r\n",                    METHOD_INVITE, username, SIP_VERSION);            break;        case Method_Ack:                         /* ACK */            sprintf(msg, "%s sip:%s %s\r\n" ,                    METHOD_ACK, username, SIP_VERSION);            break;        case Method_Bye:                         /* BYE */            sprintf(msg, "%s sip:%s %s\r\n" ,                         METHOD_BYE, username, SIP_VERSION);            break;        case Method_Cancel:                      /* CANCEL */            sprintf(msg, "%s sip:%s %s\r\n" ,                    METHOD_CANCEL, username, SIP_VERSION);            break;        case Method_Trying:                      /* 100 Trying */            sprintf(msg, "%s %s\r\n", SIP_VERSION, METHOD_TRYING);            break;        case Method_Ringing:                     /* 180 Ringing */            sprintf(msg, "%s %s\r\n", SIP_VERSION, METHOD_RINGING);            break;        case Method_OK:                          /* 200 OK */            sprintf(msg, "%s %s\r\n", SIP_VERSION, METHOD_OK);            break;        case Method_Busy:                        /* 486 Busy Here */            sprintf(msg, "%s %s\r\n", SIP_VERSION, METHOD_BUSY);            break;        case Method_RequestTerminate:            /* 487 Request Terminate */            sprintf(msg, "%s %s\r\n", SIP_VERSION, METHOD_TERMINATE);            break;        default:                                 /* 澈碰急侍戎规なし */            debug("Error!!Choice method undefined.\n");            return -1;    }    return 0;}/* Viaヘッダ〖を侯喇する */int set_Via(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_Via failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%s\r\n", HEADER_VIA, buff);    return 0;}/* Max-Forwardsヘッダ〖を侯喇する */int set_Maxforwards(char* msg, int count){    if(msg == NULL){        debug("set_Maxforwards failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%d\r\n", HEADER_MAXFORWARDS, count);    return 0;}/* Fromヘッダ〖を侯喇する */int set_From(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_From failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%s\r\n", HEADER_FROM, buff);    return 0;}/* Toヘッダ〖を侯喇する */int set_To(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_To failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%s\r\n", HEADER_TO, buff);    return 0;}/* Call-IDヘッダ〖を侯喇する */int set_CallID(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_CallID failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%s\r\n", HEADER_CALLID, buff);    return 0;}/* CSeqヘッダ〖を侯喇する */int set_CSeq(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_CSeq failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%s\r\n", HEADER_CSEQ, buff);    return 0;}/* Contactヘッダ〖を侯喇する */int set_ContactA(char* msg, char* buff, char* username){    if(msg == NULL|| buff ==NULL){        debug("set_Contact failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s<sip:%s@%s>\r\n", HEADER_CONTACT,         username, buff);    return 0;}/* Contactヘッダ〖を侯喇する */int set_Contact(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_Contact failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s<sip:%s>\r\n", HEADER_CONTACT, buff);    return 0;}/* Content-Typeヘッダ〖を侯喇する */int set_Contenttype(char* msg, char* buff){    if(msg == NULL|| buff ==NULL){        debug("set_Contenttype failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%s\r\n", HEADER_CONTENTTYPE, buff);    return 0;}/* Content-Lengthヘッダ〖を侯喇する */int set_Contentlength(char* msg, int len){    if(msg == NULL){        debug("set_Contentlength failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%d\r\n\r\n", HEADER_CONTENTLENGTH, len);    return 0;}/* Expiresヘッダ〖を侯喇する */int set_Expires(char* msg, int expires){    if(msg == NULL){        debug("set_Expires failed.\n");        return -1;    }    sprintf((msg+strlen(msg)), "%s%d\r\n", HEADER_EXPIRES, expires);    return 0;}/* SIPメッセ〖ジからスタ〖トラインの攫鼠を掐缄する */void get_Startline(char* msg, char* buff){    char* ep;    if(msg == NULL){        debug("get_Startline failed.\n");        return;    }    ep = strstr(msg, "\r\n");      //在msg中查找回车符,返回其位置    strncpy(buff, msg, (strlen(msg) - strlen(ep)));    buff[strlen(msg) - strlen(ep)] = '\0';	                                                    //因为数组下标从0开始,所以如果长度为20                                                          //在buff[20]正好是,拷贝信息的下一个字符                                                          //意思为在刚才取得的startline+messageheader后                                                          //添加一个字符串结束字符'\0'}/* SIPメッセ〖ジからViaヘッダ〖の攫鼠を掐缄する */void get_Via2nd(char* msg, char* buff){    char* sp;    char* ep;    if(msg == NULL){        debug("get_Via failed.\n");        return;    }    sp = strstr(msg,HEADER_VIA);    sp = strstr(sp+1,HEADER_VIA);/* 2nd */    ep = strstr(sp, "\r\n");    strncpy(buff, (sp+strlen(HEADER_VIA)), (strlen(sp+strlen(HEADER_VIA))-strlen(ep)));    buff[strlen(sp+strlen(HEADER_VIA))-strlen(ep)] = '\0';}/* SIPメッセ〖ジからViaヘッダ〖の攫鼠を掐缄する */void get_Via(char* msg, char* buff){    char* sp;    char* ep;    if(msg == NULL){        debug("get_Via failed.\n");        return;    }    sp = strstr(msg,HEADER_VIA);    ep = strstr(sp, "\r\n");    strncpy(buff, (sp+strlen(HEADER_VIA)), (strlen(sp+strlen(HEADER_VIA))-strlen(ep)));    buff[strlen(sp+strlen(HEADER_VIA))-strlen(ep)] = '\0';}/* SIPメッセ〖ジからFromヘッダ〖の攫鼠を掐缄する */void get_From(char* msg, char* buff){    char* sp;    char* ep;    if(msg == NULL){        debug("get_From failed.\n");        return;    }    sp = strstr(msg,HEADER_FROM);    ep = strstr(sp, "\r\n");    strncpy(buff, (sp+strlen(HEADER_FROM)), (strlen(sp+strlen(HEADER_FROM))-strlen(ep)));    buff[strlen(sp+strlen(HEADER_FROM))-strlen(ep)] = '\0';}/* SIPメッセ〖ジからToヘッダ〖の攫鼠を掐缄する */void get_To(char* msg, char* buff){    char* sp;    char* ep;    if(msg == NULL){        debug("get_To failed.\n");        return;    }    sp = strstr(msg, HEADER_TO);    ep = strstr(sp, "\r\n");    strncpy(buff, (sp+strlen(HEADER_TO)), (strlen(sp+strlen(HEADER_TO))-strlen(ep)));    buff[strlen(sp+strlen(HEADER_TO))-strlen(ep)] = '\0';}/* SIPメッセ〖ジからCall-IDヘッダ〖の攫鼠を掐缄する */void get_CallID(char* msg, char* buff){    char* sp;    char* ep;    if(msg == NULL){        debug("get_CallID failed.\n");        return;    }    sp = strstr(msg,HEADER_CALLID);    ep = strstr(sp, "\r\n");    strncpy(buff, (sp+strlen(HEADER_CALLID)), (strlen(sp+strlen(HEADER_CALLID))-strlen(ep)));    buff[strlen(sp+strlen(HEADER_CALLID))-strlen(ep)] = '\0';}/* SIPメッセ〖ジからCSeqヘッダ〖の攫鼠を掐缄する */void get_CSeq(char* msg, char* buff){    char* sp;    char* ep;    if(msg == NULL){        debug("get_CSeq failed.\n");        return;    }    sp = strstr(msg, HEADER_CSEQ);    ep = strstr(sp, "\r\n");    strncpy(buff, (sp+strlen(HEADER_CSEQ)), (strlen(sp+strlen(HEADER_CSEQ))-strlen(ep)));    buff[strlen(sp+strlen(HEADER_CSEQ))-strlen(ep)] = '\0';}/* CSeqヘッダ〖の猛からシ〖ケンス戎规を掐缄する */int get_CSeqnum(char* cseq){    int cseqnum;    if(cseq == NULL){        debug("get_CSeqnum failed.\n");        return -1;    }    cseqnum = atoi(cseq);    return cseqnum;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -