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, char* sendString){ |
28 |
char *reply; |
29 |
|
30 |
fprintf(f, sendString); |
31 |
fflush(f); |
32 |
reply=fpgetline(f); |
33 |
/* Returns pointer to static buffer */ |
34 |
return reply; |
35 |
} |
36 |
|
37 |
int ihost_configure(ihost_state_t *ihost_state){ |
38 |
struct sockaddr_in addr; |
39 |
struct in_addr haddr; |
40 |
int sd; |
41 |
FILE *fm_fd; |
42 |
char *reply; |
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 |
if ((fm_fd=fdopen(sd,"r+")) == NULL){ |
65 |
errf("Failed to open stream (%m)"); |
66 |
return -1; |
67 |
} |
68 |
|
69 |
reply=sock_comm(fm_fd, "STARTCONFIG\n"); |
70 |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
71 |
errf("Server error"); |
72 |
return -1; |
73 |
} |
74 |
|
75 |
reply=sock_comm(fm_fd, "LASTMODIFIED\n"); |
76 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
77 |
errf("Server error (%m)"); |
78 |
return -1; |
79 |
} |
80 |
ihost_state->last_modified=atol(reply); |
81 |
|
82 |
reply=sock_comm(fm_fd, "FILELIST\n"); |
83 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
84 |
errf("Server error (%m)"); |
85 |
return -1; |
86 |
} |
87 |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
88 |
errf("strdup failed (%m)"); |
89 |
return -1; |
90 |
} |
91 |
|
92 |
reply=sock_comm(fm_fd, "FQDN\n"); |
93 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
94 |
errf("Server error (%m)"); |
95 |
return -1; |
96 |
} |
97 |
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
98 |
errf("strdup failed (%m)"); |
99 |
return -1; |
100 |
} |
101 |
|
102 |
reply=sock_comm(fm_fd, "UDPUpdateTime\n"); |
103 |
if(reply== NULL){ |
104 |
errf("Server error (%m)"); |
105 |
return -1; |
106 |
} |
107 |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
108 |
ihost_state->udp_update_time=atoi(reply); |
109 |
} |
110 |
|
111 |
reply=sock_comm(fm_fd, "TCPUpdateTime\n"); |
112 |
if(reply== NULL){ |
113 |
errf("Server error (%m)"); |
114 |
return -1; |
115 |
} |
116 |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
117 |
ihost_state->tcp_update_time=atoi(reply); |
118 |
} |
119 |
|
120 |
reply=sock_comm(fm_fd, "ENDCONFIG\n"); |
121 |
if(reply== NULL){ |
122 |
errf("Server error (%m)"); |
123 |
return -1; |
124 |
} |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
return 0; |
130 |
|
131 |
} |
132 |
|
133 |
int main(){ |
134 |
|
135 |
return 0; |
136 |
} |
137 |
|