core: move redundant and identical implementation of a str_list type to core
authorHenning Westerholt <henning.westerholt@1und1.de>
Tue, 5 Jul 2011 21:38:13 +0000 (23:38 +0200)
committerHenning Westerholt <henning.westerholt@1und1.de>
Tue, 5 Jul 2011 21:53:32 +0000 (23:53 +0200)
- move redundant str_list and new_str implementations from tm and tmx to core
- rename new_str to a more descriptive append_str_list, no interface changes
- fix callers in tm and tmx
- add doxygen documentation

modules/tm/rpc_uac.c
modules_k/tmx/t_mi.c
str_list.c [new file with mode: 0644]
str_list.h [new file with mode: 0644]

index 34f7c36..f1458a1 100644 (file)
@@ -29,6 +29,7 @@
 #include "../../socket_info.h"
 #include "../../ut.h"
 #include "../../parser/parse_from.h"
+#include "../../str_list.h"
 #include "ut.h"
 #include "dlg.h"
 #include "uac.h"
 
 
 
-struct str_list {
-       str s;
-       struct str_list *next;
-};
-
-
-
 /** make sure the rpc user created the msg properly.
  * Make sure that the FIFO user created the message
  * correctly and fill some extra parameters in function
@@ -151,25 +145,6 @@ err:
 
 
 
-static inline struct str_list *new_str(char *s, int len,
-                                                                               struct str_list **last, int *total)
-{
-       struct str_list *new;
-       new = pkg_malloc(sizeof(struct str_list));
-       if (!new) {
-               LOG(L_ERR, "new_str: Not enough mem\n");
-               return 0;
-       }
-       new->s.s = s;
-       new->s.len = len;
-       new->next = 0;
-
-       (*last)->next = new;
-       *last = new;
-       *total += len;
-       return new;
-}
-
 
 
 /** construct a "header block" from a header list.
@@ -208,7 +183,7 @@ static char *get_hfblock(str *uri, struct hdr_field *hf, int proto,
                while(p) {
                        d = q_memchr(needle, SUBST_CHAR, p);
                        if (!d || d + 1 >= needle + p) { /* nothing to substitute */
-                               if (!new_str(begin, p, &last, &total_len)) goto error;
+                               if (!append_str_list(begin, p, &last, &total_len)) goto error;
                                break;
                        } else {
                                frag_len = d - begin;
@@ -216,7 +191,7 @@ static char *get_hfblock(str *uri, struct hdr_field *hf, int proto,
                                switch(*d) {
                                case SUBST_CHAR: /* double SUBST_CHAR: IP */
                                             /* string before substitute */
-                                       if (!new_str(begin, frag_len, &last, &total_len))
+                                       if (!append_str_list(begin, frag_len, &last, &total_len))
                                                goto error;
                                             /* substitute */
                                        if (!sock_name) {
@@ -234,12 +209,12 @@ static char *get_hfblock(str *uri, struct hdr_field *hf, int proto,
                                                sock_name = &di.send_sock->address_str;
                                                portname = &di.send_sock->port_no_str;
                                        }
-                                       if (!new_str(sock_name->s, sock_name->len, &last,
+                                       if (!append_str_list(sock_name->s, sock_name->len, &last,
                                                                        &total_len))
                                                goto error;
                                        /* inefficient - FIXME --andrei*/
-                                       if (!new_str(":", 1, &last, &total_len)) goto error;
-                                       if (!new_str(portname->s, portname->len, &last,
+                                       if (!append_str_list(":", 1, &last, &total_len)) goto error;
+                                       if (!append_str_list(portname->s, portname->len, &last,
                                                                &total_len)) goto error;
                                        /* keep going ... */
                                        begin = needle = d + 1;
index 973061e..e784bf6 100644 (file)
 #include "../../parser/parse_from.h"
 #include "../../modules/tm/ut.h"
 #include "../../lib/kmi/mi.h"
+#include "../../str_list.h"
 #include "tmx_mod.h"
 
-/*! simple string list */
-struct str_list {
-       str s;
-       struct str_list *next;
-};
 
 /*! Which header fields should be skipped */
 #define skip_hf(_hf) \
@@ -160,35 +156,6 @@ static inline struct mi_root* mi_check_msg(struct sip_msg* msg, str* method,
        return 0;
 }
 
-/*!
- * \brief Allocate a new str on a str list
- *
- * Allocate a new str in pkg_mem and attach it to a str list. Update
- * the total number of list elements.
- * \param s char array
- * \param len length of the char array
- * \param last last list element
- * \param total total number of list elements
- * \return pointer to the new list element
- */
-static inline struct str_list *new_str(char *s, int len, struct str_list **last, int *total)
-{
-       struct str_list *new;
-       new=pkg_malloc(sizeof(struct str_list));
-       if (!new) {
-               LM_ERR("no more pkg mem\n");
-               return 0;
-       }
-       new->s.s=s;
-       new->s.len=len;
-       new->next=0;
-
-       (*last)->next=new;
-       *last=new;
-       *total+=len;
-
-       return new;
-}
 
 /*!
  * \brief Convert a header field block to char array
@@ -225,7 +192,7 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
                while(hf_avail) {
                        d=memchr(needle, SUBST_CHAR, hf_avail);
                        if (!d || d+1>=needle+hf_avail) { /* nothing to substitute */
-                               new=new_str(begin, hf_avail, &last, &total_len); 
+                               new=append_str_list(begin, hf_avail, &last, &total_len); 
                                if (!new) goto error;
                                break;
                        } else {
@@ -234,7 +201,7 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
                                switch(*d) {
                                        case SUBST_CHAR:        /* double SUBST_CHAR: IP */
                                                /* string before substitute */
-                                               new=new_str(begin, frag_len, &last, &total_len); 
+                                               new=append_str_list(begin, frag_len, &last, &total_len); 
                                                if (!new) goto error;
                                                /* substitute */
                                                if (!sock_name) {
@@ -248,13 +215,13 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
                                                        sock_name=&(*send_sock)->address_str;
                                                        portname=&(*send_sock)->port_no_str;
                                                }
-                                               new=new_str(sock_name->s, sock_name->len,
+                                               new=append_str_list(sock_name->s, sock_name->len,
                                                                &last, &total_len );
                                                if (!new) goto error;
                                                /* inefficient - FIXME --andrei*/
-                                               new=new_str(":", 1, &last, &total_len);
+                                               new=append_str_list(":", 1, &last, &total_len);
                                                if (!new) goto error;
-                                               new=new_str(portname->s, portname->len,
+                                               new=append_str_list(portname->s, portname->len,
                                                                &last, &total_len );
                                                if (!new) goto error;
                                                /* keep going ... */
@@ -268,7 +235,7 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
                        } /* possible substitute */
                } /* substitution loop */
                /* proceed to next header */
-               /* new=new_str(CRLF, CRLF_LEN, &last, &total_len );
+               /* new=append_str_list(CRLF, CRLF_LEN, &last, &total_len );
                if (!new) goto error; */
                LM_DBG("one more hf processed\n");
        } /* header loop */
diff --git a/str_list.c b/str_list.c
new file mode 100644 (file)
index 0000000..a51bffa
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2001-2003 FhG Fokus
+ *
+ * This file is part of sip-router, a free SIP server.
+ *
+ * sip-router 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
+ *
+ * sip-router 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/**
+ * @file 
+ * @brief Simple str type list and helper functions
+ */
+
+
+#include "str.h"
+#include "mem/mem.h"
+#include "str_list.h"
+
+
+/**
+ * @brief Add a new allocated list element to an existing list
+ * 
+ * Add a new allocated list element to an existing list, the allocation is done
+ * from the private memory pool
+ * @param s input character
+ * @param len length of input character
+ * @param last existing list
+ * @param total length of total characters in list
+ * @return extended list
+ */
+struct str_list *append_str_list(char *s, int len, struct str_list **last, int *total)
+{
+       struct str_list *new;
+       new = pkg_malloc(sizeof(struct str_list));
+       if (!new) {
+               PKG_MEM_ERROR;
+               return 0;
+       }
+       new->s.s = s;
+       new->s.len = len;
+       new->next = 0;
+
+       (*last)->next = new;
+       *last = new;
+       *total += len;
+       return new;
+}
diff --git a/str_list.h b/str_list.h
new file mode 100644 (file)
index 0000000..6373d68
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2006 Voice Sistem SRL
+ *
+ * This file is part of sip-router, a free SIP server.
+ *
+ * sip-router 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
+ *
+ * sip-router 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/**
+ * @file 
+ * @brief Simple str type list and helper functions
+ */
+
+
+#ifndef str_list_h
+#define str_list_h
+
+
+/**
+ * @brief Simple str type list
+ */
+struct str_list {
+       str s;
+       struct str_list *next;
+};
+
+
+/**
+ * @brief Add a new allocated list element to an existing list
+ * 
+ * Add a new allocated list element to an existing list, the allocation is done
+ * from the private memory pool
+ * @param s input character
+ * @param len length of input character
+ * @param last existing list
+ * @param total length of total characters in list
+ * @return extended list
+ */
+struct str_list *append_str_list(char *s, int len, struct str_list **last, int *total);
+
+#endif