10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <sys/utsname.h>
18 #include "udp_server.h"
22 static char id[]="@(#) $Id$";
23 static char version[]="sip_router 0.3";
24 static char help_msg[]= "\
25 Usage: sip_router -l address [-l address] [options]\n\
27 -f file Configuration file (default " CFG_FILE ")\n\
28 -p port Listen on the specified port (default: 5060)\n\
29 -l address Listen on the specified address (multiple -l mean\n\
30 listening on more addresses). The default behaviour\n\
31 is to listen on the addresses returned by uname(2)\n\
33 -n processes Number of child processes to fork per interface\n\
36 -r Use dns to check if is necessary to add a \"received=\"\n\
38 -R Same as `-r´ but use reverse dns;\n\
39 (to use both use `-rR´)\n\
41 -v Turn on \"via:\" host checking when forwarding replies\n\
42 -d Debugging mode (multiple -d increase the level)\n\
43 -D Do not fork into daemon mode\n\
46 -h This help message\n\
50 /* debuging function */
52 void receive_stdin_loop()
59 len=fread(buf,1,BSIZE,stdin);
61 receive_msg(buf, len);
62 printf("-------------------------\n");
70 unsigned short port_no = 0; /* port on which we listen */
71 int child_no = 0; /* number of children processing requests */
75 int check_via = 0; /* check if reply first via host==us */
76 int received_dns = 0; /* use dns and/or rdns or to see if we need to
77 add a ;received=x.x.x.x to via: */
79 char* names[MAX_LISTEN]; /* our names */
80 unsigned long addresses[MAX_LISTEN]; /* our ips */
81 int addresses_no=0; /* number of names/ips */
85 int main(int argc, char** argv)
92 struct utsname myname;
94 /* process command line (get port no, cfg. file path etc) */
96 while((c=getopt(argc,argv,"f:p:l:n:rRvdDEVh"))!=-1){
102 port_no=strtol(optarg, &tmp, 10);
104 fprintf(stderr, "bad port number: -p %s\n", optarg);
109 /* add a new addr. to out address list */
110 if (addresses_no < MAX_LISTEN){
111 names[addresses_no]=(char*)malloc(strlen(optarg)+1);
112 if (names[addresses_no]==0){
113 fprintf(stderr, "Out of memory.\n");
116 strncpy(names[addresses_no], optarg, strlen(optarg)+1);
120 "Too many addresses (max. %d).\n",
126 child_no=strtol(optarg, tmp, 10);
128 fprintf(stderr, "bad process number: -n %s\n", optarg);
136 received_dns|=DO_DNS;
139 received_dns|=DO_REV_DNS;
150 printf("version: %s\n", version);
154 printf("version: %s\n", version);
155 printf("%s",help_msg);
160 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
163 "Unknown option character `\\x%x´.\n",
168 "Option `-%c´ requires an argument.\n",
176 /* fill missing arguments with the default values*/
177 if (cfg_file==0) cfg_file=CFG_FILE;
178 if (port_no==0) port_no=SIP_PORT;
179 if (child_no==0) child_no=CHILD_NO;
180 if (addresses_no==0) {
181 /* get our address, only the first one */
182 if (uname (&myname) <0){
183 fprintf(stderr, "cannot determine hostname, try -l address\n");
186 names[addresses_no]=(char*)malloc(strlen(myname.nodename)+1);
187 if (names[addresses_no]==0){
188 fprintf(stderr, "Out of memory.\n");
191 strncpy(names[addresses_no], myname.nodename,
192 strlen(myname.nodename)+1);
197 printf("Listening on ");
198 for (r=0; r<addresses_no;r++){
199 he=gethostbyname(names[r]);
201 DPrint("ERROR: could not resolve %s\n", names[r]);
204 addresses[r]=*((long*)he->h_addr_list[0]);
205 printf("%s [%s] : %d\n",names[r],
206 inet_ntoa(*(struct in_addr*)&addresses[r]),
207 (unsigned short)port_no);
212 /* load config file or die */
213 cfg_stream=fopen (cfg_file, "r");
215 DPrint("ERROR: loading config file(%s): %s\n", cfg_file,
220 if (cfg_parse_stream(cfg_stream)!=0){
221 DPrint("ERROR: config parser failure\n");
231 /* only one address for now */
232 if (udp_init(addresses[0],port_no)==-1) goto error;
233 /* start/init other processes/threads ? */