• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

raw_listener.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 iptelorg GmbH
00003  *
00004  * Permission to use, copy, modify, and distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00015  */
00021 /*
00022  * History:
00023  * --------
00024  *  2010-06-09  intial version (from older code) andrei
00025  */
00026 
00027 #ifdef USE_RAW_SOCKS
00028 
00029 
00030 #include "raw_listener.h"
00031 #include "raw_sock.h"
00032 #include "receive.h"
00033 
00034 #include <errno.h>
00035 #include <string.h>
00036 
00037 struct socket_info* raw_udp_sendipv4=0;
00038 
00047 int raw_listener_init(struct socket_info* si, str* iface, int iphdr_incl)
00048 {
00049         int sock;
00050         struct ip_addr ip;
00051         
00052         su2ip_addr(&ip, &si->su);
00053         sock=raw_udp4_socket(&ip, iface, iphdr_incl);
00054         if (sock>=0){
00055                 if (raw_udp_sendipv4==0 || iface==0 || iface->s==0)
00056                         raw_udp_sendipv4=si;
00057         }
00058         return sock;
00059 }
00060 
00061 
00062 
00073 int raw_udp4_rcv_loop(int rsock, int port1, int port2)
00074 {
00075         static char buf[BUF_SIZE+1];
00076         char* p;
00077         char* tmp;
00078         union sockaddr_union from;
00079         union sockaddr_union to;
00080         struct receive_info ri;
00081         struct raw_filter rf;
00082         int len;
00083         
00084         /* this will not change */
00085         from.sin.sin_family=AF_INET;
00086         ri.bind_address=0;
00087         ri.proto=PROTO_UDP;
00088         ri.proto_reserved1=0;
00089         ri.proto_reserved2=0;
00090         /* set filter to match any address but with the specified port range */
00091         memset(&rf, 0, sizeof(rf));
00092         rf.dst.ip.af=AF_INET;
00093         rf.dst.ip.len=4;
00094         rf.dst.mask.af=AF_INET;
00095         rf.dst.mask.len=4;
00096         rf.proto=PROTO_UDP;
00097         rf.port1=port1;
00098         rf.port2=port2?port2:port1;
00099         for(;;){
00100                 p=buf;
00101                 len=raw_udp4_recv(rsock, &p, BUF_SIZE, &from, &to, &rf);
00102                 if (len<0){
00103                         if (len==-1){
00104                                 LOG(L_ERR, "ERROR: raw_udp4_rcv_loop:raw_udp4_recv: %s [%d]\n",
00105                                                 strerror(errno), errno);
00106                                 if ((errno==EINTR)||(errno==EWOULDBLOCK))
00107                                         continue;
00108                                 else
00109                                         goto error;
00110                         }else{
00111                                 DBG("raw_udp4_rcv_loop: raw_udp4_recv error: %d\n", len);
00112                                 continue;
00113                         }
00114                 }
00115                 /* we must 0-term the message */
00116                 p[len]=0;
00117                 ri.src_su=from;
00118                 su2ip_addr(&ri.src_ip, &from);
00119                 ri.src_port=su_getport(&from);
00120                 su2ip_addr(&ri.dst_ip, &to);
00121                 ri.dst_port=su_getport(&to);
00122                 /* sanity checks */
00123                 if (len<MIN_UDP_PACKET){
00124                         tmp=ip_addr2a(&ri.src_ip);
00125                         DBG("raw_udp4_rcv_loop: probing packet received from %s %d\n",
00126                                         tmp, htons(ri.src_port));
00127                         continue;
00128                 }
00129                 if (ri.src_port==0){
00130                         tmp=ip_addr2a(&ri.src_ip);
00131                         LOG(L_INFO, "raw_udp4_rcv_loop: dropping 0 port packet from %s\n",
00132                                                 tmp);
00133                         continue;
00134                 }
00135                 tmp=ip_addr2a(&ri.src_ip);
00136                 DBG("raw_udp4_rcv_loop: received from %s:\n[%.*s]\n", tmp, len, p);
00137                 receive_msg(p, len, &ri);
00138         }
00139 error:
00140         return -1;
00141 }
00142 
00143 
00144 #endif /* USE_RAW_SOCKS */

Generated on Tue May 22 2012 13:10:13 for SIP Router by  doxygen 1.7.1