core: more debug messages when matching myself
authorDaniel-Constantin Mierla <miconda@gmail.com>
Sat, 2 May 2020 06:46:28 +0000 (08:46 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Sat, 2 May 2020 06:47:30 +0000 (08:47 +0200)
- coherent coding style for related functions

src/core/forward.c
src/core/name_alias.h
src/core/socket_info.c

index 7129af8..716b148 100644 (file)
@@ -407,15 +407,23 @@ int run_check_self_func(str* host, unsigned short port, unsigned short proto)
  */
 int check_self(str* host, unsigned short port, unsigned short proto)
 {
-       if (grep_sock_info(host, port, proto)) goto found;
+       int ret = 1;
+       if (grep_sock_info(host, port, proto)) {
+               goto done;
+       }
        /* try to look into the aliases*/
-       if (grep_aliases(host->s, host->len, port, proto)==0){
-               LM_DBG("host != me\n");
-               return (_check_self_func_list==NULL)?0:run_check_self_func(host,
-                                                                                                               port, proto);
+       if (grep_aliases(host->s, host->len, port, proto)==0) {
+               ret = (_check_self_func_list==NULL)?0:run_check_self_func(host,
+                                       port, proto);
        }
-found:
-       return 1;
+
+done:
+       if(ret==1) {
+               LM_DBG("host (%d:%.*s:%d) == me\n", proto, host->len, host->s, port);
+       } else {
+               LM_DBG("host (%d:%.*s:%d) != me\n", proto, host->len, host->s, port);
+       }
+       return ret;
 }
 
 /** checks if the proto:port is one of the ports we listen on;
@@ -424,11 +432,14 @@ found:
  */
 int check_self_port(unsigned short port, unsigned short proto)
 {
-       if (grep_sock_info_by_port(port, proto))
-               /* as aliases do not contain different ports we can skip them */
+       /* aliases do not contain different ports we can skip them */
+       if (grep_sock_info_by_port(port, proto)) {
+               LM_DBG("proto:port (%d:%d) == me\n", proto, port);
                return 1;
-       else
+       } else {
+               LM_DBG("proto:port (%d:%d) != me\n", proto, port);
                return 0;
+       }
 }
 
 
index 7efb571..3dd6066 100644 (file)
@@ -14,8 +14,8 @@
  * 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 
+ * 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 "mem/mem.h"
 
 
-
-struct host_alias{
+typedef struct host_alias{
        str alias;
        unsigned short port;
        unsigned short proto;
        struct host_alias* next;
-};
+} host_alias_t;
 
 
 extern struct host_alias* aliases;
 
 
-
 /** returns 1 if  name is in the alias list; if port=0, port no is ignored
  * if proto=0, proto is ignored*/
 static inline int grep_aliases(char* name, int len, unsigned short port,
                                                                unsigned short proto)
 {
-       struct  host_alias* a;
-       
-       if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
+       struct host_alias* a;
+
+       if ((len>2)&&((*name)=='[')&&(name[len-1]==']')) {
                /* ipv6 reference, skip [] */
                name++;
                len-=2;
        }
-       for(a=aliases;a;a=a->next)
-               if ((a->alias.len==len) && ((a->port==0) || (port==0) || 
-                               (a->port==port)) && ((a->proto==0) || (proto==0) || 
-                               (a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0))
+       for(a=aliases;a;a=a->next) {
+               LM_DBG("matching (%d:%.*s:%d) vs. (%d:%.*s:%d)\n",
+                               proto, len, name, port, a->proto, a->alias.len, a->alias.s,
+                               a->port);
+               if ((a->alias.len==len) && ((a->port==0) || (port==0) ||
+                               (a->port==port)) && ((a->proto==0) || (proto==0) ||
+                               (a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0)) {
                        return 1;
+               }
+       }
        return 0;
 }
 
 
-
 /** adds an alias to the list (only if it isn't already there)
  * if port==0, the alias will match all the ports
  * if proto==0, the alias will match all the protocols
  * returns 1 if a new alias was added, 0 if a matching alias was already on
  * the list and  -1 on error */
-static inline int add_alias(char* name, int len, unsigned short port, 
+static inline int add_alias(char* name, int len, unsigned short port,
                                                                unsigned short proto)
 {
        struct host_alias* a;
-       
-       if ((port) && (proto)){
+
+       if ((port) && (proto)) {
                /* don't add if there is already an alias matching it */
-               if (grep_aliases(name,len, port, proto)) return 0;
-       }else{
+               if (grep_aliases(name,len, port, proto)) {
+                       return 0;
+               }
+       } else {
                /* don't add if already in the list with port or proto ==0*/
-               for(a=aliases;a;a=a->next)
-                       if ((a->alias.len==len) && (a->port==port) && (a->proto==proto) &&
-                                       (strncasecmp(a->alias.s, name, len)==0))
+               for(a=aliases;a;a=a->next) {
+                       if ((a->alias.len==len) && (a->port==port) && (a->proto==proto)
+                                       && (strncasecmp(a->alias.s, name, len)==0)) {
                                return 0;
+                       }
+               }
        }
        a=(struct host_alias*)pkg_malloc(sizeof(struct host_alias));
-       if(a==0) goto error;
+       if(a==0) {
+               goto error;
+       }
        a->alias.s=(char*)pkg_malloc(len+1);
-       if (a->alias.s==0) goto error;
+       if (a->alias.s==0) {
+               goto error;
+       }
        a->alias.len=len;
        memcpy(a->alias.s, name, len);
        a->alias.s[len]=0; /* null terminate for easier printing*/
@@ -104,10 +114,10 @@ static inline int add_alias(char* name, int len, unsigned short port,
        return 1;
 error:
        PKG_MEM_ERROR;
-       if (a) pkg_free(a);
+       if (a) {
+               pkg_free(a);
+       }
        return -1;
 }
 
-
-
-#endif
\ No newline at end of file
+#endif
index 16a7491..d2e1149 100644 (file)
@@ -619,23 +619,25 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
        struct socket_info** list;
        struct addr_info* ai;
        unsigned short c_proto;
-       
+
        hname=*host;
-       if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
-               /* ipv6 reference, skip [] */
+       if ((hname.len>2) && ((*hname.s)=='[') && (hname.s[hname.len-1]==']')) {
+               /* ipv6 - skip [] */
                hname.s++;
                hname.len-=2;
        }
 
        c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
 retry:
-       do{
+       do {
                /* get the proper sock_list */
                list=get_sock_info_list(c_proto);
-       
-               if (list==0) /* disabled or unknown protocol */
+
+               if (list==0) {
+                       /* disabled or unknown protocol */
                        continue;
-               for (si=*list; si; si=si->next){
+               }
+               for (si=*list; si; si=si->next) {
                        LM_DBG("checking if host==us: %d==%d && [%.*s] == [%.*s]\n",
                                                hname.len,
                                                si->name.len,
@@ -649,31 +651,34 @@ retry:
                                        continue;
                                }
                        }
-                       if (si_hname_cmp(&hname, &si->name, &si->address_str, 
-                                                               &si->address, si->flags)==0)
+                       if (si_hname_cmp(&hname, &si->name, &si->address_str,
+                                                               &si->address, si->flags)==0) {
                                goto found;
-                       if(si->useinfo.name.s!=NULL)
-                       {
+                       }
+                       if(si->useinfo.name.s!=NULL) {
                                LM_DBG("checking advertise if host==us:"
                                                " %d==%d && [%.*s] == [%.*s]\n",
                                                hname.len,
                                                si->useinfo.name.len,
                                                hname.len, hname.s,
                                                si->useinfo.name.len, si->useinfo.name.s
-                               );
+                                       );
                                if (si_hname_cmp(&hname, &si->useinfo.name,
                                                        &si->useinfo.address_str, &si->useinfo.address,
-                                                       si->flags)==0)
+                                                       si->flags)==0) {
                                        goto found;
+                               }
                        }
                        /* try among the extra addresses */
-                       for (ai=si->addr_info_lst; ai; ai=ai->next)
-                               if (si_hname_cmp(&hname, &ai->name, &ai->address_str, 
-                                                                       &ai->address, ai->flags)==0)
+                       for (ai=si->addr_info_lst; ai; ai=ai->next) {
+                               if (si_hname_cmp(&hname, &ai->name, &ai->address_str,
+                                                                       &ai->address, ai->flags)==0) {
                                        goto found;
+                               }
+                       }
                }
 
-       }while( (proto==0) && (c_proto=next_proto(c_proto)) );
+       } while( (proto==0) && (c_proto=next_proto(c_proto)) );
 
 #ifdef USE_TLS
        if (unlikely(c_proto == PROTO_WS)) {
@@ -726,8 +731,8 @@ socket_info_t* ksr_get_socket_by_name(str *sockname)
  * if proto==0 (PROTO_NONE) the protocol is ignored
  * returns  0 if not found
  */
-struct socket_info* grep_sock_info_by_port(unsigned short port, 
-                                                                                       unsigned short proto)
+struct socket_info* grep_sock_info_by_port(unsigned short port,
+               unsigned short proto)
 {
        struct socket_info* si;
        struct socket_info** list;
@@ -737,22 +742,24 @@ struct socket_info* grep_sock_info_by_port(unsigned short port,
                goto not_found;
        }
        c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
-       do{
+       do {
                /* get the proper sock_list */
                list=get_sock_info_list(c_proto);
-       
+
                if (list==0) /* disabled or unknown protocol */
                        continue;
-               
+
                for (si=*list; si; si=si->next){
                        LM_DBG("checking if port %d matches port %d\n", si->port_no, port);
                        if (si->port_no==port) {
                                goto found;
                        }
                }
-       }while( (proto==0) && (c_proto=next_proto(c_proto)) );
+       } while( (proto==0) && (c_proto=next_proto(c_proto)) );
+
 not_found:
        return 0;
+
 found:
        return si;
 }