Browse Source

- small but essential fixes

Andrei Pelinescu-Onciul 24 years ago
parent
commit
21f03122ff
12 changed files with 214 additions and 79 deletions
  1. 2 2
      action.c
  2. 2 2
      main.c
  3. 3 3
      mem.h
  4. 1 1
      modules/tm/Makefile
  5. 2 0
      modules/tm/h_table.h
  6. 1 1
      modules/tm/hash_func.c
  7. 24 14
      modules/tm/t_funcs.c
  8. 5 4
      modules/tm/t_funcs.h
  9. 149 38
      modules/tm/tm.c
  10. 7 1
      msg_parser.c
  11. 6 1
      msg_parser.h
  12. 12 12
      shm_mem.h

+ 2 - 2
action.c

@@ -342,8 +342,8 @@ int do_action(struct action* a, struct sip_msg* msg)
 				}
 			break;
 		case MODULE_T:
-			if ( ((a->p1_type==CMDF_ST)&&a->p1.data)&&
-					((a->p2_type==STRING_ST)&&a->p2.data) ){
+			if ( ((a->p1_type==CMDF_ST)&&a->p1.data)/*&&
+					((a->p2_type==STRING_ST)&&a->p2.data)*/ ){
 				ret=((cmd_function)(a->p1.data))(msg, (char*)a->p2.data,
 													  (char*)a->p3.data);
 			}else{

+ 2 - 2
main.c

@@ -300,7 +300,7 @@ static void sig_usr(int signo)
 		pkg_status();
 #endif
 #ifdef SHM_MEM
-		sh_status();
+		shm_status();
 #endif
 		DPrint("INT received, program terminates\n");
 		DPrint("Thank you for flying ser\n");
@@ -317,7 +317,7 @@ static void sig_usr(int signo)
 		pkg_status();
 #endif
 #ifdef SHM_MEM
-		sh_status();
+		shm_status();
 #endif
 	}
 }

+ 3 - 3
mem.h

@@ -23,9 +23,9 @@ extern struct qm_block* mem_block;
 
 #include "shm_mem.h"
 
-#define pkg_malloc(s) sh_malloc(s)
-#define pkg_free(p)   sh_free(p)
-#define pkg_status()  sh_status()
+#define pkg_malloc(s) shm_malloc(s)
+#define pkg_free(p)   shm_free(p)
+#define pkg_status()  shm_status()
 
 #else
 

+ 1 - 1
modules/tm/Makefile

@@ -13,7 +13,7 @@ depends= $(sources:.c=.d)
 NAME=tm.so
 
 SHLIB_LD_FLAGS= -shared
-LIBS= -lpthread
+LIBS= #-lpthread
 
 ifeq ($(ARCH), SunOS)
 LDFLAGS=-O2 -G

+ 2 - 0
modules/tm/h_table.h

@@ -19,8 +19,10 @@ struct timer;
 #include "lock.h"
 #include "sip_msg.h"
 
+
 #define sh_malloc( size )     malloc(size)
 #define sh_free( ptr )           free(ptr)
+
 /* defined in msg_parser.h
 #define get_cseq( p_msg)    ((struct cseq_body*)p_msg->cseq->parsed)
 */

+ 1 - 1
modules/tm/hash_func.c

@@ -1,10 +1,10 @@
 #include "hash_func.h"
+#include "../../dprint.h"
 
 int hash( str  call_id, str cseq_nr )
 {
    int  hash_code = 0;
    int  i;
-
     if ( call_id.len>0 )
       for( i=0 ; i<call_id.len ; hash_code+=call_id.s[i++]  );
     if ( cseq_nr.len>0 )

+ 24 - 14
modules/tm/t_funcs.c

@@ -23,7 +23,7 @@ int tm_startup()
 
    /*first msg id*/
    global_msg_id = 0;
-   T = 0;
+   T = (struct cell*)-1;
 
    return 0;
 }
@@ -47,7 +47,7 @@ int tm_shutdown()
  *      -1 - retransmission
  *      -2 - error
  */
-int t_add_transaction( struct sip_msg* p_msg )
+int t_add_transaction( struct sip_msg* p_msg, char* foo, char* bar )
 {
    struct cell*    new_cell;
 
@@ -61,7 +61,7 @@ int t_add_transaction( struct sip_msg* p_msg )
     /* if the transaction is not found yet we are tring to look for it*/
    if ( (int)T==-1 )
       /* if the lookup's result is not 0 means that it's a retransmission */
-      if ( t_lookup_request( p_msg ) )	{
+      if ( t_lookup_request( p_msg, foo, bar ) )	{
 	 DBG("DEBUG: t_add_transaction: won't add a retransmission\n");
          return -1;
       }
@@ -84,7 +84,7 @@ int t_add_transaction( struct sip_msg* p_msg )
  *       0 - transaction wasn't found
  *       1 - transaction found
  */
-int t_lookup_request( struct sip_msg* p_msg )
+int t_lookup_request( struct sip_msg* p_msg, char* foo, char* bar  )
 {
    struct cell      *p_cell;
    struct cell      *tmp_cell;
@@ -110,10 +110,19 @@ int t_lookup_request( struct sip_msg* p_msg )
       return 0;
    }
 
+   DBG("t_lookup_request: start searching\n");
+   /* parse all*/
+   if (check_transaction_quadruple(p_msg)==0) {
+	   LOG(L_ERR, "ERROR: TM module: t_lookup_request: too few headers\n");
+	   T=0;
+	   return -1;
+   }
    /* start searching into the table */
-   hash_index = hash( p_msg->callid , get_cseq(p_msg)->number ) ;
+   hash_index = hash( p_msg->callid->body , get_cseq(p_msg)->number ) ;
+   DBG("hash_index=%d\n", hash_index);
    if ( p_msg->first_line.u.request.method_value==METHOD_ACK  )
       isACK = 1;
+   DBG("t_lookup_request: 1.continue searching\n");
 
    /* all the transactions from the entry are compared */
    p_cell     = hash_table->entrys[hash_index].first_cell;
@@ -195,11 +204,12 @@ int t_forward( struct sip_msg* p_msg , unsigned int dest_ip_param , unsigned int
       T = (struct cell*)-1;
       global_msg_id = p_msg->id;
    }
-
+	DBG("t_forward: 1. T=%x\n", T);
    /* if  T hasn't been previous searched -> search for it */
-   if ( (int)T !=-1 )
-      t_lookup_request( p_msg );
+   if ( (int)T ==-1 )
+      t_lookup_request( p_msg, 0 , 0 );
 
+	DBG("t_forward: 2. T=%x\n", T);
    /*if T hasn't been found after all -> return not found (error) */
    if ( !T )
       return -1;
@@ -253,8 +263,8 @@ int t_forward( struct sip_msg* p_msg , unsigned int dest_ip_param , unsigned int
       T->outbound_request[branch]->dest_ip         = dest_ip;
       T->outbound_request[branch]->dest_port      = dest_port;
       T->outbound_request[branch]->to.sin_family = AF_INET;
-      T->outbound_request[branch]->to.sin_port     = htonl( dest_port ) ;
-      T->outbound_request[branch]->to.sin_addr.s_addr = ntohl( dest_ip ) ;
+      T->outbound_request[branch]->to.sin_port     =  dest_port;
+      T->outbound_request[branch]->to.sin_addr.s_addr =  dest_ip;
 
       if (add_branch_label( T, p_msg , branch )==-1) return -1;
       buf = build_req_buf_from_sip_req  ( p_msg, &len);
@@ -408,7 +418,7 @@ int t_put_on_wait(  struct sip_msg  *p_msg  )
   * Returns  -1 -error
   *                0 - OK
   */
-int t_retransmit_reply( struct sip_msg* p_msg )
+int t_retransmit_reply( struct sip_msg* p_msg, char* foo, char* bar  )
 {
    t_check( hash_table, p_msg );
 
@@ -486,7 +496,7 @@ int t_send_reply(  struct sip_msg* p_msg , unsigned int code , char * text )
          insert_into_timer_list( hash_table , &(T->inbound_response->tl[FR_TIMER_LIST]) , FR_TIMER_LIST , FR_TIME_OUT );
       }
 
-      t_retransmit_reply( p_msg );
+      t_retransmit_reply( p_msg, 0 , 0);
    }
 }
 
@@ -723,7 +733,7 @@ int t_check( struct s_table *hash_table , struct sip_msg* p_msg )
       global_msg_id = p_msg->id;
       /* transaction lookup */
      if ( p_msg->first_line.type=SIP_REQUEST )
-         t_lookup_request( p_msg );
+         t_lookup_request( p_msg, 0, 0 );
       else
          t_reply_matching( hash_table , p_msg , &T , &branch );
 
@@ -837,7 +847,7 @@ int push_reply_from_uac_to_uas( struct sip_msg *p_msg , unsigned int branch )
             insert_into_timer_list( hash_table , &(T->inbound_response->tl[FR_TIMER_LIST]) , FR_TIMER_LIST , FR_TIME_OUT );
          }
 
-   t_retransmit_reply( p_msg );
+   t_retransmit_reply( p_msg, 0 , 0 );
 }
 
 

+ 5 - 4
modules/tm/t_funcs.h

@@ -38,21 +38,22 @@ int tm_shutdown();
  *      -1 - retransmission
  *      -2 - error
  */
-int  t_add_transaction( struct sip_msg* p_msg );
+int  t_add_transaction( struct sip_msg* p_msg, char* foo, char* bar  );
 
 
 /* function returns:
  *       0 - transaction wasn't found
  *       1 - transaction found
  */
-int  t_lookup_request( struct sip_msg* p_msg );
+int  t_lookup_request( struct sip_msg* p_msg, char* foo, char* bar  );
 
 
 /* function returns:
  *       0 - forward successfull
  *      -1 - error during forward
  */
-int t_forward( struct sip_msg* p_msg , unsigned int dst_ip , unsigned int dst_port);
+int t_forward( struct sip_msg* p_msg , unsigned int dst_ip ,
+				unsigned int dst_port);
 
 
 
@@ -75,7 +76,7 @@ int t_put_on_wait(  struct sip_msg  *p_msg  );
 
 /* Retransmits the last sent inbound reply.
   */
-int t_retransmit_reply( struct sip_msg *  );
+int t_retransmit_reply( struct sip_msg *, char* , char* );
 
 
 

+ 149 - 38
modules/tm/tm.c

@@ -1,6 +1,6 @@
 /*$Id$
  *
- * Example ser module, it will just print its string parameter to stdout
+ * TM module
  *
  */
 
@@ -8,64 +8,174 @@
 
 #include "../../sr_module.h"
 #include "../../dprint.h"
+#include "../../error.h"
+#include "../../ut.h"
 #include "sip_msg.h"
 #include <stdio.h>
-#include "h_table.h"
+#include <string.h>
+#include <netdb.h>
 
-static int test_f(struct sip_msg*, char*,char*);
+#include "h_table.h"
+#include "t_funcs.h"
+
+
+
+/*static int test_f(struct sip_msg*, char*,char*);*/
+static int w_t_send_reply(struct sip_msg* msg, char* str, char* str2);
+static int w_t_forward(struct sip_msg* msg, char* str, char* str2);
+static int t_forward_uri(struct sip_msg* msg, char* str, char* str2);
+static int fixup_t_forward(void** param, int param_no);
+static int fixup_t_send_reply(void** param, int param_no);
+
+static struct module_exports nm_exports= {
+	"tm_module", 
+	(char*[]){	"t_add_transaction",
+				"t_lookup_request",
+				"t_forward",
+				"t_forward_uri",
+				"t_send_reply",
+				"t_retransmit_reply"
+			},
+	(cmd_function[]){
+					t_add_transaction,
+					t_lookup_request,
+					w_t_forward,
+					t_forward_uri,
+					w_t_send_reply,
+					t_retransmit_reply,
+					},
+	(int[]){
+				0,
+				0,
+				2,
+				0,
+				2,
+				0
+			},
+	(fixup_function[]){
+				0,
+				0,
+				fixup_t_forward,
+				0,
+				fixup_t_send_reply,
+				0
+		},
+	6,
+	(response_function) t_on_reply_received
+};
 
-static struct module_exports nm_exports= {	"tm_module", 
-												(char*[]){"tm_test"},
-												(cmd_function[]){test_f},
-												(int[]){1},
-												(fixup_function[]){0},
-												1,
-												0
-											};
 
 
 struct module_exports* mod_register()
 {
 
-/* Jiri's testing playground
+	DBG( "TM - registering...\n");
+	if (tm_startup()==-1) return 0;
+	return &nm_exports;
+}
 
-	struct s_table *hash_table;
-	int p,i,j,k;
 
-	DBG("tm module registering\n");
-	DBG("tm init table\n");
-	hash_table = init_hash_table( );
 
-#define DELAY for(i=0,j=0;i<100000;i++) j=j+3*i+1;
+static int fixup_t_forward(void** param, int param_no)
+{
+	char* name;
+	struct hostent* he;
+	unsigned int port;
+	int err;
+#ifdef DNS_IP_HACK
+	unsigned int ip;
+	int len;
+#endif
+
+	DBG("TM module: fixup_t_forward(%s, %d)\n", (char*)*param, param_no);
+	if (param_no==1){
+		name=*param;
+#ifdef DNS_IP_HACK
+		len=strlen(name);
+		ip=str2ip(name, len, &err);
+		if (err==0){
+			goto copy;
+		}
+#endif
+		/* fail over to normal lookup */
+		he=gethostbyname(name);
+		if (he==0){
+			LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
+						" \"%s\"\n", name);
+			return E_BAD_ADDRESS;
+		}
+		memcpy(&ip, he->h_addr_list[0], sizeof(unsigned int));
+	copy:
+		free(*param);
+		*param=(void*)ip;
+		return 0;
+	}else if (param_no==2){
+		port=htons(str2s(*param, strlen(*param), &err));
+		if (err==0){
+			free(*param);
+			*param=(void*)port;
+			return 0;
+		}else{
+			LOG(L_ERR, "TM module:fixup_t_forward: bad port number <%s>\n",
+					*param);
+			return E_UNSPEC;
+		}
+	}
+	return 0;
+}
+
+
 
-	p=fork();
-	sleep(2);
-	for(k=0;k<50;k++){	
-		DBG("DEBUG: trying to lock %d in\n", p );
-		lock( hash_table->entrys[1].mutex );
-		DBG("DEBUG: lock %d in\n", p );
-		DELAY; DELAY; DELAY; DELAY; DELAY;
-		DELAY; DELAY; DELAY; DELAY; DELAY;
-		unlock( hash_table->entrys[1].mutex );
-		DBG("DEBUG: lock %d out\n", p );
-		DELAY
+static int fixup_t_send_reply(void** param, int param_no)
+{
+	unsigned int code;
+	int err;
+
+	if (param_no==1){
+		code=str2s(*param, strlen(*param), &err);
+		if (err==0){
+			free(*param);
+			*param=(void*)code;
+			return 0;
+		}else{
+			LOG(L_ERR, "TM module:fixup_t_send_reply: bad  number <%s>\n",
+					*param);
+			return E_UNSPEC;
+		}
 	}
+	/* second param => no conversion*/
+	return 0;
+}
 
-	DBG("tm free table\n");
-	free_hash_table( hash_table );
-*/
-	fprintf(stderr, "nm - registering...\n");
-	return &nm_exports;
+
+
+static int t_forward_uri(struct sip_msg* msg, char* str, char* str2)
+{
+
+	LOG(L_CRIT, "BUG: TM module: t_forwad_uri not implemented!");
+	return -1;
+}
+
+
+
+static int w_t_forward(struct sip_msg* msg, char* str, char* str2)
+{
+	DBG("TM module: w_t_forward entered\n");
+	DBG("TM module: w_t_forward params: %x %d\n", 
+			 str, (unsigned int)str2);
+	return t_forward(msg, (unsigned int) str, (unsigned int) str2);
 }
 
 
-static int print_f(struct sip_msg* msg, char* str, char* str2)
+
+static int w_t_send_reply(struct sip_msg* msg, char* str, char* str2)
 {
-	/*we registered only 1 param, so we ignore str2*/
-	printf("%s\n",str);
-	return 1;
+	return t_send_reply(msg, (unsigned int) str, str2);
 }
 
+
+
+#if 0
 static int test_f(struct sip_msg* msg, char* s1, char* s2)
 {
 	struct sip_msg* tst;
@@ -113,5 +223,6 @@ static int test_f(struct sip_msg* msg, char* s1, char* s2)
 	free(tst);
 }
 
+#endif
 
 

+ 7 - 1
msg_parser.c

@@ -948,16 +948,22 @@ void free_sip_msg(struct sip_msg* msg)
 }
 
 
+#if 0
+/* it's a macro now*/
 /* make sure all HFs needed for transaction identification have been
    parsed; return 0 if those HFs can't be found
 */
 int check_transaction_quadruple( struct sip_msg* msg )
 {
    return 
+	(parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ)!=-1 &&
+	 msg->from && msg->to && msg->callid && msg->cseq);
+  /* replaced by me ( :) andrei)
    ( (msg->from || (parse_headers( msg, HDR_FROM)!=-1 && msg->from)) &&
    (msg->to|| (parse_headers( msg, HDR_TO)!=-1 && msg->to)) &&
    (msg->callid|| (parse_headers( msg, HDR_CALLID)!=-1 && msg->callid)) &&
    (msg->cseq|| (parse_headers( msg, HDR_CSEQ)!=-1 && msg->cseq)) ) ? 1 : 0;
+  */
 
 }
-
+#endif

+ 6 - 1
msg_parser.h

@@ -222,7 +222,12 @@ void clean_hdr_field(struct hdr_field* hf);
 void free_hdr_field_lst(struct hdr_field* hf);
 void free_sip_msg(struct sip_msg* msg);
 
-int check_transaction_quadruple( struct sip_msg* msg );
+/* make sure all HFs needed for transaction identification have been
+   parsed; return 0 if those HFs can't be found
+ */
+#define check_transaction_quadruple(msg ) \
+	(parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ)!=-1 && \
+	 msg->from && msg->to && msg->callid && msg->cseq)
 
 
 #endif

+ 12 - 12
shm_mem.h

@@ -30,7 +30,7 @@ void shm_mem_destroy();
 
 
 
-inline static void sh_lock()
+inline static void shm_lock()
 {
 	struct sembuf sop;
 	
@@ -55,7 +55,7 @@ again:
 
 
 
-inline static void sh_unlock()
+inline static void shm_unlock()
 {
 	struct sembuf sop;
 	
@@ -80,14 +80,14 @@ again:
 }
 
 
-inline static void* sh_malloc(unsigned int size)
+inline static void* shm_malloc(unsigned int size)
 {
 	void *p;
 	
-	/*if (sh_lock()==0){*/
-		sh_lock();
+	/*if (shm_lock()==0){*/
+		shm_lock();
 		p=qm_malloc(shm_block, size);
-		sh_unlock();
+		shm_unlock();
 	/*
 	}else{
 		p=0;
@@ -97,20 +97,20 @@ inline static void* sh_malloc(unsigned int size)
 
 
 
-#define sh_free(p) \
+#define shm_free(p) \
 do { \
-		sh_lock(); \
+		shm_lock(); \
 		qm_free(shm_block, p); \
-		sh_unlock(); \
+		shm_unlock(); \
 }while(0)
 
 
 
-#define sh_status() \
+#define shm_status() \
 do { \
-		sh_lock(); \
+		shm_lock(); \
 		qm_status(shm_block); \
-		sh_unlock(); \
+		shm_unlock(); \
 }while(0)