From 80aff89a911c374774d3ef0820b4b7f755d0a10e Mon Sep 17 00:00:00 2001 From: Kaian Date: Thu, 27 Jun 2013 18:21:22 +0200 Subject: [PATCH] Code comment and cleanup --- src/sip.c | 71 +++++++++++----------- src/sip.h | 146 ++++++++++++++++++++++++++++++++++++--------- src/ui_call_list.h | 3 +- src/ui_manager.h | 6 +- 4 files changed, 159 insertions(+), 67 deletions(-) diff --git a/src/sip.c b/src/sip.c index 49ea1e3..a679399 100644 --- a/src/sip.c +++ b/src/sip.c @@ -217,14 +217,13 @@ sip_calls_count() int sip_check_call_ignore(sip_call_t *call) { - int ret = 0; - // ret |= is_ignored_value("sipfrom", call_get_attribute(call, SIP_ATTRIBUTE)); - // ret |= is_ignored_value("sipto", call_get_attribute(call, "sipto")); - // ret |= is_ignored_value("msgcnt", call_get_attribute(call, "msgcnt")); - // ret |= is_ignored_value("msgcnt", call_get_attribute(call, "src")); - // ret |= is_ignored_value("dst", call_get_attribute(call, "dst")); - // ret |= is_ignored_value("starting", call_get_attribute(call, "starting")); - return ret; + int i; + for (i = 0; i < sizeof(attrs) / sizeof(*attrs); i++) { + if (is_ignored_value(attrs[i].name, call_get_attribute(call, attrs[i].id))) { + return 1; + } + } + return 0; } sip_attr_hdr_t * @@ -244,24 +243,26 @@ sip_attr_get_description(enum sip_attr_id id) { sip_attr_hdr_t *header; - if ((header = sip_attr_get_header(id))){ + if ((header = sip_attr_get_header(id))) { return header->desc; } return NULL; } const char * -sip_attr_get_name(enum sip_attr_id id){ +sip_attr_get_name(enum sip_attr_id id) +{ sip_attr_hdr_t *header; - if ((header = sip_attr_get_header(id))){ + if ((header = sip_attr_get_header(id))) { return header->name; } return NULL; } enum sip_attr_id -sip_attr_from_name(const char *name){ +sip_attr_from_name(const char *name) +{ int i; for (i = 0; i < sizeof(attrs) / sizeof(*attrs); i++) { if (!strcasecmp(name, attrs[i].name)) { @@ -466,6 +467,27 @@ call_get_prev(sip_call_t *cur) return prev; } +void +call_set_attribute(sip_call_t *call, enum sip_attr_id id, const char *value) +{ + sip_attr_set(&call->attrs, id, value); +} + +const char * +call_get_attribute(sip_call_t *call, enum sip_attr_id id) +{ + char value[80]; + if (id == SIP_ATTR_MSGCNT) { + // FIXME REALLY + sprintf(value, "%d", call_msg_count(call)); + return strdup(value); + } + if (id == SIP_ATTR_STARTING) { + return msg_get_attribute(call_get_next_msg(call, NULL), SIP_ATTR_METHOD); + } + return msg_get_attribute(call_get_next_msg(call, NULL), id); +} + sip_msg_t * msg_parse(sip_msg_t *msg) { @@ -556,7 +578,9 @@ msg_parse_payload(sip_msg_t *msg, const char *payload) continue; } if (sscanf(pch, "SIP/2.0 %[^\n]", value)) { - msg_set_attribute(msg, SIP_ATTR_METHOD, value); + if (!msg_get_attribute(msg, SIP_ATTR_METHOD)) { + msg_set_attribute(msg, SIP_ATTR_METHOD, value); + } continue; } if (sscanf(pch, "CSeq: %d %[^\t\n\r]", &irest, value)) { @@ -582,27 +606,6 @@ msg_parse_payload(sip_msg_t *msg, const char *payload) return 0; } -void -call_set_attribute(sip_call_t *call, enum sip_attr_id id, const char *value) -{ - sip_attr_set(&call->attrs, id, value); -} - -const char * -call_get_attribute(sip_call_t *call, enum sip_attr_id id) -{ - //char value[80]; - // if (!strcasecmp(name, "msgcnt")) { - // // FIXME REALLY - // sprintf(value, "%d", call_msg_count(call)); - // return strdup(value); - // } - // if (!strcasecmp(name, "starting")) { - // return msg_get_attribute(call_get_next_msg(call, NULL), "method"); - // } - return msg_get_attribute(call_get_next_msg(call, NULL), id); -} - void msg_set_attribute(sip_msg_t *msg, enum sip_attr_id id, const char *value) { diff --git a/src/sip.h b/src/sip.h index 24bc366..a1f62b6 100644 --- a/src/sip.h +++ b/src/sip.h @@ -52,15 +52,25 @@ typedef struct sip_attr sip_attr_t; */ enum sip_attr_id { + //! SIP Message From: header SIP_ATTR_SIPFROM = 1, + //! SIP Message To: header SIP_ATTR_SIPTO, + //! Package IP source address and port SIP_ATTR_SRC, + //! Package IP destiny address and port SIP_ATTR_DST, + //! SIP Message Call-ID header SIP_ATTR_CALLID, + //! SIP Message X-Call-ID or X-CID header SIP_ATTR_XCALLID, + //! SIP Message timestamp SIP_ATTR_TIME, + //! SIP Message Method or Response code SIP_ATTR_METHOD, + //! SIP Call first message method SIP_ATTR_STARTING, + //! SIP Call message counter SIP_ATTR_MSGCNT, }; @@ -169,6 +179,9 @@ sip_msg_create(const char *header, const char *payload); /** * @brief Create a new call with the given callid (Minimum required data) * + * Allocated required memory for a new SIP Call. The call acts as + * header structure to all the messages with the same callid. + * * @param callid Call-ID Header value * @return pointer to the sip_call created */ @@ -181,7 +194,7 @@ sip_call_create(char *callid); * Mainly used to check if a payload contains a callid. * * @param payload SIP message payload - * @returns callid parsed from Call-ID header + * @return callid parsed from Call-ID header */ extern char * sip_get_callid(const char* payload); @@ -199,27 +212,10 @@ sip_get_callid(const char* payload); extern sip_msg_t * sip_load_message(const char *header, const char *payload); -/** - * @brief Parse header and payload into a new message - * - * This function parses ngrep header and SIP message payload to - * fill a sip_message structure. - * - * If no call is found with the given Call-ID, a new one will be - * created and added to calls list. - * - * @param header ngrep header generated by -qpt arguments - * @param payload SIP message payload - * @returns the message structure @sip_msg or NULL if parsed failed - * - */ -extern sip_msg_t * -sip_parse_message(const char *header, const char *payload); - /** * @brief Getter for calls linked list size * - * @returns how many calls are linked in the list + * @return how many calls are linked in the list */ extern int sip_calls_count(); @@ -242,22 +238,66 @@ sip_check_call_ignore(sip_call_t *call); * Retrieve header data from attribute list * * @param id Attribute id + * @return Attribute header data structure pointer */ extern sip_attr_hdr_t * sip_attr_get_header(enum sip_attr_id id); +/** + * @brief Get Attribute description + * + * Retrieve description of given attribute from its + * header structure. + * + * @param id Attribut id + * @return Attribute description from its header + */ extern const char * sip_attr_get_description(enum sip_attr_id id); +/** + * @brief Get Attribute name + * + * Retrieve name of given attribute from its + * header structure. + * + * @param id Attribut id + * @return Attribute name from its header + */ extern const char * sip_attr_get_name(enum sip_attr_id id); +/** + * @brief Get Attribute id from its name + * + * Retrieve attribute id of the given attribute name. + * + * @param name Attribut name + * @return Attribute id or 0 if not found + */ extern enum sip_attr_id sip_attr_from_name(const char *name); +/** + * @brief Sets the given attribute value to an attribute + * + * Primitive for setting an attribute value of a given attribute list. + * This can be used for calls and message attributes. + * + * @param list Pointer to the attribute list + * @param id Attribute id + * @param value Attribute value + */ extern void sip_attr_set(sip_attr_t **list, enum sip_attr_id id, const char *value); +/** + * @brief Gets the given attribute value to an attribute + * + * Primitive for getting an attribute value of a given attribute list. + * This can be used for calls and message attributes. + * + */ extern const char * sip_attr_get(sip_attr_t *list, enum sip_attr_id id); @@ -276,8 +316,10 @@ call_add_message(sip_call_t *call, sip_msg_t *msg); /** * @brief Find a call structure in calls linked list given an callid * + * + * * @param callid Call-ID Header value - * @returns pointer to the sip_call structure found or NULL + * @return pointer to the sip_call structure found or NULL */ extern sip_call_t * call_find_by_callid(const char *callid); @@ -285,8 +327,11 @@ call_find_by_callid(const char *callid); /** * @brief Find a call structure in calls linked list given an xcallid * + * Find the call that have the xcallid attribute equal tot he given + * value. + * * @param xcallid X-Call-ID or X-CID Header value - * @returns pointer to the sip_call structure found or NULL + * @return pointer to the sip_call structure found or NULL */ extern sip_call_t * call_find_by_xcallid(const char *xcallid); @@ -294,7 +339,11 @@ call_find_by_xcallid(const char *xcallid); /** * @brief Getter for call messages linked list size * - * @returns how many messages are in the call + * Return the number of messages stored in this call. All messages + * share the same Call-ID + * + * @param call SIP call structure + * @return how many messages are in the call */ extern int call_msg_count(sip_call_t *call); @@ -307,7 +356,7 @@ call_msg_count(sip_call_t *call); * matching the given call's Call-ID will be find or returned. * * @param call SIP call structure - * @returns The other call structure or NULL if none found + * @return The other call structure or NULL if none found */ extern sip_call_t * call_get_xcall(sip_call_t *call); @@ -318,8 +367,9 @@ call_get_xcall(sip_call_t *call); * If the passed msg is NULL it returns the first message * in the call * + * @param call SIP call structure * @param msg Actual SIP msg from the call (can be NULL) - * @returns Next chronological message in the call + * @return Next chronological message in the call */ extern sip_msg_t * call_get_next_msg(sip_call_t *call, sip_msg_t *msg); @@ -332,7 +382,7 @@ call_get_next_msg(sip_call_t *call, sip_msg_t *msg); * * @param call SIP call structure * @param msg Actual SIP msg from the call (can be NULL) - * @returns Next chronological message in the conversation + * @return Next chronological message in the conversation * */ extern sip_msg_t * @@ -362,18 +412,27 @@ call_get_next(sip_call_t *cur); extern sip_call_t * call_get_prev(sip_call_t *cur); +/** + * @brief Sets the attribute value for a given call + * + * This function acts as wrapper of sip call attributes + * + * @param call SIP call structure + * @param id Attribute id + * @param value Attribute value + */ extern void call_set_attribute(sip_call_t *call, enum sip_attr_id id, const char *value); /** - * @brief Return a call value + * @brief Return a call attribute value * * This function will be used to avoid accessing call structure * fields directly. * @todo Code a proper way to store this information * * @param call SIP call structure - * @param attr Attribute id + * @param id Attribute id * @return Attribute value or NULL if not found */ extern const char * @@ -382,9 +441,16 @@ call_get_attribute(sip_call_t *call, enum sip_attr_id id); /** * @brief Parse ngrep header line to get timestamps and ip addresses * + * This function will convert the ngrep header line in format: + * U DD/MM/YY hh:mm:ss.uuuuuu fff.fff.fff.fff:pppp -> fff.fff.fff.fff:pppp + * + * to some attributes. + * + * @todo This MUST disappear someday. + * * @param msg SIP message structure * @param header ngrep header generated by -qpt arguments - * @returns 0 on success, 1 on malformed header + * @return 0 on success, 1 on malformed header */ extern int msg_parse_header(sip_msg_t *msg, const char *header); @@ -392,9 +458,11 @@ msg_parse_header(sip_msg_t *msg, const char *header); /** * @brief Parse SIP Message payload to fill sip_msg structe * + * Parse the payload content to set message attributes. + * * @param msg SIP message structure * @param payload SIP message payload - * @returns 0 in all cases + * @return 0 in all cases */ extern int msg_parse_payload(sip_msg_t *msg, const char *payload); @@ -412,9 +480,29 @@ msg_parse_payload(sip_msg_t *msg, const char *payload); extern sip_msg_t * msg_parse(sip_msg_t *msg); +/** + * @brief Sets the attribute value for a given message + * + * This function acts as wrapper of sip message attributes + * + * @param msg SIP message structure + * @param id Attribute id + * @param value Attribute value + */ extern void msg_set_attribute(sip_msg_t *msg, enum sip_attr_id id, const char *value); + +/** + * @brief Return a message attribute value + * + * This function will be used to avoid accessing call structure + * fields directly. + * + * @param msg SIP message structure + * @param id Attribute id + * @return Attribute value or NULL if not found + */ extern const char * msg_get_attribute(sip_msg_t *msg, enum sip_attr_id id); diff --git a/src/ui_call_list.h b/src/ui_call_list.h index 7a9097d..4ee9e87 100644 --- a/src/ui_call_list.h +++ b/src/ui_call_list.h @@ -158,8 +158,9 @@ call_list_help(PANEL *panel); * @todo Columns are not configurable yet. * * @param panel Ncurses panel pointer + * @param id SIP call attribute id * @param attr SIP call attribute name - * @param title Column Title + * @param title SIP call attribute description * @param width Column Width * @return 0 if column has been successufly added to the list, -1 otherwise */ diff --git a/src/ui_manager.h b/src/ui_manager.h index 1e5b06f..5d40abb 100644 --- a/src/ui_manager.h +++ b/src/ui_manager.h @@ -124,7 +124,6 @@ enum panel_types * This functions will initialize ncurses mode and show a * Call List panel. * - * @param ui_config UI configuration structure * @returns 0 on ncurses initialization success, 1 otherwise */ extern int @@ -238,10 +237,11 @@ toggle_color(int on); /** * @brief Wait for user input. + * * This function manages all user input in all panel types and * redraws the panel using its own draw function * - * @param panel the topmost panel ui structure + * @param ui the topmost panel ui structure */ extern int wait_for_input(ui_t *ui); @@ -267,7 +267,7 @@ title_foot_box(WINDOW *win); * callid. If the UI is displaying this call or it's * extended one, the topmost panel will be redraw again * - * @param callid Call-ID from the last received message + * @param msg Last readed message in Online mode */ extern void ui_new_msg_refresh(sip_msg_t *msg);