1 /* This is a simple TCP client that connects to port 1234 and prints a list
2  * of files in a given directory.
3  *
4  * It directly deserializes and serializes messages from network, minimizing
5  * memory use.
6  *
7  * For flexibility, this example is implemented using posix api.
8  * In a real embedded system you would typically use some other kind of
9  * a communication and filesystem layer.
10  */
11 
12 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <unistd.h>
16 #include <dirent.h>
17 #include <stdio.h>
18 #include <string.h>
19 
20 #include <pb_encode.h>
21 #include <pb_decode.h>
22 
23 #include "fileproto.pb.h"
24 #include "common.h"
25 
26 /* This callback function will be called once for each filename received
27  * from the server. The filenames will be printed out immediately, so that
28  * no memory has to be allocated for them.
29  */
30 bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
31 {
32     FileInfo fileinfo = {};
33 
34     if (!pb_decode(stream, FileInfo_fields, &fileinfo))
35         return false;
36 
37     printf("%-10lld %s\n", (long long)fileinfo.inode, fileinfo.name);
38 
39     return true;
40 }
41 
42 /* This function sends a request to socket 'fd' to list the files in
43  * directory given in 'path'. The results received from server will
44  * be printed to stdout.
45  */
46 bool listdir(int fd, char *path)
47 {
48     /* Construct and send the request to server */
49     {
50         ListFilesRequest request = {};
51         pb_ostream_t output = pb_ostream_from_socket(fd);
52 
53         /* In our protocol, path is optional. If it is not given,
54          * the server will list the root directory. */
55         if (path == NULL)
56         {
57             request.has_path = false;
58         }
59         else
60         {
61             request.has_path = true;
62             if (strlen(path) + 1 > sizeof(request.path))
63             {
64                 fprintf(stderr, "Too long path.\n");
65                 return false;
66             }
67 
68             strcpy(request.path, path);
69         }
70 
71         /* Encode the request. It is written to the socket immediately
72          * through our custom stream. */
73         if (!pb_encode_delimited(&output, ListFilesRequest_fields, &request))
74         {
75             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&output));
76             return false;
77         }
78     }
79 
80     /* Read back the response from server */
81     {
82         ListFilesResponse response = {};
83         pb_istream_t input = pb_istream_from_socket(fd);
84 
85         /* Give a pointer to our callback function, which will handle the
86          * filenames as they arrive. */
87         response.file.funcs.decode = &printfile_callback;
88 
89         if (!pb_decode_delimited(&input, ListFilesResponse_fields, &response))
90         {
91             fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input));
92             return false;
93         }
94 
95         /* If the message from server decodes properly, but directory was
96          * not found on server side, we get path_error == true. */
97         if (response.path_error)
98         {
99             fprintf(stderr, "Server reported error.\n");
100             return false;
101         }
102     }
103 
104     return true;
105 }
106 
107 int main(int argc, char **argv)
108 {
109     int sockfd;
110     struct sockaddr_in servaddr;
111     char *path = NULL;
112 
113     if (argc > 1)
114         path = argv[1];
115 
116     sockfd = socket(AF_INET, SOCK_STREAM, 0);
117 
118     /* Connect to server running on localhost:1234 */
119     memset(&servaddr, 0, sizeof(servaddr));
120     servaddr.sin_family = AF_INET;
121     servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
122     servaddr.sin_port = htons(1234);
123 
124     if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
125     {
126         perror("connect");
127         return 1;
128     }
129 
130     /* Send the directory listing request */
131     if (!listdir(sockfd, path))
132         return 2;
133 
134     /* Close connection */
135     close(sockfd);
136 
137     return 0;
138 }
139