Skip to content

Commit

Permalink
Refactoring code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
staskobzar committed Jun 20, 2018
1 parent 591df35 commit d057954
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 101 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

[![Build Status](https://travis-ci.org/staskobzar/sippak.svg?branch=master)](https://travis-ci.org/staskobzar/sippak)
[![codecov](https://codecov.io/gh/staskobzar/sippak/branch/master/graph/badge.svg)](https://codecov.io/gh/staskobzar/sippak)
[![CodeFactor](https://www.codefactor.io/repository/github/staskobzar/sippak/badge/master)](https://www.codefactor.io/repository/github/staskobzar/sippak/overview/master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/3c43374de75b492e9e0be3d97612e643)](https://www.codacy.com/app/staskobzar/sippak?utm_source=github.com&utm_medium=referral&utm_content=staskobzar/sippak&utm_campaign=Badge_Grade)
![GPL3](https://img.shields.io/badge/license-GPL_3-green.svg "License")

Simple command line utility for SIP protocol based on PJPROJECT.
Simple command line utility for SIP protocol based on [PJPROJECT](http://www.pjsip.org/).
Can be useful for SIP administrators and developers.

<p align="center">
<img src="https://github.com/staskobzar/sippak/blob/master/sippak.png?raw=true" width="600"/>
</p>

### Install

Expand All @@ -16,6 +23,8 @@ make
sudo make install
```

Also, packages (rpm, deb and tgz) are available in "[dist](https://github.com/staskobzar/sippak/tree/master/dist)" directory.

### Usage

```
Expand Down
Binary file added sippak.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/app/sip_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,27 @@ PJ_DEF(void) sippak_add_sip_headers (pjsip_tx_data *tdata, struct sippak_app *ap
}
}
}

PJ_DEF(pj_bool_t) sippak_set_proxies_list(struct sippak_app *app, pjsip_route_hdr **rset)
{
pjsip_route_hdr *route_set = pj_pool_alloc(app->pool, sizeof(pjsip_route_hdr));
int i;
const pj_str_t hname = { "Route", 5 };

if (app->cfg.proxy.cnt == 0) {
return PJ_FALSE;
}

pj_list_init(route_set);

for(i = 0; i < app->cfg.proxy.cnt; i++) {
pjsip_route_hdr *route = pjsip_parse_hdr(app->pool, &hname,
app->cfg.proxy.p[i], pj_ansi_strlen(app->cfg.proxy.p[i]),
NULL);
if (route) {
pj_list_push_back(route_set, route);
}
}
*rset = route_set;
return PJ_TRUE;
}
9 changes: 9 additions & 0 deletions src/include/sippak.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,13 @@ PJ_DEF(pj_status_t) sippak_set_media_sdp(struct sippak_app *app, pjmedia_sdp_ses
*/
PJ_DEF(void) sippak_add_sip_headers (pjsip_tx_data *tdata, struct sippak_app *app);

/**
* Set proxies list.
*
* @param dlg PJSIP dialog instance
* @param list Route set
* @return PJ_TRUE if set
*/
PJ_DEF(pj_bool_t) sippak_set_proxies_list(struct sippak_app *app, pjsip_route_hdr **route_set);

#endif
29 changes: 4 additions & 25 deletions src/mod/invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ static pj_bool_t on_rx_response (pjsip_rx_data *rdata);
static void call_on_state_changed( pjsip_inv_session *inv, pjsip_event *e);
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
static void call_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e);
static void set_dlg_outbound_proxy(pjsip_dialog *dlg, struct sippak_app *app);

static pj_bool_t early_cancel;
static pjsip_inv_session *inv;
Expand Down Expand Up @@ -129,29 +128,6 @@ static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
PJ_UNUSED_ARG(inv);
}

static void set_dlg_outbound_proxy(pjsip_dialog *dlg, struct sippak_app *app)
{
pjsip_route_hdr route_set;
int i;
const pj_str_t hname = { "Route", 5 };

if (app->cfg.proxy.cnt == 0) {
return;
}

pj_list_init(&route_set);

for(i = 0; i < app->cfg.proxy.cnt; i++) {
pjsip_route_hdr *route = pjsip_parse_hdr(dlg->pool, &hname,
app->cfg.proxy.p[i], pj_ansi_strlen(app->cfg.proxy.p[i]),
NULL);
if (route) {
pj_list_push_back(&route_set, route);
}
}
pjsip_dlg_set_route_set(dlg, &route_set);
}

/* Ping */
PJ_DEF(pj_status_t) sippak_cmd_invite (struct sippak_app *app)
{
Expand All @@ -164,6 +140,7 @@ PJ_DEF(pj_status_t) sippak_cmd_invite (struct sippak_app *app)
pjsip_inv_callback inv_cb;
pjsip_cred_info cred[1];
pjmedia_sdp_session *sdp_sess;
pjsip_route_hdr *route_set;

early_cancel = app->cfg.cancel;

Expand Down Expand Up @@ -213,7 +190,9 @@ PJ_DEF(pj_status_t) sippak_cmd_invite (struct sippak_app *app)
SIPPAK_ASSERT_SUCC(status, "Failed to create invite UAC.");

/* outbound proxy */
set_dlg_outbound_proxy(dlg, app);
if (sippak_set_proxies_list(app, &route_set) == PJ_TRUE) {
pjsip_dlg_set_route_set(dlg, route_set);
}

/* create invite request */
status = pjsip_inv_invite(inv, &tdata);
Expand Down
29 changes: 4 additions & 25 deletions src/mod/publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
static pj_bool_t on_rx_response (pjsip_rx_data *rdata);
static short unsigned auth_tries = 0;
static int pj_str_toi(pj_str_t val);
static void set_pubc_outbound_proxy(pjsip_publishc *pubc, struct sippak_app *app);

static pjsip_module mod_publish =
{
Expand Down Expand Up @@ -84,29 +83,6 @@ static void publish_cb(struct pjsip_publishc_cbparam *param)
sippak_loop_cancel();
}

static void set_pubc_outbound_proxy(pjsip_publishc *pubc, struct sippak_app *app)
{
pjsip_route_hdr route_set;
int i;
const pj_str_t hname = { "Route", 5 };

if (app->cfg.proxy.cnt == 0) {
return;
}

pj_list_init(&route_set);

for(i = 0; i < app->cfg.proxy.cnt; i++) {
pjsip_route_hdr *route = pjsip_parse_hdr(app->pool, &hname,
app->cfg.proxy.p[i], pj_ansi_strlen(app->cfg.proxy.p[i]),
NULL);
if (route) {
pj_list_push_back(&route_set, route);
}
}
pjsip_publishc_set_route_set (pubc, &route_set);
}

PJ_DEF(pj_status_t) sippak_cmd_publish (struct sippak_app *app)
{
pj_status_t status;
Expand All @@ -119,6 +95,7 @@ PJ_DEF(pj_status_t) sippak_cmd_publish (struct sippak_app *app)
pjsip_publishc *publish_sess = NULL;
pjsip_publishc_opt publish_opt;
pjsip_tx_data *tdata;
pjsip_route_hdr *route_set;
pjsip_pres_status pres_status;
pjsip_cred_info cred[1];
pj_str_t event;
Expand Down Expand Up @@ -161,7 +138,9 @@ PJ_DEF(pj_status_t) sippak_cmd_publish (struct sippak_app *app)

sippak_set_cred(app, cred);

set_pubc_outbound_proxy(publish_sess, app);
if (sippak_set_proxies_list(app, &route_set) == PJ_TRUE) {
pjsip_publishc_set_route_set (publish_sess, route_set);
}

status = pjsip_publishc_set_credentials(publish_sess, 1, cred);
SIPPAK_ASSERT_SUCC(status, "Failed to set auth credentials.");
Expand Down
29 changes: 4 additions & 25 deletions src/mod/register.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

static void reg_callback(struct pjsip_regc_cbparam *param);
static void print_reg_success(struct pjsip_regc_cbparam *regp);
static void set_regc_outbound_proxy(pjsip_regc *regc, struct sippak_app *app);

static pjsip_module mod_register =
{
Expand Down Expand Up @@ -89,29 +88,6 @@ static void reg_callback(struct pjsip_regc_cbparam *regp)
sippak_loop_cancel();
}

static void set_regc_outbound_proxy(pjsip_regc *regc, struct sippak_app *app)
{
pjsip_route_hdr route_set;
int i;
const pj_str_t hname = { "Route", 5 };

if (app->cfg.proxy.cnt == 0) {
return;
}

pj_list_init(&route_set);

for(i = 0; i < app->cfg.proxy.cnt; i++) {
pjsip_route_hdr *route = pjsip_parse_hdr(app->pool, &hname,
app->cfg.proxy.p[i], pj_ansi_strlen(app->cfg.proxy.p[i]),
NULL);
if (route) {
pj_list_push_back(&route_set, route);
}
}
pjsip_regc_set_route_set(regc, &route_set);
}

/* Register */
PJ_DEF(pj_status_t) sippak_cmd_register (struct sippak_app *app)
{
Expand All @@ -121,6 +97,7 @@ PJ_DEF(pj_status_t) sippak_cmd_register (struct sippak_app *app)
pjsip_tx_data *tdata;
pjsip_regc *regc;
pjsip_cred_info cred[1];
pjsip_route_hdr *route_set;

pj_str_t srv_url, from_uri, to_uri;
pj_str_t contacts[1];
Expand Down Expand Up @@ -154,7 +131,9 @@ PJ_DEF(pj_status_t) sippak_cmd_register (struct sippak_app *app)
status = pjsip_regc_set_credentials(regc, 1, cred);
SIPPAK_ASSERT_SUCC(status, "Failed to set auth credentials.");

set_regc_outbound_proxy(regc, app);
if (sippak_set_proxies_list(app, &route_set) == PJ_TRUE) {
pjsip_regc_set_route_set(regc, route_set);
}

if (app->cfg.cancel_all_reg == PJ_TRUE) {
status = pjsip_regc_unregister_all(regc, &tdata);
Expand Down
26 changes: 1 addition & 25 deletions src/mod/subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ static void on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
static void on_tsx_state(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event);
static sippak_evtype_e set_sub_evtype(struct sippak_app *app);
static void set_dlg_outbound_proxy(pjsip_dialog *dlg, struct sippak_app *app);
static short unsigned auth_tries = 0;

static pjsip_module mod_subscribe =
Expand Down Expand Up @@ -148,29 +147,6 @@ static sippak_evtype_e set_sub_evtype(struct sippak_app *app)
return app->cfg.pres_ev;
}

static void set_dlg_outbound_proxy(pjsip_dialog *dlg, struct sippak_app *app)
{
pjsip_route_hdr route_set;
int i;
const pj_str_t hname = { "Route", 5 };

if (app->cfg.proxy.cnt == 0) {
return;
}

pj_list_init(&route_set);

for(i = 0; i < app->cfg.proxy.cnt; i++) {
pjsip_route_hdr *route = pjsip_parse_hdr(dlg->pool, &hname,
app->cfg.proxy.p[i], pj_ansi_strlen(app->cfg.proxy.p[i]),
NULL);
if (route) {
pj_list_push_back(&route_set, route);
}
}
pjsip_dlg_set_route_set(dlg, &route_set);
}

PJ_DEF(pj_status_t) sippak_cmd_subscribe (struct sippak_app *app)
{
pj_status_t status;
Expand Down Expand Up @@ -200,7 +176,7 @@ PJ_DEF(pj_status_t) sippak_cmd_subscribe (struct sippak_app *app)
SIPPAK_ASSERT_SUCC(status, "Failed to create dialog uac.");

// TODO: set routes for subscribe
// set_dlg_outbound_proxy(dlg, app);
// sippak_set_dlg_outbound_proxy(dlg, app);

sippak_set_cred(app, cred);
status = pjsip_auth_clt_set_credentials(&dlg->auth_sess, 1, cred);
Expand Down

0 comments on commit d057954

Please sign in to comment.