24 |
|
|
25 |
|
}ihost_state_t; |
26 |
|
|
27 |
< |
char* sock_comm(FILE *f, char* sendString){ |
27 |
> |
char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){ |
28 |
|
char *reply; |
29 |
< |
|
30 |
< |
fprintf(f, sendString); |
31 |
< |
fflush(f); |
32 |
< |
reply=fpgetline(f); |
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 |
|
} |
37 |
|
struct sockaddr_in addr; |
38 |
|
struct in_addr haddr; |
39 |
|
int sd; |
40 |
< |
FILE *fm_fd; |
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)"); |
61 |
|
return -1; |
62 |
|
} |
63 |
|
|
64 |
< |
if ((fm_fd=fdopen(sd,"r+")) == NULL){ |
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 |
< |
reply=sock_comm(fm_fd, "STARTCONFIG\n"); |
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, "LASTMODIFIED\n"); |
82 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
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, "FILELIST\n"); |
89 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
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 |
|
} |
95 |
|
return -1; |
96 |
|
} |
97 |
|
|
98 |
< |
reply=sock_comm(fm_fd, "FQDN\n"); |
99 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
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 |
|
} |
105 |
|
return -1; |
106 |
|
} |
107 |
|
|
108 |
< |
reply=sock_comm(fm_fd, "UDPUpdateTime\n"); |
108 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n"); |
109 |
|
if(reply== NULL){ |
110 |
|
errf("Server error (%m)"); |
111 |
|
return -1; |
114 |
|
ihost_state->udp_update_time=atoi(reply); |
115 |
|
} |
116 |
|
|
117 |
< |
reply=sock_comm(fm_fd, "TCPUpdateTime\n"); |
117 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n"); |
118 |
|
if(reply== NULL){ |
119 |
|
errf("Server error (%m)"); |
120 |
|
return -1; |
123 |
|
ihost_state->tcp_update_time=atoi(reply); |
124 |
|
} |
125 |
|
|
126 |
< |
reply=sock_comm(fm_fd, "ENDCONFIG\n"); |
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 |
< |
|
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 |
< |
return 0; |
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(){ |
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 |
|
} |