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

📄 stk_main.c

📁 linux下的sip voip程序
💻 C
字号:
#include <pthread.h>#include <string.h>#include "sip_util.h"#include "sip_ctrl.h"#include "sip_message.h"#include "sdp.h"#include "stack_if.h"#include "rtp_ctrl.h"#include <pthread.h>SAVE_MSG       g_SipMsg;STACK_INFO     g_StackInfo;enum send_flag g_send_switch = null;int main(){    char usercommand;    pthread_t sipthreadid;    pthread_t r_rtpthreadid;    RECV_INFO* r_info;    RTP_INFO* rtp_info;    pthread_t s_rtpthreadid;    char called[256];    printf("SIP user agent is startup...,\n");    r_info = (RECV_INFO*)malloc(sizeof(RECV_INFO));    rtp_info = (RTP_INFO*)malloc(sizeof(RTP_INFO));    /* 减慨脱スレッド惟ち惧げ */    loadsetting(&g_StackInfo, CONFIG_FILE_NAME);    if(g_StackInfo.server_flag == eRNull){        g_StackInfo.state = eSIdle;    }    r_info->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);    r_info->pmsg = &g_SipMsg;    r_info->pinfo = &g_StackInfo;    r_info->psend_switch = &g_send_switch;    pthread_create(&sipthreadid, NULL, RecvSipMsg, (void*)r_info);    rtp_info->send_sock = (AF_INET,  SOCK_DGRAM, IPPROTO_UDP);    rtp_info->recv_sock = (AF_INET,  SOCK_DGRAM, IPPROTO_UDP);    rtp_info->recv_port = g_StackInfo.rtpport;    rtp_info->rtp_header = (rtp_hdr_t*)malloc(sizeof(rtp_hdr_t));    rtp_info->rtp_header->version = RTP_VERSION;    rtp_info->rtp_header->p = 0;    rtp_info->rtp_header->x= 0;    rtp_info->rtp_header->cc =0;    rtp_info->rtp_header->m = 1;    rtp_info->rtp_header->pt= RTPTYPE;    rtp_info->rtp_header->ssrc = htonl(0x80000000+RANDOM(0x7FFFFFFF));    rtp_info->seqnum = 0x8000+RANDOM(0x7FFF);    rtp_info->timestamp = 0x80000000+RANDOM(0x7FFFFFFF);    rtp_info->psend_switch = &g_send_switch;    pthread_create(&r_rtpthreadid, NULL, Recv_Rtp, (void*)rtp_info);    do{        printf("Input command.    h or H for help\n");        printf(">");        scanf("%s", &usercommand);        if(tolower(usercommand) == 'i'){            printf("Please input called terminal's SIP URI.\n");            printf("SIP URI:>");            scanf("%s", called);        }else if(tolower(usercommand) == 's'){            rtp_info->send_port = atoi(g_StackInfo.y_rtpport);            printf("rtp port =%s\n", g_StackInfo.y_rtpport);            strcpy(rtp_info->send_addr, g_StackInfo.rtp_addr);            printf("rtp addr =%s\n", g_StackInfo.rtp_addr);            if(*(rtp_info->psend_switch) != ok){                *(rtp_info->psend_switch) = ok;                 pthread_create(&s_rtpthreadid, NULL, Rtp_Ctrl, (void*)rtp_info);            }            continue;        }else if(tolower(usercommand) == 'b'){            *(rtp_info->psend_switch) = ng;        }else if(tolower(usercommand) == 'h'){            printf("r,R:Send REGISTER.\n");            printf("i,I:Send INVITE.\n");            printf("u,U:Send UNREGISTER.\n");            printf("e,E:exit SIP user agent .\n");            continue;        }else if(tolower(usercommand) == 'e'){            printf("\nterminate SIP user agent...,\n");            continue;        }        if_main(&usercommand, called);    }while(tolower(usercommand) != 'e');    close(r_info->sock);    close(rtp_info->send_sock);    close(rtp_info->recv_sock);    free(r_info);    free(rtp_info);    debug("done\n");    return 0;}if_main(char* usercommand, char* called){    int ret;    char msg[MAX_MESSAGE];    if(tolower(*usercommand) == 'r'){        if(g_StackInfo.server_flag == eRRegister){            SendRegister(msg, g_StackInfo.username, g_StackInfo.serv_addr);            do{                sleep(1);            }while(g_StackInfo.state != eSIdle);            printf("done\n");        }    }else if(tolower(*usercommand) == 'i'){        if(g_StackInfo.state != eSIdle){            debug("Warning!!Invalid State\n");            return -1;        }else{            SendInvite( msg, g_StackInfo.username,                 called, g_StackInfo.rtpport);        }    }else if(tolower(*usercommand) == 'o'){        if(g_StackInfo.state != eSWaitBusy){            debug("Warning!!Invalid State\n");            return -1;        }else{            SendOK(msg);        }    }else if(tolower(*usercommand) == 'b'){        if(g_StackInfo.state != eSWaitBusy && g_StackInfo.state != eSBusy ){            debug("Warning!!Invalid State\n");            return -1;        }else{            SendBye(msg);        }    }else if(tolower(*usercommand) == 'u'){        if(g_StackInfo.server_flag == eRRegister){            SendUnRegister(msg);        }    }else if(tolower(*usercommand) == 'c'){        if(g_StackInfo.state != eSWaitBusy){            debug("Warning!!Invalid State\n");            return -1;        }else{            SendCancel(msg);        }    }else{        printf("Undefined command plese retry input command.\n");    }    return 0;}static char* skipblank(char** pl){    char *p = *pl;    /* スペ〖スをとばす */    while(*p == ' ' || *p == '\t') p++;    *pl = p;    if(*p == '\0' || *p == '\r' || *p == '\n'){        return NULL;    }    return p;}static char* skipblank_rev(char** pl, char* first){    char *p = *pl;    while((*(p-1) == ' ' || *(p-1) == '\t') && p > first) p--;    *pl = p;    return p;}static void cutcrlf(char* p){    char *crlf = strchr(p, '\r');    if(crlf == NULL) crlf = strchr(p, '\n');    if(crlf) *crlf = '\0';}char* getstring(const char* filename, const char* key, char* buff){    int lineno;    char *pl;    char line[1000];    FILE *fp = fopen(filename, "rt");    buff[0] = '\0';    if(fp == NULL) {        printf("getstring(): file open error.\n");        return buff;    }    lineno = 0;    while((pl=fgets(line, 1000, fp)) != NULL){        char *p;        lineno++;        if(skipblank(&pl) == NULL || *pl == '#'){            continue;        }        p = strchr(pl, '=');        if(p){            char *val = p+1;            skipblank_rev(&p, pl);            *p = '\0';            p = val;        }        else{            continue;        }        cutcrlf(p);        if(p && strcmp(key, pl) == 0){            /* キ〖叹疚を玫す */            p = skipblank(&p);            /* 猛を艰り叫す */            if(p){                strcpy(buff, p);            }else{                buff[0] = '\0';            }            return buff;        }    }    buff[0] = '\0';    return buff;}int loadsetting(STACK_INFO* info, const char* file){    char buff[MAX_FIELD];    /* 极眉琐で蝗脱するRTPポ〖ト戎规∈rtp_port∷ */    getstring(file, "rtp_port", buff);    info->rtpport = atoi(buff);    if( info->rtpport < 0){        debug("loadsetting(): get rtp_port failed.\n");        return -1;    }    printf("        rtp_port=%d\n", info->rtpport);        getstring(file, "user_name", buff);        strcpy(info->username, buff);        if(strlen(info->username) == 0){                debug("loadsetting(): get user_name failed.\n");                return -1;        }        printf("        user_name=%s\n", info->username);    /* SIPサ〖バのIPアドレス */    getstring(file, "reg_svr", buff);    strcpy(info->serv_addr, buff);    if(strlen(info->serv_addr) == 0){        debug("loadsetting(): get reg_svr failed.\n");        return -1;    }    printf("        reg_svr=%s\n", info->serv_addr);    getstring(file, "use_svr", buff);    if(strcmp(buff, "on")==0){        info->server_flag = eRRegister;    }else if(strcmp(buff, "off")==0){        info->server_flag = eRNull;    }else{        debug("loadsetting(): get user_name failed.\n");        return -1;    }    printf("        use_svr=%s\n", buff);    return 0;}

⌨️ 快捷键说明

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