- removed 2 warnings on Solaris
[sip-router] / forward.c
index 1342656..64dc6bb 100644 (file)
--- a/forward.c
+++ b/forward.c
@@ -4,8 +4,11 @@
 
 
 #include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 
 #include "forward.h"
 #include "config.h"
 #include "route.h"
 #include "dprint.h"
 #include "udp_server.h"
+#include "globals.h"
 
 #define MAX_VIA_LINE_SIZE      240
 #define MAX_RECEIVED_SIZE  57
 
 
 
+/* checks if ip is in host(name) and ?host(ip)=name? 
+ * ip must be in network byte order!
+ *  resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
+ * return 0 if equal */
+int check_address(unsigned long ip, char *name, int resolver)
+{
+       struct hostent* he;
+       int i;
+       
+       /* maybe we are lucky and name it's an ip */
+       if (strcmp(name, inet_ntoa( *(struct in_addr *)&ip ))==0)
+               return 0;
+       if (resolver&DO_DNS){ 
+               /* try all names ips */
+               he=gethostbyname(name);
+               for(i=0; he->h_addr_list[i];i++){
+                       if (*(unsigned long*)he->h_addr_list[i]==ip)
+                               return 0;
+               }
+       }
+       if (resolver&DO_REV_DNS){
+               /* try reverse dns */
+               he=gethostbyaddr((char*)&ip, sizeof(ip), AF_INET);
+               if (strcmp(he->h_name, name)==0)
+                       return 0;
+               for (i=0; he->h_aliases[i];i++){
+                       if (strcmp(he->h_aliases[i],name)==0)
+                               return 0;
+               }
+       }
+       return -1;
+}
+
+
+
 int forward_request(char * orig, char* buf, 
                                         unsigned int len,
                                         struct sip_msg* msg,
-                                        struct route_elem* re)
+                                        struct route_elem* re,
+                                        unsigned long source_ip)
 {
        unsigned int new_len, via_len, received_len;
        char line_buf[MAX_VIA_LINE_SIZE];
@@ -34,11 +74,13 @@ int forward_request(char * orig, char* buf,
        received_len=0;
 
        via_len=snprintf(line_buf, MAX_VIA_LINE_SIZE, "Via: SIP/2.0/UDP %s:%d\r\n",
-                                               our_name, our_port);
+                                               names[0], port_no);
        /* check if received needs to be added */
-       /* if check_address(source_ip, msg->via1.host) */
-       received_len=snprintf(received_buf, MAX_RECEIVED_SIZE, ";received=%s",
-                                                       "10.11.12.13");
+       if (check_address(source_ip, msg->via1.host, received_dns)!=0){
+               received_len=snprintf(received_buf, MAX_RECEIVED_SIZE,
+                                                               ";received=%s", 
+                                                               inet_ntoa(*(struct in_addr *)&source_ip));
+       }
        
        new_len=len+via_len+received_len;
        new_buf=(char*)malloc(new_len+1);
@@ -118,13 +160,25 @@ int forward_reply(char * orig, char* buf,
 {
 
 
-       unsigned int new_len, via_len;
+       unsigned int new_len, via_len,r;
        char* new_buf;
        int offset, s_offset, size;
        struct hostent* he;
        struct sockaddr_in to;
 
+       new_buf=0;
 
+       /*check if first via host = us */
+       if (check_via){
+               for (r=0; r<addresses_no; r++)
+                       if(strcmp(msg->via1.host, names[r])==0) break;
+               if (r==addresses_no){
+                       DPrint("ERROR: forward_reply: host in first via != me : %s\n",
+                                       msg->via1.host);
+                       /* send error msg back? */
+                       goto error;
+               }
+       }
        /* we must remove the first via */
        via_len=msg->via1.size;
        size=msg->via1.hdr-buf;