Skip to content

Commit

Permalink
Move re-usable registrar code under "lib/reg/"
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Dec 5, 2016
1 parent 42bbbf3 commit 4b53a3b
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 293 deletions.
14 changes: 2 additions & 12 deletions modules/mid_registrar/rerrno.c → lib/reg/rerrno.c
@@ -1,11 +1,7 @@
/*
* mid-registrar errno
*
* This module is intended to be used as a middle layer SIP component in
* environments where a large proportion of SIP UAs (e.g. mobile devices)
* register at high enough frequencies that they actually degrade the
* performance of their registrars.
* Registrar errno
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2016 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
Expand All @@ -23,10 +19,6 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2016-07-06 initial version (liviu)
*/

#include "../../str.h"
Expand Down Expand Up @@ -66,7 +58,6 @@ str error_info[] = {
{EI_R_CALLID_LEN, sizeof(EI_R_CALLID_LEN) - 1},
{EI_R_PARSE_PATH, sizeof(EI_R_PARSE_PATH) - 1},
{EI_R_PATH_UNSUP, sizeof(EI_R_PATH_UNSUP) - 1}

};

int rerr_codes[] = {
Expand Down Expand Up @@ -100,5 +91,4 @@ int rerr_codes[] = {
400, /* R_CALLID_LEN */
400, /* R_PARSE_PATH */
420 /* R_PATH_UNSUP */

};
63 changes: 27 additions & 36 deletions modules/mid_registrar/rerrno.h → lib/reg/rerrno.h
@@ -1,11 +1,7 @@
/*
* mid-registrar errno
*
* This module is intended to be used as a middle layer SIP component in
* environments where a large proportion of SIP UAs (e.g. mobile devices)
* register at high enough frequencies that they actually degrade the
* performance of their registrars.
* Registrar errno
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2016 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
Expand All @@ -23,39 +19,35 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2016-07-06 initial version (liviu)
*/

#ifndef __MID_REG_ERRNO_
#define __MID_REG_ERRNO_
#ifndef __LIB_REG_RERRNO_H__
#define __LIB_REG_RERRNO_H__

#define EI_R_FINE "No problem" /* R_FINE */
#define EI_R_UL_DEL_R "usrloc_record_delete failed" /* R_UL_DEL_R */
#define EI_R_UL_GET_R "usrloc_record_get failed" /* R_UL_GET */
#define EI_R_UL_NEW_R "usrloc_record_new failed" /* R_UL_NEW_R */
#define EI_R_INV_CSEQ "Invalid CSeq number" /* R_INV_CSEQ */
#define EI_R_UL_INS_C "usrloc_contact_insert failed" /* R_UL_INS_C */
#define EI_R_UL_INS_R "usrloc_record_insert failed" /* R_UL_INS_R */
#define EI_R_UL_DEL_C "usrloc_contact_delete failed" /* R_UL_DEL_C */
#define EI_R_UL_UPD_C "usrloc_contact_update failed" /* R_UL_UPD_C */
#define EI_R_TO_USER "No username in To URI" /* R_TO_USER */
#define EI_R_AOR_LEN "Address Of Record too long" /* R_AOR_LEN */
#define EI_R_AOR_PARSE "Error while parsing AOR" /* R_AOR_PARSE */
#define EI_R_INV_EXP "Invalid expires param in contact" /* R_INV_EXP */
#define EI_R_INV_Q "Invalid q param in contact" /* R_INV_Q */
#define EI_R_PARSE "Message parse error" /* R_PARSE */
#define EI_R_TO_MISS "To header not found" /* R_TO_MISS */
#define EI_R_CID_MISS "Call-ID header not found" /* R_CID_MISS */
#define EI_R_CS_MISS "CSeq header not found" /* R_CS_MISS */
#define EI_R_PARSE_EXP "Expires parse error" /* R_PARSE_EXP */
#define EI_R_PARSE_CONT "Contact parse error" /* R_PARSE_CONT */
#define EI_R_STAR_EXP "* used in contact and expires is not zero" /* R_STAR__EXP */
#define EI_R_STAR_CONT "* used in contact and more than 1 contact" /* R_STAR_CONT */
#define EI_R_OOO "Out of order request" /* R_OOO */
#define EI_R_RETRANS "Retransmission" /* R_RETRANS */
#define EI_R_UL_GET_R "usrloc_record_get failed" /* R_UL_GET */
#define EI_R_UL_NEW_R "usrloc_record_new failed" /* R_UL_NEW_R */
#define EI_R_INV_CSEQ "Invalid CSeq number" /* R_INV_CSEQ */
#define EI_R_UL_INS_C "usrloc_contact_insert failed" /* R_UL_INS_C */
#define EI_R_UL_INS_R "usrloc_record_insert failed" /* R_UL_INS_R */
#define EI_R_UL_DEL_C "usrloc_contact_delete failed" /* R_UL_DEL_C */
#define EI_R_UL_UPD_C "usrloc_contact_update failed" /* R_UL_UPD_C */
#define EI_R_TO_USER "No username in To URI" /* R_TO_USER */
#define EI_R_AOR_LEN "Address Of Record too long" /* R_AOR_LEN */
#define EI_R_AOR_PARSE "Error while parsing AOR" /* R_AOR_PARSE */
#define EI_R_INV_EXP "Invalid expires param in contact" /* R_INV_EXP */
#define EI_R_INV_Q "Invalid q param in contact" /* R_INV_Q */
#define EI_R_PARSE "Message parse error" /* R_PARSE */
#define EI_R_TO_MISS "To header not found" /* R_TO_MISS */
#define EI_R_CID_MISS "Call-ID header not found" /* R_CID_MISS */
#define EI_R_CS_MISS "CSeq header not found" /* R_CS_MISS */
#define EI_R_PARSE_EXP "Expires parse error" /* R_PARSE_EXP */
#define EI_R_PARSE_CONT "Contact parse error" /* R_PARSE_CONT */
#define EI_R_STAR_EXP "* used in contact and expires is not zero" /* R_STAR__EXP */
#define EI_R_STAR_CONT "* used in contact and more than 1 contact" /* R_STAR_CONT */
#define EI_R_OOO "Out of order request" /* R_OOO */
#define EI_R_RETRANS "Retransmission" /* R_RETRANS */
#define EI_R_UNESCAPE "Error while unescaping username" /* R_UNESCAPE */
#define EI_R_TOO_MANY "Too many registered contacts" /* R_TOO_MANY */
#define EI_R_CONTACT_LEN "Contact/received too long" /* R_CONTACT_LEN */
Expand Down Expand Up @@ -94,9 +86,8 @@ typedef enum rerr {
R_CALLID_LEN, /*!< Callid too long */
R_PARSE_PATH, /*!< Error while parsing Path */
R_PATH_UNSUP /*!< Path not supported by UAC */

} rerr_t;

extern rerr_t rerrno;

#endif /* __MID_REG_ERRNO_ */
#endif /* __RERRNO_H__ */
150 changes: 150 additions & 0 deletions lib/reg/sip_msg.c
@@ -0,0 +1,150 @@
/*
* Registrar specific SIP message processing
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2016 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../../parser/contact/parse_contact.h"
#include "sip_msg.h"
#include "rerrno.h"

/*! \brief
* Parse the whole message and bodies of all header fields
* that will be needed by registrar
*/
int parse_reg_headers(struct sip_msg* _m)
{
struct hdr_field* ptr;

if (parse_headers(_m, HDR_EOH_F, 0) == -1) {
rerrno = R_PARSE;
LM_ERR("failed to parse headers\n");
return -1;
}

if (!_m->to) {
rerrno = R_TO_MISS;
LM_ERR("To not found\n");
return -2;
}

if (!_m->callid) {
rerrno = R_CID_MISS;
LM_ERR("Call-ID not found\n");
return -3;
}

if (!_m->cseq) {
rerrno = R_CS_MISS;
LM_ERR("CSeq not found\n");
return -4;
}

if (_m->expires && !_m->expires->parsed && (parse_expires(_m->expires) < 0)) {
rerrno = R_PARSE_EXP;
LM_ERR("failed to parse expires body\n");
return -5;
}

if (_m->contact) {
ptr = _m->contact;
while(ptr) {
if (ptr->type == HDR_CONTACT_T) {
if (!ptr->parsed && (parse_contact(ptr) < 0)) {
rerrno = R_PARSE_CONT;
LM_ERR("failed to parse Contact body\n");
return -6;
}
}
ptr = ptr->next;
}
}

return 0;
}

#define has_nonzero_exp(_m) \
(!_m->expires || !((exp_body_t*)_m->expires->parsed)->valid || \
((exp_body_t*)_m->expires->parsed)->val != 0)

/*! \brief
* Check if the originating REGISTER message was formed correctly
* The whole message must be parsed before calling the function
* _s indicates whether the contact was star
*/
int check_contacts(struct sip_msg* _m, int* _s)
{
struct hdr_field* p;
contact_t* c;

*_s = 0;
/* Message without contacts is OK */
if (_m->contact == 0) return 0;

if (((contact_body_t*)_m->contact->parsed)->star == 1) {
/* The first Contact HF is star */
/* Expires must be zero */
if (has_nonzero_exp(_m)) {
rerrno = R_STAR_EXP;
return 1;
}

/* Message must contain no contacts */
if (((contact_body_t*)_m->contact->parsed)->contacts) {
rerrno = R_STAR_CONT;
return 1;
}

/* Message must contain no other Contact HFs */
p = _m->contact->next;
while(p) {
if (p->type == HDR_CONTACT_T) {
rerrno = R_STAR_CONT;
return 1;
}
p = p->next;
}

*_s = 1;
} else { /* The first Contact HF is not star */
/* Message must contain no star Contact HF */
p = _m->contact->next;
while(p) {
if (p->type == HDR_CONTACT_T) {
if (((contact_body_t*)p->parsed)->star == 1) {
rerrno = R_STAR_CONT;
return 1;
}
/* check also the lenght of all contacts */
for(c=((contact_body_t*)p->parsed)->contacts ; c ; c=c->next) {
if (c->uri.len > CONTACT_MAX_SIZE
|| (c->received && c->received->len>RECEIVED_MAX_SIZE) ) {
rerrno = R_CONTACT_LEN;
return 1;
}
}
}
p = p->next;
}
}

return 0;
}

34 changes: 26 additions & 8 deletions modules/registrar/rerrno.c → lib/reg/sip_msg.h
@@ -1,7 +1,8 @@
/*
* Registrar errno
* Registrar specific SIP message processing
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2016 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
Expand All @@ -20,14 +21,31 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*!
* \file
* \brief SIP registrar module - registrar errno
* \ingroup registrar
*/
#ifndef __LIB_REG_CONTACT__
#define __LIB_REG_CONTACT__

#include "../../parser/hf.h"
#include "../../dprint.h"
#include "../../parser/parse_expires.h"
#include "../../parser/contact/contact.h"
#include "../../ut.h"
#include "../../qvalue.h"

#define CONTACT_MAX_SIZE 255
#define RECEIVED_MAX_SIZE 255

#include "rerrno.h"
/*! \brief
* Parse the whole message and bodies of all header fields
* that will be needed by registrar
*/
int parse_reg_headers(struct sip_msg* _m);

rerr_t rerrno;

/*! \brief
* Check if the originating REGISTER message was formed correctly
* The whole message must be parsed before calling the function
* _s indicates whether the contact was star
*/
int check_contacts(struct sip_msg* _m, int* _s);

#endif /* __LIB_REG_CONTACT__ */
2 changes: 1 addition & 1 deletion modules/mid_registrar/mid_registrar.c
Expand Up @@ -42,8 +42,8 @@
#include "lookup.h"
#include "encode.h"
#include "ulcb.h"
#include "rerrno.h"

#include "../../lib/reg/rerrno.h"
#include "../../parser/contact/contact.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/msg_parser.h"
Expand Down
2 changes: 1 addition & 1 deletion modules/mid_registrar/save.c
Expand Up @@ -34,8 +34,8 @@
#include "encode.h"
#include "save.h"
#include "gruu.h"
#include "rerrno.h"

#include "../../lib/reg/rerrno.h"
#include "../../parser/parse_rr.h"
#include "../../parser/parse_uri.h"
#include "../../parser/contact/contact.h"
Expand Down
2 changes: 1 addition & 1 deletion modules/mid_registrar/ulcb.c
Expand Up @@ -34,9 +34,9 @@
#include "../../parser/contact/contact.h"
#include "../tm/tm_load.h"
#include "../tm/dlg.h"
#include "../../lib/reg/rerrno.h"

#include "mid_registrar.h"
#include "rerrno.h"

char extra_hdrs_buf[512];
static str extra_hdrs={extra_hdrs_buf, 512};
Expand Down
2 changes: 1 addition & 1 deletion modules/registrar/common.c
Expand Up @@ -36,7 +36,7 @@
#include "../../dprint.h"
#include "../../ut.h" /* q_memchr */
#include "../../parser/parse_uri.h"
#include "rerrno.h"
#include "../../lib/reg/rerrno.h"
#include "reg_mod.h"
#include "common.h"

Expand Down
5 changes: 3 additions & 2 deletions modules/registrar/lookup.c
Expand Up @@ -40,6 +40,7 @@
#include "../../parser/parse_rr.h"
#include "../usrloc/usrloc.h"
#include "../../parser/parse_from.h"
#include "../../lib/reg/sip_msg.h"
#include "common.h"
#include "regtime.h"
#include "reg_mod.h"
Expand Down Expand Up @@ -419,7 +420,7 @@ struct to_body* select_uri(struct sip_msg* _m)

} else {
/* WARNING in msg_aor_parse the to header is checked in
* parse_message so no need to check it; take care when
* parse_reg_headers so no need to check it; take care when
* you use this function */
return get_to(_m);
}
Expand Down Expand Up @@ -449,7 +450,7 @@ int msg_aor_parse(struct sip_msg* _m, char *_aor, str *_saor)
pv_value_t val;
struct to_body *hdr;

if (parse_message(_m) < 0) {
if (parse_reg_headers(_m) < 0) {
LM_ERR("unable to parse message\n");
return -2;
}
Expand Down

0 comments on commit 4b53a3b

Please sign in to comment.