backup
[lcr.git] / h323conf.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** parse h323 gateway config file                                            **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "main.h"
16
17 /* parse h323_gateway.conf
18  *
19  * searches for the given ip and returns the extension or NULL if not found
20  */
21 char *parse_h323gateway(char *ip, char *opt, int opt_size)
22 {
23         FILE *fp=NULL;
24         char filename[256];
25         char *p;
26         unsigned int line,i;
27         char buffer[256];
28         static char host_ip[32], extension[32], option[64];
29         int found = 0;
30
31         SPRINT(filename, "%s/h323_gateway.conf", INSTALL_DATA);
32
33         if (!(fp = fopen(filename, "r")))
34         {
35                 PERROR("Cannot open h323 gateway map: \"%s\"\n", filename);
36                 return(0);
37         }
38
39         line=0;
40         while((fgets(buffer, sizeof(buffer), fp)))
41         {
42                 line++;
43                 buffer[sizeof(buffer)-1] = '\0';
44                 if (buffer[0]) buffer[strlen(buffer)-1] = '\0';
45                 p = buffer;
46
47                 while(*p <= 32) /* skip spaces */
48                 {
49                         if (*p == 0)
50                                 break;
51                         p++;
52                 }
53                 if (*p==0 || *p=='#') /* ignore comments and empty line */
54                         continue;
55
56                 host_ip[0]=0;
57                 extension[0]=0;
58                 option[0]=0;
59
60                 i=0; /* read host ip */
61                 while(*p > 32)
62                 {
63                         if (i+1 >= sizeof(host_ip))
64                         {
65                                 PERROR_RUNTIME("Error in %s (line %d): ip too long.\n",filename,line);
66                                 break;
67                         }
68                         host_ip[i+1] = '\0';
69                         host_ip[i++] = *p++;
70                 }
71
72                 while(*p <= 32) /* skip spaces */
73                 {
74                         if (*p == 0)
75                                 break;
76                         p++;
77                 }
78
79                 if (*p!=0 && *p!='#') /* extension */
80                 {
81                         i=0; /* read extension */
82                         while(*p > 32)
83                         {
84                                 if (i+1 >= sizeof(extension))
85                                 {
86                                         PERROR_RUNTIME("Error in %s (line %d): extension too long.\n",filename,line);
87                                         break;
88                                 }
89                                 extension[i+1] = '\0';
90                                 extension[i++] = *p++;
91                         }
92                         while(*p <= 32) /* skip spaces */
93                         {
94                                 if (*p == 0)
95                                         break;
96                                 p++;
97                         }
98                 }
99
100                 if (*p!=0 && *p!='#') /* option */
101                 {
102                         i=0; /* read option */
103                         while(*p > 32)
104                         {
105                                 if (i+1 >= sizeof(option))
106                                 {
107                                         PERROR_RUNTIME("Error in %s (line %d): option too long.\n",filename,line);
108                                         break;
109                                 }
110                                 option[i+1] = '\0';
111                                 option[i++] = *p++;
112                         }
113                         // ignoring more
114                 }
115
116                 if (!!strcasecmp(ip, host_ip))
117                         continue;
118
119                 if (extension[0] == '\0')
120                         continue;
121
122                 found = 1;
123                 break; /* found entry */
124         }
125
126         if (fp) fclose(fp);
127
128         if (found)
129         {
130                 UNCPY(opt, option, opt_size-1);
131                 opt[opt_size-1] = '\0';
132                 return(extension);
133         }
134         return(0);
135 }
136
137