#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
+#include "proto.h"
+
int main(int argc, char **argv)
{
- int s;
+ int i, s, sz;
struct sockaddr_in addr;
struct hostent *host;
+ char *resp, *ptr;
+ int resp_size, pending;
+ struct proto_flist *flist;
+ char buf[256];
if((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("failed to create socket");
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(64357);
- addr.sin_addr.s_addr = inet_addr(host->h_addr);
+ addr.sin_addr.s_addr = *(in_addr_t*)host->h_addr;
+
+ printf("Connecting to %s ...\n", inet_ntoa(addr.sin_addr));
if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
fprintf(stderr, "Failed to connect to %s: %s\n", argv[1], strerror(errno));
return 1;
}
+ if(read_line(s, buf, sizeof buf) == -1) {
+ fprintf(stderr, "Failed to read server identifier\n");
+ return 1;
+ }
+ if(memcmp(buf, "reposerve-", 10) != 0) {
+ fprintf(stderr, "Protocol error, doesn't seem we're talking to reposerve at the other end\n");
+ return 1;
+ }
+
+ write(s, "flist\n", 6);
+ shutdown(s, SHUT_WR);
+
+ if((resp_size = read_resp(s)) == -1) {
+ fprintf(stderr, "flist request failed\n");
+ return -1;
+ }
+ if(!resp_size) {
+ fprintf(stderr, "flist response empty\n");
+ return 0;
+ }
+
+ if(!(resp = malloc(resp_size))) {
+ fprintf(stderr, "failed to allocate response buffer (%d bytes)\n", resp_size);
+ return -1;
+ }
+ ptr = resp;
+ pending = resp_size;
+ while(pending > 0 && (sz = read(s, ptr, pending)) > 0) {
+ pending -= sz;
+ ptr += sz;
+ }
+ if(pending > 0) {
+ fprintf(stderr, "failed to read complete response (%d/%d bytes)\n",
+ resp_size - pending, resp_size);
+ free(resp);
+ return -1;
+ }
+
+ /* parse response */
+ flist = (struct proto_flist*)resp;
+ ptr = resp + sizeof *flist + (flist->num_files - 1) * sizeof *flist->files;
+ for(i=0; i<flist->num_files; i++) {
+ char *name = ptr + flist->files[i].nameoffs;
+ int tmp = name[flist->files[i].namelen];
+ name[flist->files[i].namelen] = 0;
+
+ printf("%s\n", name);
+ name[flist->files[i].namelen] = tmp;
+ }
+
close(s);
return 0;
}
flist->final_size = newsz;
return 0;
}
+
+int read_line(int s, char *buf, int bufsz)
+{
+ char *ptr = buf;
+ while(bufsz > 1 && read(s, ptr, 1) > 0 && *ptr++ != '\n');
+ *ptr = 0;
+ return ptr == buf ? -1 : 0;
+}
+
+int read_resp(int s)
+{
+ int sz, msglen = 0;
+ char buf[64];
+ char *ptr;
+
+ while(msglen < 3 && (sz = read(s, buf + msglen, 3 - msglen)) > 0) {
+ msglen += sz;
+ }
+ if(msglen < 3) return -1;
+
+ if(memcmp(buf, "ERR", 3) == 0) {
+ return -1;
+ }
+
+ ptr = buf;
+ while(read(s, ptr, 1) > 0 && *ptr++ != '\n');
+ *ptr = 0;
+
+ return atoi(buf);
+}