- still to do action,c
[sip-router] / proxy.c
1 /*
2  * $Id$
3  *
4  * proxy list & assoc. functions
5  *
6  */
7
8
9 #include "proxy.h"
10 #include "error.h"
11
12 #include <string.h>
13
14
15 struct proxy_l* proxies=0;
16
17
18
19 /* searches for the proxy named 'name', on port 'port'
20    returns: pointer to proxy_l on success or 0 if not found */ 
21 struct proxy_l* find_proxy(char *name, unsigned short port)
22 {
23         struct proxy_l* t;
24         for(t=proxies; t; t=t->next)
25                 if ((strcasecmp(t->name, name)==0) && (t->port==port))
26                         break;
27         return t;
28 }
29
30
31
32 /* copies a hostent structure*, returns 0 on success, <0 on error*/
33 int hostent_cpy(struct hostent *dst, struct hosten* src)
34 {
35         int len, r;
36
37         /* start copying the host entry.. */
38         /* copy h_name */
39         len=strlen(src->h_name)+1;
40         dst->h_name=(char*)malloc(sizeof(char) * len);
41         if (dst->h_name) strncpy(dst->h_name,src->h_name, len);
42         else{
43                 ret=E_OUT_OF_MEM;
44                 goto error;
45         }
46
47         /* copy h_aliases */
48         for (len=0;src->h_aliases[len];len++);
49         dst->h_aliases=(char**)malloc(sizeof(char*)*(len+1));
50         if (dst->h_aliases==0){
51                 ret=E_OUT_OF_MEM;
52                 free(dst->h_name);
53                 goto error;
54         }
55         memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );
56         for (i=0;i<len;i++){
57                 len2=strlen(src->h_aliases[i])+1;
58                 dst->.h_aliases[i]=(char*)malloc(sizeof(char)*len2);
59                 if (dst->h_aliases==0){
60                         ret=E_OUT_OF_MEM;
61                         free(dst->h_name);
62                         for(r=0; r<i; r++)      free(dst->h_aliases[r]);
63                         free(dst->h_aliases);
64                         goto error;
65                 }
66                 strncpy(dst->h_aliases[i], src->h_aliases[i], len2);
67         }
68         /* copy h_addr_list */
69         for (len=0;src->h_addr_list[len];len++);
70         dst->h_addr_list=(char**)malloc(sizeof(char*)*(len+1));
71         if (dst->h_addr_list==0){
72                 ret=E_OUT_OF_MEM;
73                 free(dst->h_name);
74                 for(r=0; h_aliases[r]; r++)     free(dst->h_aliases[r]);
75                 free h_aliases[r];
76                 free(dst->h_aliases);
77                 goto error;
78         }
79         memset((void*)dst->.h_addr_list, 0, sizeof(char*) * (len+1) );
80         for (i=0;i<len;i++){
81                 dst->h_addr_list[i]=(char*)malloc(sizeof(char)*src->h_length);
82                 if (dst->h_addr_list[i]==0){
83                         ret=E_OUT_OF_MEM;
84                         free(dst->h_name);
85                         for(r=0; h_aliases[r]; r++)     free(dst->h_aliases[r]);
86                         free h_aliases[r];
87                         free(dst->h_aliases);
88                         for (r=0; r<i;r++) free(dst->h_addr_list[r]);
89                         free(dst->h_addr_list);
90                         goto error;
91                 }
92                 memcpy(dst->h_addr_list[i], src->h_addr_list[i], src->h_length);
93         }
94
95         /* copy h_addr_type & length */
96         dst->h_addrtype=src->h_addrtype;
97         dst->host.h_length=src->h_length;
98         /*finished hostent copy */
99         
100         return 0;
101
102 error:
103         LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
104         return ret;
105 }
106
107
108
109 struct proxy_l* add_proxy(char* name, unsigned short port)
110 {
111         proxy_l* p;
112         struct hostent he;
113         
114         if ((p=find_proxy(name, port))!=0) return p;
115         p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
116         if (p==0){
117                 LOG(L_CRIT, "ERROR: add_proxy: memory allocation failure\n");
118                 goto error;
119         }
120         memset(p,0,sizeof(struct_proxy_l));
121         p->name=name;
122         p->port=port;
123         he=gethostbyname(name);
124         if (he==0){
125                 LOG(L_CRIT, "ERROR: add_proxy: could not resolve hostname:"
126                                         " \"%s\"\n", name);
127                 free(p);
128                 goto error;
129         }
130         if (hostent_cpy(&(p->host), he)!=0){
131                 free(p);
132                 goto error;
133         }
134         p->ok=1;
135         /* add p to the proxy list */
136         p->next=proxies;
137         proxies=p;
138         return p;
139
140 error:
141         return 0;
142 }
143