Skip to content

Commit

Permalink
Implement pjsua2 setting sock opt+sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming committed Sep 12, 2024
1 parent 2092026 commit 0cc536b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
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 1
/* 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
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,22 @@ public void init(MyAppObserver obs, String app_dir,
}

try {
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
27 changes: 19 additions & 8 deletions pjsip/include/pjsua2/siptypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,31 @@ struct SockOpt {
*/
int optName;

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

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

/**
* Pointer to the buffer in which the option is specified.
* 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.
*/
/** Buffer size of the buffer pointed by optVal. */
int optLen;

/**
* Internal buffer for optval.
*/
string optValBuf_;
/** Option value if the type is integer. */
int optValInt;
};

/** Array of socket options */
Expand Down
49 changes: 37 additions & 12 deletions pjsip/src/pjsua2/siptypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ void TlsConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
NODE_READ_NUM_T ( this_node, pj_qos_type, qosType);
readQosParams ( this_node, qosParams);
NODE_READ_BOOL ( this_node, qosIgnoreError);
NODE_READ_OBJ ( this_node, sockOptParams);
NODE_READ_BOOL ( this_node, sockOptIgnoreError);
NODE_READ_NUM_T ( this_node, pj_ssl_cert_lookup_type, certLookupType);
NODE_READ_STRING ( this_node, certLookupKeyword);
}
Expand All @@ -288,12 +290,34 @@ void TlsConfig::writeObject(ContainerNode &node) const PJSUA2_THROW(Error)
NODE_WRITE_NUM_T ( this_node, pj_qos_type, qosType);
writeQosParams ( this_node, qosParams);
NODE_WRITE_BOOL ( this_node, qosIgnoreError);
NODE_WRITE_OBJ ( this_node, sockOptParams);
NODE_WRITE_BOOL ( this_node, sockOptIgnoreError);
NODE_WRITE_NUM_T ( this_node, pj_ssl_cert_lookup_type, certLookupType);
NODE_WRITE_STRING ( this_node, certLookupKeyword);
}

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

SockOpt::SockOpt()
{
pj_bzero(this, sizeof(*this));
}

SockOpt::SockOpt(int level, int optName, int optVal)
{
pj_bzero(this, sizeof(*this));
this->level = level;
this->optName = optName;
setOptValInt(optVal);
}

void SockOpt::setOptValInt(int opt_val)
{
optVal = &optValInt;
optLen = sizeof(int);
optValInt = opt_val;
}

SockOptParams::SockOptParams()
{
}
Expand All @@ -305,6 +329,8 @@ pj_sockopt_params SockOptParams::toPj() const

pj_bzero(&sop, sizeof(sop));
sop.cnt = this->sockOpts.size();
if (sop.cnt > PJ_MAX_SOCKOPT_PARAMS)
sop.cnt = PJ_MAX_SOCKOPT_PARAMS;
for (i = 0; i < sop.cnt; ++i) {
sop.options[i].level = this->sockOpts[i].level;
sop.options[i].optname = this->sockOpts[i].optName;
Expand All @@ -324,14 +350,8 @@ void SockOptParams::fromPj(const pj_sockopt_params &prm)
SockOpt so;
so.level = prm.options[i].level;
so.optName = prm.options[i].optname;
if (prm.options[i].optlen > 0) {
string so_val((char*)prm.options[i].optval, prm.options[i].optlen);
so.optValBuf_ = so_val;
so.optVal = (void*)so.optValBuf_.data();
so.optLen = so.optValBuf_.size();
} else {
so.optVal = NULL;
so.optLen = 0;
if (prm.options[i].optlen == sizeof(int)) {
so.setOptValInt(*((int *)prm.options[i].optval));
}
this->sockOpts.push_back(so);
}
Expand All @@ -346,9 +366,11 @@ void SockOptParams::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
SockOpt so;
so.level = so_node.readInt("level");
so.optName = so_node.readInt("optName");
so.optValBuf_ = so_node.readString("optVal");
so.optLen = so.optValBuf_.size();
so.optVal = so.optLen > 0? (void*)so.optValBuf_.data() : NULL;
so.optLen = so_node.readInt("optLen");
if (so.optLen == sizeof(int)) {
int optVal = so_node.readInt("optVal");
so.setOptValInt(optVal);
}
sockOpts.push_back(so);
}
}
Expand All @@ -362,7 +384,10 @@ void SockOptParams::writeObject(ContainerNode &node) const PJSUA2_THROW(Error)
sockOpts[i].optLen > 0? sockOpts[i].optLen : 0);
so_node.writeInt("level", sockOpts[i].level);
so_node.writeInt("optName", sockOpts[i].optName);
so_node.writeString("optVal", so_val);
so_node.writeInt("optLen", sockOpts[i].optLen);
if (sockOpts[i].optLen == sizeof(int)) {
so_node.writeInt("optVal", sockOpts[i].optValInt);
}
}
}

Expand Down

0 comments on commit 0cc536b

Please sign in to comment.