eXosip示例程序——注册/认证 .

2024-02-22 02:38

本文主要是介绍eXosip示例程序——注册/认证 .,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#!/bin/sh
./sip_reg  \
-r sip:188.131.134.191:5060 \
-u sip:1002@188.131.134.191 \
-c sip:1002@10.0.192.88:5060 \
-U 1002 \
-P 1002  \
-p 5060 \
-e 60 \
-d

 实例脚本

 

在eXosip中,当用户第一次发送注册请求后,eXosip会产生EXOSIP_REGISTRATION_FAILURE事件,注意这并不一定是注册失败,用户需要根据event->response->status_code 来判断具体的状态,如果是401状态,则是服务器需要用户再次发送带认证信息的注册请求;如果是其他值,则认为注册失败,比如无法连接服务器等。

    对于EXOSIP_REGISTRATION_FAILURE事件,有两种处理方式:
    1、自动处理。
    在eXosip事件处理循环中调用eXosip_automatic_action()函数,该函数会自动帮助用户处理401/407状态,用户并不再需要手动发送带认证信息的注册请求,只需要在第一次注册时就添加好认证信息即可;

    2、手动处理,需要在EXOSIP_REGISTRATION_FAILURE事件中增加以下代码:

 

/** SIP Registration Agent -- by ww@styx.org* * This program is Free Software, released under the GNU General* Public License v2.0 http://www.gnu.org/licenses/gpl** This program will register to a SIP proxy using the contact* supplied on the command line. This is useful if, for some * reason your SIP client cannot register to the proxy itself.* For example, if your SIP client registers to Proxy A, but* you want to be able to recieve calls that arrive at Proxy B,* you can use this program to register the client's contact* information to Proxy B.** This program requires the eXosip library. To compile,* assuming your eXosip installation is in /usr/local,* use something like:** gcc -O2 -I/usr/local/include -L/usr/local/lib sipreg.c \*         -o sipreg \*         -leXosip2 -losip2 -losipparser2 -lpthread** It should compile and run on any POSIX compliant system* that supports pthreads.**/#if defined(__arc__)
#define LOG_PERROR 1
#include <includes_api.h>
#include <os_cfg_pub.h>
#endif#if !defined(WIN32) && !defined(_WIN32_WCE)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <syslog.h>
#ifndef OSIP_MONOTHREAD
#include <pthread.h>
#endif
#endif#ifdef _WIN32_WCE
/* #include <syslog.h> */
#include <winsock2.h>
#endif#include <osip2/osip_mt.h>
#include <eXosip2/eXosip.h>#if !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__arc__)
#define _GNU_SOURCE
#include <getopt.h>
#endif#define PROG_NAME "sipreg"
#define PROG_VER  "1.0"
#define UA_STRING "SipReg v" PROG_VER
#define SYSLOG_FACILITY LOG_DAEMON#if defined(WIN32) || defined(_WIN32_WCE)
static void
syslog_wrapper (int a, const char *fmt, ...)
{va_list args;va_start (args, fmt);vfprintf (stdout, fmt, args);va_end (args);
}#define LOG_INFO 0
#define LOG_ERR 0
#define LOG_WARNING 0
#define LOG_DEBUG 0#elif defined(LOG_PERROR)
/* If we can, we use syslog() to emit the debugging messages to stderr. */
#define syslog_wrapper    syslog
#else
#define syslog_wrapper(a,b...) fprintf(stderr,b);fprintf(stderr,"\n")
#endifstatic void usage (void);#ifndef OSIP_MONOTHREAD
static void *register_proc (void *arg);
#endifstatic void
usage (void)
{printf ("Usage: " PROG_NAME " [required_options] [optional_options]\n""\n\t[required_options]\n""\t-r --proxy\tsip:proxyhost[:port]\n""\t-u --from\tsip:user@host[:port]\n""\n\t[optional_options]\n""\t-c --contact\tsip:user@host[:port]\n""\t-d --debug (log to stderr and do not fork)\n""\t-e --expiry\tnumber (default 3600)\n""\t-f --firewallip\tN.N.N.N\n" "\t-h --help\n" "\t-l --localip\tN.N.N.N (force local IP address)\n" "\t-p --port\tnumber (default 5060)\n" "\t-U --username\tauthentication username\n" "\t-P --password\tauthentication password\n");
}typedef struct regparam_t {int regid;int expiry;int auth;
} regparam_t;struct eXosip_t *context_eXosip;#ifndef OSIP_MONOTHREAD
static void *
register_proc (void *arg)
{struct regparam_t *regparam = arg;int reg;for (;;) {
#ifdef _WIN32_WCESleep ((regparam->expiry / 2) * 1000);
#elsesleep (regparam->expiry / 2);
#endifeXosip_lock (context_eXosip);reg = eXosip_register_send_register (context_eXosip, regparam->regid, NULL);printf("======================eXosip_register_send_register:%d\n", reg);if (0 > reg) {
#ifdef _WIN32_WCEfprintf (stdout, "eXosip_register: error while registring");
#elseperror ("eXosip_register");
#endifexit (1);}regparam->auth = 0;eXosip_unlock (context_eXosip);}return NULL;	
}
#endif#ifdef _WIN32_WCE
int WINAPI
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
#else
int
main (int argc, char *argv[])
#endif
{int c;int port = 5060;char *contact = NULL;char *fromuser = NULL;const char *localip = NULL;const char *firewallip = NULL;char *proxy = NULL;#if !defined(__arc__)struct servent *service;
#endifchar *username = NULL;char *password = NULL;struct regparam_t regparam = { 0, 3600, 0 };
#ifndef OSIP_MONOTHREADstruct osip_thread *register_thread;
#endifint debug = 0;int nofork = 0;#ifdef _WIN32_WCEproxy = osip_strdup ("sip:sip.antisip.com");fromuser = osip_strdup ("sip:jack@sip.antisip.com");#elsefor (;;) {
#define short_options "c:de:f:hl:p:r:u:U:P:"
#ifdef _GNU_SOURCEint option_index = 0;static struct option long_options[] = {{"contact", required_argument, NULL, 'c'},{"debug", no_argument, NULL, 'd'},{"expiry", required_argument, NULL, 'e'},{"firewallip", required_argument, NULL, 'f'},{"from", required_argument, NULL, 'u'},{"help", no_argument, NULL, 'h'},{"localip", required_argument, NULL, 'l'},{"port", required_argument, NULL, 'p'},{"proxy", required_argument, NULL, 'r'},{"username", required_argument, NULL, 'U'},{"password", required_argument, NULL, 'P'},{NULL, 0, NULL, 0}};c = getopt_long (argc, argv, short_options, long_options, &option_index);
#elsec = getopt (argc, argv, short_options);
#endifif (c == -1)break;switch (c) {case 'c':contact = optarg;break;case 'd':nofork = 1;
#ifdef LOG_PERRORdebug = LOG_PERROR;
#endifbreak;case 'e':regparam.expiry = atoi (optarg);break;case 'f':firewallip = optarg;break;case 'h':usage ();exit (0);case 'l':localip = optarg;break;case 'p':
#if !defined(__arc__)service = getservbyname (optarg, "udp");if (service)port = ntohs (service->s_port);elseport = atoi (optarg);
#elseport = atoi (optarg);
#endifbreak;case 'r':proxy = optarg;break;case 'u':fromuser = optarg;break;case 'U':username = optarg;break;case 'P':password = optarg;break;default:break;}}
#endifif (!proxy || !fromuser) {usage ();exit (1);}
#ifndef _WIN32_WCEif (!nofork) {daemon (1, 0);}
#endif#if 0openlog (PROG_NAME, LOG_PID | debug, SYSLOG_FACILITY);
#endifsyslog_wrapper (LOG_INFO, UA_STRING " up and running");syslog_wrapper (LOG_INFO, "proxy: %s", proxy);syslog_wrapper (LOG_INFO, "fromuser: %s", fromuser);syslog_wrapper (LOG_INFO, "contact: %s", contact);syslog_wrapper (LOG_INFO, "expiry: %d", regparam.expiry);syslog_wrapper (LOG_INFO, "local port: %d", port);if (debug > 0)TRACE_INITIALIZE (6, NULL);context_eXosip = eXosip_malloc ();if (eXosip_init (context_eXosip)) {syslog_wrapper (LOG_ERR, "eXosip_init failed");exit (1);}if (eXosip_listen_addr (context_eXosip, IPPROTO_TCP, NULL, port, AF_INET, 0)) {syslog_wrapper (LOG_ERR, "eXosip_listen_addr failed");exit (1);}if (localip) {syslog_wrapper (LOG_INFO, "local address: %s", localip);eXosip_masquerade_contact (context_eXosip, localip, port);}if (firewallip) {syslog_wrapper (LOG_INFO, "firewall address: %s:%i", firewallip, port);eXosip_masquerade_contact (context_eXosip, firewallip, port);}eXosip_set_user_agent (context_eXosip, UA_STRING);if (username && password) {syslog_wrapper (LOG_INFO, "username: %s", username);syslog_wrapper (LOG_INFO, "password: [removed]");if (eXosip_add_authentication_info (context_eXosip, username, username, password, NULL, NULL)) {syslog_wrapper (LOG_ERR, "eXosip_add_authentication_info failed");exit (1);}}{osip_message_t *reg = NULL;int i;regparam.regid = eXosip_register_build_initial_register (context_eXosip, fromuser, proxy, contact, regparam.expiry * 2, &reg);printf("=============================regid:%d\n", regparam.regid);if (regparam.regid < 1) {syslog_wrapper (LOG_ERR, "eXosip_register_build_initial_register failed");exit (1);}i = eXosip_register_send_register (context_eXosip, regparam.regid, reg);printf("======================eXosip_register_send_register:%d\n", i);if (i != 0) {syslog_wrapper (LOG_ERR, "eXosip_register_send_register failed");exit (1);}}#ifndef OSIP_MONOTHREADregister_thread = osip_thread_create (20000, register_proc, &regparam);if (register_thread == NULL) {syslog_wrapper (LOG_ERR, "pthread_create failed");exit (1);}
#endiffor (;;) {eXosip_event_t *event;if (!(event = eXosip_event_wait (context_eXosip, 0, 1))) {
#ifdef OSIP_MONOTHREADeXosip_execute (context_eXosip);eXosip_automatic_action (context_eXosip);
#endifosip_usleep (10000);continue;}
#ifdef OSIP_MONOTHREADeXosip_execute (context_eXosip);
#endifeXosip_automatic_action (context_eXosip);switch (event->type) {case EXOSIP_REGISTRATION_SUCCESS:syslog_wrapper (LOG_INFO, "registrered successfully");printf("=======================registrered successfully\n");break;case EXOSIP_REGISTRATION_FAILURE:regparam.auth = 1;break;default:syslog_wrapper (LOG_DEBUG, "recieved unknown eXosip event (type, did, cid) = (%d, %d, %d)", event->type, event->did, event->cid);}eXosip_event_free (event);}
}

 

参考:

https://blog.csdn.net/yyyxxxzzz111/article/details/24885631

这篇关于eXosip示例程序——注册/认证 .的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/733879

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee