ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.3
Committed: Fri May 10 21:32:38 2002 UTC (22 years ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.2: +8 -4 lines
Log Message:
Command line parsring

File Contents

# Content
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 typedef struct{
12 int fm_port;
13 char *fm_host;
14
15 char *my_fqdn;
16 char *server_fqdn;
17 int server_udp_port;
18 int server_tcp_port;
19 long last_modified;
20 char *files_list;
21 char *key;
22 int udp_update_time;
23 int tcp_update_time;
24
25 }ihost_state_t;
26
27 char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){
28 char *reply;
29 fprintf(f_w, "%s", sendString);
30 fflush(f_w);
31 reply=fpgetline(f_r);
32 /* Returns pointer to static buffer */
33 return reply;
34 }
35
36 int ihost_configure(ihost_state_t *ihost_state){
37 struct sockaddr_in addr;
38 struct in_addr haddr;
39 int sd;
40 FILE *fm_fd_r, *fm_fd_w;
41 char *reply;
42 char *reply_ptr;
43
44 if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
45 errf("Can't create AF_INET socket (%m)");
46 return -1;
47 }
48
49 if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
50 errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
51 return -1;
52 }
53
54 memset((char *)&addr, 0, sizeof addr);
55 addr.sin_family = AF_INET;
56 memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
57 addr.sin_port = htons(ihost_state->fm_port);
58
59 if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
60 errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
61 return -1;
62 }
63
64 /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
65 if ((fm_fd_r=fdopen(sd,"r")) == NULL){
66 errf("Failed to open stream (%m)");
67 return -1;
68 }
69
70 if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
71 errf("Failed to open stream (%m)");
72 return -1;
73 }
74
75 reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n");
76 if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
77 errf("Server error");
78 return -1;
79 }
80
81 reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n");
82 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
83 errf("Server error (%m)");
84 return -1;
85 }
86 ihost_state->last_modified=atol(reply);
87
88 reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
89 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
90 errf("Server error (%m)");
91 return -1;
92 }
93 if((ihost_state->files_list=strdup(reply)) == NULL){
94 errf("strdup failed (%m)");
95 return -1;
96 }
97
98 reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n");
99 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
100 errf("Server error (%m)");
101 return -1;
102 }
103 if((ihost_state->my_fqdn=strdup(reply)) == NULL){
104 errf("strdup failed (%m)");
105 return -1;
106 }
107
108 reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n");
109 if(reply== NULL){
110 errf("Server error (%m)");
111 return -1;
112 }
113 if (strncasecmp(reply, "ERROR", 5) != 0){
114 ihost_state->udp_update_time=atoi(reply);
115 }
116
117 reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n");
118 if(reply== NULL){
119 errf("Server error (%m)");
120 return -1;
121 }
122 if (strncasecmp(reply, "ERROR", 5) != 0){
123 ihost_state->tcp_update_time=atoi(reply);
124 }
125
126 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n");
127 if(reply== NULL){
128 errf("Server error (%m)");
129 return -1;
130 }
131
132 reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n");
133 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
134 errf("Server error (%m)");
135 return -1;
136 }
137 reply_ptr=strchr(reply,';');
138 if (reply_ptr==NULL){
139 errf("Incorrect data returned");
140 return -1;
141 }
142 *reply_ptr='\0';
143 if((ihost_state->server_fqdn=strdup(reply)) == NULL){
144 errf("strdup failed (%m)");
145 return -1;
146 }
147 reply=++reply_ptr;
148 reply_ptr=strchr(reply,';');
149 if (reply_ptr==NULL){
150 errf("Incorrect data returned 2");
151 return -1;
152 }
153 *reply_ptr='\0';
154 ihost_state->server_udp_port=atoi(reply);
155 reply=++reply_ptr;
156 ihost_state->server_tcp_port=atoi(reply);
157 if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
158 errf("Incorrect data returned 3 ");
159 return -1;
160 }
161
162 reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
163 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
164 errf("Server error (%m)");
165 return -1;
166 }
167
168 if(fclose(fm_fd_r) !=0){
169 errf("Failed to close FD (%m)");
170 return -1;
171 }
172 if(fclose(fm_fd_w) !=0){
173 errf("Failed to close FD (%m)");
174 return -1;
175 }
176
177 return 0;
178 }
179
180 int main(int argc, char **argv){
181 ihost_state_t ihost_state;
182 errf_set_progname(argv[0]);
183 if(argc!=3){
184 errf_usage("<host> <port>");
185 exit(1);
186 }
187
188 ihost_state.fm_host=argv[1];
189 ihost_state.fm_port=atoi(argv[2]);
190
191 if(ihost_configure(&ihost_state)!=0){
192 errf("configure failed");
193 }
194
195 return 0;
196 }
197