Skip to content

Commit

Permalink
Add socket option params to transport config in PJSUA2 (#4071)
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming authored Sep 13, 2024
1 parent 194dec3 commit 3c3eef9
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 4 deletions.
13 changes: 13 additions & 0 deletions pjlib/include/pj/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,19 @@ PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
*****************************************************************************
*/

/**
* Deep clone the socket options.
*
* @param pool The pool.
* @param dst Destination socket options.
* @param src Source socket options.
*
* @return PJ_SUCCESS on success.
*/
PJ_DECL(pj_status_t) pj_sockopt_params_clone(pj_pool_t *pool,
pj_sockopt_params *dst,
const pj_sockopt_params *src);

/**
* Print socket address string. This method will enclose the address string
* with square bracket if it's IPv6 address.
Expand Down
11 changes: 11 additions & 0 deletions pjlib/src/pj/sock_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
#include <pj/errno.h>
#include <pj/unicode.h>

#if 0
/* Enable some tracing */
#include <pj/log.h>
#define TRACE_(arg) PJ_LOG(4,arg)
#else
#define TRACE_(arg)
#endif

#define THIS_FILE "sock_bsd.c"

/*
Expand Down Expand Up @@ -569,6 +577,7 @@ PJ_DEF(pj_status_t) pj_sock_socket(int af,
#endif

*sock = socket(af, type, proto);
TRACE_((THIS_FILE, "Created new socket of type %d: %ld", type, *sock));
if (*sock == PJ_INVALID_SOCKET)
return PJ_RETURN_OS_ERROR(pj_get_native_netos_error());
else {
Expand Down Expand Up @@ -832,6 +841,8 @@ PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sock,
(const char*)optval, optlen);
#else
status = setsockopt(sock, level, optname, (const char*)optval, optlen);
TRACE_((THIS_FILE, "setsockopt %ld level:%d name:%d val:%d(%d)->%d", sock,
level, optname, *((const char *)optval), optlen, status));
#endif

if (status != 0)
Expand Down
24 changes: 24 additions & 0 deletions pjlib/src/pj/sock_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <pj/errno.h>
#include <pj/ip_helper.h>
#include <pj/os.h>
#include <pj/pool.h>
#include <pj/addr_resolv.h>
#include <pj/rand.h>
#include <pj/string.h>
Expand Down Expand Up @@ -1205,6 +1206,29 @@ PJ_DEF(pj_status_t) pj_sock_setsockopt_sobuf( pj_sock_t sockfd,
}


PJ_DEF(pj_status_t) pj_sockopt_params_clone(pj_pool_t *pool,
pj_sockopt_params *dst,
const pj_sockopt_params *src)
{
unsigned int i;

PJ_ASSERT_RETURN(pool && src && dst, PJ_EINVAL);

pj_memcpy(dst, src, sizeof(pj_sockopt_params));
for (i = 0; i < dst->cnt && i < PJ_MAX_SOCKOPT_PARAMS; ++i) {
if (dst->options[i].optlen == 0) {
dst->options[i].optval = NULL;
continue;
}
dst->options[i].optval = pj_pool_alloc(pool, dst->options[i].optlen);
pj_memcpy(dst->options[i].optval, src->options[i].optval,
dst->options[i].optlen);
}

return PJ_SUCCESS;
}


PJ_DEF(char *) pj_addr_str_print( const pj_str_t *host_str, int port,
char *buf, int size, unsigned flag)
{
Expand Down
2 changes: 2 additions & 0 deletions pjlib/src/pj/ssl_sock_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ PJ_DEF(void) pj_ssl_sock_param_copy( pj_pool_t *pool,
/* Path name must be null-terminated */
pj_strdup_with_null(pool, &dst->entropy_path, &src->entropy_path);
}

pj_sockopt_params_clone(pool, &dst->sockopt_params, &src->sockopt_params);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,25 @@ public void init(MyAppObserver obs, String app_dir,
}

try {
/* Setting socket option parameters (uncomment this if needed). */
/*
final int SOL_SOCKET = 1;
final int SOL_TCP = 6;
final int SO_KEEPALIVE = 9;
final int TCP_KEEPIDLE = 4;
final int TCP_KEEPINTVL = 5;
final int TCP_KEEPCNT = 6;
SockOptVector soVector = new SockOptVector();
soVector.add(new SockOpt(SOL_SOCKET, SO_KEEPALIVE, 1));
soVector.add(new SockOpt(SOL_TCP, TCP_KEEPIDLE, 1));
soVector.add(new SockOpt(SOL_TCP, TCP_KEEPINTVL, 5));
soVector.add(new SockOpt(SOL_TCP, TCP_KEEPCNT, 1));
sipTpConfig.getTlsConfig().getSockOptParams().setSockOpts(soVector);
*/

sipTpConfig.setPort(SIP_PORT+1);
ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_TLS,
sipTpConfig);
Expand Down
1 change: 1 addition & 0 deletions pjsip-apps/src/swig/pjsua2.i
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ using namespace pj;

%include "pjsua2/siptypes.hpp"

%template(SockOptVector) std::vector<pj::SockOpt>;
%template(SipHeaderVector) std::vector<pj::SipHeader>;
%template(AuthCredInfoVector) std::vector<pj::AuthCredInfo>;
%template(SrtpCryptoVector) std::vector<pj::SrtpCrypto>;
Expand Down
2 changes: 2 additions & 0 deletions pjsip/include/pjsip/sip_transport_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ PJ_INLINE(void) pjsip_tls_setting_copy(pj_pool_t *pool,
for (i=0; i<src->curves_num; ++i)
dst->curves[i] = src->curves[i];
}

pj_sockopt_params_clone(pool, &dst->sockopt_params, &src->sockopt_params);
}


Expand Down
106 changes: 106 additions & 0 deletions pjsip/include/pjsua2/siptypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,86 @@ struct AuthCredInfo : public PersistentObject

//////////////////////////////////////////////////////////////////////////////

/**
* Socket option type.
*/
struct SockOpt {
/**
* The level at which the option is defined.
*/
int level;

/**
* Option name.
*/
int optName;

public:
/** Default constructor. */
SockOpt();

/** Construct a socket option with the specified parameters. */
SockOpt(int level, int optName, int optVal);

/**
* Set option value of type integer.
*
* @param opt_val Option value.
*/
void setOptValInt(int opt_val);

private:
friend struct SockOptParams;

/** Pointer to the buffer in which the option is specified. */
void *optVal;

/** Buffer size of the buffer pointed by optVal. */
int optLen;

/** Option value if the type is integer. */
int optValInt;
};

/** Array of socket options */
typedef std::vector<SockOpt> SockOptVector;

/**
* Socket option parameters, to be specified in TransportConfig.
*/
struct SockOptParams : public PersistentObject
{
/**
* Array of socket options.
*/
SockOptVector sockOpts;

public:
/** Default constructor initialises with default values */
SockOptParams();

/** Convert to pjsip */
pj_sockopt_params toPj() const;

/** Convert from pjsip */
void fromPj(const pj_sockopt_params &prm);

/**
* Read this object from a container node.
*
* @param node Container to read values from.
*/
virtual void readObject(const ContainerNode &node) PJSUA2_THROW(Error);

/**
* Write this object to a container node.
*
* @param node Container to write values to.
*/
virtual void writeObject(ContainerNode &node) const PJSUA2_THROW(Error);
};


/**
* TLS transport settings, to be specified in TransportConfig.
*/
Expand Down Expand Up @@ -300,6 +380,22 @@ struct TlsConfig : public PersistentObject
*/
bool qosIgnoreError;

/**
* Specify options to be set on the transport.
*
* By default, this is unset, which means that the underlying sockopt
* params as returned by #pj_ssl_sock_param_default() will be used.
*/
SockOptParams sockOptParams;

/**
* Specify if the transport should ignore any errors when setting the
* sockopt parameters.
*
* Default: true
*/
bool sockOptIgnoreError;

/**
* Specify if renegotiation is enabled for TLSv1.2 or earlier.
*
Expand Down Expand Up @@ -424,6 +520,16 @@ struct TransportConfig : public PersistentObject
*/
pj_qos_params qosParams;

/**
* Set the low level socket options to the transport.
*
* For TLS transport, this field will be ignored, the socket options
* can be set via tlsConfig.
*
* Default is no socket option set.
*/
SockOptParams sockOptParams;

public:
/** Default constructor initialises with default values */
TransportConfig();
Expand Down
4 changes: 2 additions & 2 deletions pjsip/src/pjsip/sip_transport_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ PJ_DEF(pj_status_t) pjsip_tcp_transport_start3(
listener->initial_timeout = cfg->initial_timeout;
pj_memcpy(&listener->qos_params, &cfg->qos_params,
sizeof(cfg->qos_params));
pj_memcpy(&listener->sockopt_params, &cfg->sockopt_params,
sizeof(cfg->sockopt_params));
pj_sockopt_params_clone(pool, &listener->sockopt_params,
&cfg->sockopt_params);

pj_ansi_strxcpy(listener->factory.obj_name, "tcptp",
sizeof(listener->factory.obj_name));
Expand Down
8 changes: 6 additions & 2 deletions pjsip/src/pjsua2/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ static void jsonNode_writeString(ContainerNode *node,
pj_json_elem *el = jdat->doc->allocElement();
pj_str_t nm = alloc_name(jdat->doc, name);
pj_str_t new_val;
pj_strdup2(jdat->doc->getPool(), &new_val, value.c_str());
pj_str_t str_val;
pj_strset(&str_val, (char*)value.data(), value.size());
pj_strdup(jdat->doc->getPool(), &new_val, &str_val);
pj_json_elem_string(el, &nm, &new_val);

pj_json_elem_add(jdat->jnode, el);
Expand All @@ -491,8 +493,10 @@ static void jsonNode_writeStringVector(ContainerNode *node,
pj_json_elem_array(el, &nm);
for (unsigned i=0; i<value.size(); ++i) {
pj_str_t new_val;
pj_str_t str_val;

pj_strdup2(jdat->doc->getPool(), &new_val, value[i].c_str());
pj_strset(&str_val, (char*)value[i].data(), value[i].size());
pj_strdup(jdat->doc->getPool(), &new_val, &str_val);
pj_json_elem *child = jdat->doc->allocElement();
pj_json_elem_string(child, NULL, &new_val);
pj_json_elem_add(el, child);
Expand Down
Loading

0 comments on commit 3c3eef9

Please sign in to comment.