ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.5
Committed: Sun May 12 14:10:45 2002 UTC (22 years, 7 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.4: +19 -1 lines
Log Message:
Improved the memory managment.

File Contents

# User Rev Content
1 pajs 1.1 #include <stdio.h>
2     #include <sys/socket.h>
3     #include <stdlib.h>
4     #include <unistd.h>
5     #include <syslog.h>
6     #include <netinet/in.h>
7     #include <ukcprog.h>
8     #include <netdb.h>
9     #include <strings.h>
10    
11 pajs 1.4 #define RECONFIGURE_RETURN_CODE 2
12    
13 pajs 1.1 typedef struct{
14     int fm_port;
15     char *fm_host;
16    
17     char *my_fqdn;
18     char *server_fqdn;
19     int server_udp_port;
20     int server_tcp_port;
21 pajs 1.4 char *last_modified;
22 pajs 1.1 char *files_list;
23     char *key;
24     int udp_update_time;
25     int tcp_update_time;
26    
27     }ihost_state_t;
28    
29 pajs 1.2 char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){
30 pajs 1.1 char *reply;
31 pajs 1.2 fprintf(f_w, "%s", sendString);
32     fflush(f_w);
33     reply=fpgetline(f_r);
34 pajs 1.1 /* Returns pointer to static buffer */
35     return reply;
36     }
37    
38     int ihost_configure(ihost_state_t *ihost_state){
39     struct sockaddr_in addr;
40     struct in_addr haddr;
41     int sd;
42 pajs 1.2 FILE *fm_fd_r, *fm_fd_w;
43 pajs 1.1 char *reply;
44 pajs 1.2 char *reply_ptr;
45 pajs 1.1
46 pajs 1.5 /* Check to see if anything needs to be free'd */
47     if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host);
48     if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
49     if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
50     if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
51     if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
52    
53 pajs 1.1 if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
54     errf("Can't create AF_INET socket (%m)");
55     return -1;
56     }
57    
58     if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
59     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
60     return -1;
61     }
62    
63     memset((char *)&addr, 0, sizeof addr);
64     addr.sin_family = AF_INET;
65     memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
66     addr.sin_port = htons(ihost_state->fm_port);
67    
68     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
69     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
70     return -1;
71     }
72    
73 pajs 1.2 /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
74     if ((fm_fd_r=fdopen(sd,"r")) == NULL){
75     errf("Failed to open stream (%m)");
76     return -1;
77     }
78    
79     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
80 pajs 1.1 errf("Failed to open stream (%m)");
81     return -1;
82     }
83 pajs 1.2
84     reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n");
85 pajs 1.1 if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
86     errf("Server error");
87     return -1;
88     }
89    
90 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n");
91     if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
92 pajs 1.1 errf("Server error (%m)");
93     return -1;
94     }
95 pajs 1.4 if((ihost_state->last_modified=strdup(reply)) == NULL){
96     errf("strdup failed (%m)");
97     return -1;
98     }
99 pajs 1.1
100 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
101     if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
102 pajs 1.1 errf("Server error (%m)");
103     return -1;
104     }
105     if((ihost_state->files_list=strdup(reply)) == NULL){
106     errf("strdup failed (%m)");
107     return -1;
108     }
109    
110 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n");
111     if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
112 pajs 1.1 errf("Server error (%m)");
113     return -1;
114     }
115     if((ihost_state->my_fqdn=strdup(reply)) == NULL){
116     errf("strdup failed (%m)");
117     return -1;
118     }
119    
120 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n");
121 pajs 1.1 if(reply== NULL){
122     errf("Server error (%m)");
123     return -1;
124     }
125     if (strncasecmp(reply, "ERROR", 5) != 0){
126     ihost_state->udp_update_time=atoi(reply);
127     }
128    
129 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n");
130 pajs 1.1 if(reply== NULL){
131     errf("Server error (%m)");
132     return -1;
133     }
134     if (strncasecmp(reply, "ERROR", 5) != 0){
135     ihost_state->tcp_update_time=atoi(reply);
136     }
137    
138 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n");
139 pajs 1.1 if(reply== NULL){
140     errf("Server error (%m)");
141     return -1;
142     }
143    
144 pajs 1.2 reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n");
145     if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
146     errf("Server error (%m)");
147     return -1;
148     }
149     reply_ptr=strchr(reply,';');
150     if (reply_ptr==NULL){
151     errf("Incorrect data returned");
152     return -1;
153     }
154     *reply_ptr='\0';
155     if((ihost_state->server_fqdn=strdup(reply)) == NULL){
156     errf("strdup failed (%m)");
157     return -1;
158     }
159     reply=++reply_ptr;
160     reply_ptr=strchr(reply,';');
161     if (reply_ptr==NULL){
162     errf("Incorrect data returned 2");
163     return -1;
164     }
165     *reply_ptr='\0';
166     ihost_state->server_udp_port=atoi(reply);
167     reply=++reply_ptr;
168     ihost_state->server_tcp_port=atoi(reply);
169     if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
170     errf("Incorrect data returned 3 ");
171     return -1;
172     }
173    
174     reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
175     if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
176     errf("Server error (%m)");
177     return -1;
178     }
179 pajs 1.1
180 pajs 1.2 if(fclose(fm_fd_r) !=0){
181     errf("Failed to close FD (%m)");
182     return -1;
183     }
184     if(fclose(fm_fd_w) !=0){
185     errf("Failed to close FD (%m)");
186     return -1;
187     }
188 pajs 1.1
189     return 0;
190     }
191 pajs 1.4
192     int heartbeat(ihost_state_t *ihost_state){
193     struct sockaddr_in addr;
194     struct in_addr haddr;
195     int sd;
196     FILE *fm_fd_r, *fm_fd_w;
197     char *reply;
198     int exitcode=0;
199    
200     if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
201     errf("Can't create AF_INET socket (%m)");
202     return -1;
203     }
204    
205     if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
206     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
207     return -1;
208     }
209    
210     memset((char *)&addr, 0, sizeof addr);
211     addr.sin_family = AF_INET;
212     memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
213     addr.sin_port = htons(ihost_state->server_tcp_port);
214    
215     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
216     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
217     return -1;
218     }
219    
220     /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
221     if ((fm_fd_r=fdopen(sd,"r")) == NULL){
222     errf("Failed to open stream (%m)");
223     return -1;
224     }
225    
226     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
227     errf("Failed to open stream (%m)");
228     return -1;
229     }
230    
231     reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n");
232     if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
233     errf("Server error");
234     return -1;
235     }
236    
237     reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n");
238     if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
239     errf("Server error");
240     return -1;
241     }
242    
243     reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
244     if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
245     errf("Server error");
246     return -1;
247     }
248    
249     reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
250     if (reply==NULL) {
251     errf("Server error");
252     return -1;
253     }
254     if (strncasecmp(reply, "ERROR", 2) == 0){
255     /* Means the config has changed */
256     exitcode=RECONFIGURE_RETURN_CODE;
257     }
258     reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n");
259     if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
260     errf("Server error");
261     return -1;
262     }
263 pajs 1.5 if (ihost_state->key!=NULL) free(ihost_state->key);
264    
265 pajs 1.4 if((ihost_state->key=strdup(reply)) == NULL){
266     errf("strdup failed (%m)");
267     return -1;
268     }
269    
270     reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
271     if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
272     errf("Server error (%m)");
273     return -1;
274     }
275    
276     return exitcode;
277     }
278    
279 pajs 1.1
280 pajs 1.3 int main(int argc, char **argv){
281 pajs 1.2 ihost_state_t ihost_state;
282 pajs 1.5
283     /* NULL'ify so i can tell if i need to free it or not */
284     ihost_state.fm_host=NULL;
285     ihost_state.my_fqdn=NULL;
286     ihost_state.server_fqdn=NULL;
287     ihost_state.last_modified=NULL;
288     ihost_state.files_list=NULL;
289     ihost_state.key=NULL;
290    
291 pajs 1.3 errf_set_progname(argv[0]);
292     if(argc!=3){
293     errf_usage("<host> <port>");
294     exit(1);
295     }
296 pajs 1.2
297 pajs 1.3 ihost_state.fm_host=argv[1];
298     ihost_state.fm_port=atoi(argv[2]);
299 pajs 1.2
300     if(ihost_configure(&ihost_state)!=0){
301     errf("configure failed");
302     }
303 pajs 1.5
304 pajs 1.1
305     return 0;
306     }
307