flist request
[reposerve] / client / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <netdb.h>
10 #include "proto.h"
11
12
13 int main(int argc, char **argv)
14 {
15         int i, s, sz;
16         struct sockaddr_in addr;
17         struct hostent *host;
18         char *resp, *ptr;
19         int resp_size, pending;
20         struct proto_flist *flist;
21         char buf[256];
22
23         if((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
24                 perror("failed to create socket");
25                 return 1;
26         }
27
28         if(!(host = gethostbyname(argv[1]))) {
29                 fprintf(stderr, "Can't find %s: %s\n", argv[1], hstrerror(h_errno));
30                 return 1;
31         }
32
33         memset(&addr, 0, sizeof addr);
34         addr.sin_family = AF_INET;
35         addr.sin_port = htons(64357);
36         addr.sin_addr.s_addr = *(in_addr_t*)host->h_addr;
37
38         printf("Connecting to %s ...\n", inet_ntoa(addr.sin_addr));
39
40         if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
41                 fprintf(stderr, "Failed to connect to %s: %s\n", argv[1], strerror(errno));
42                 return 1;
43         }
44
45         if(read_line(s, buf, sizeof buf) == -1) {
46                 fprintf(stderr, "Failed to read server identifier\n");
47                 return 1;
48         }
49         if(memcmp(buf, "reposerve-", 10) != 0) {
50                 fprintf(stderr, "Protocol error, doesn't seem we're talking to reposerve at the other end\n");
51                 return 1;
52         }
53
54         write(s, "flist\n", 6);
55         shutdown(s, SHUT_WR);
56
57         if((resp_size = read_resp(s)) == -1) {
58                 fprintf(stderr, "flist request failed\n");
59                 return -1;
60         }
61         if(!resp_size) {
62                 fprintf(stderr, "flist response empty\n");
63                 return 0;
64         }
65
66         if(!(resp = malloc(resp_size))) {
67                 fprintf(stderr, "failed to allocate response buffer (%d bytes)\n", resp_size);
68                 return -1;
69         }
70         ptr = resp;
71         pending = resp_size;
72         while(pending > 0 && (sz = read(s, ptr, pending)) > 0) {
73                 pending -= sz;
74                 ptr += sz;
75         }
76         if(pending > 0) {
77                 fprintf(stderr, "failed to read complete response (%d/%d bytes)\n",
78                                 resp_size - pending, resp_size);
79                 free(resp);
80                 return -1;
81         }
82
83         /* parse response */
84         flist = (struct proto_flist*)resp;
85         ptr = resp + sizeof *flist + (flist->num_files - 1) * sizeof *flist->files;
86         for(i=0; i<flist->num_files; i++) {
87                 char *name = ptr + flist->files[i].nameoffs;
88                 int tmp = name[flist->files[i].namelen];
89                 name[flist->files[i].namelen] = 0;
90
91                 printf("%s\n", name);
92                 name[flist->files[i].namelen] = tmp;
93         }
94
95         close(s);
96         return 0;
97 }