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  */
ListFilesResponse_callback(pb_istream_t * istream,pb_ostream_t * ostream,const pb_field_iter_t * field)30 bool ListFilesResponse_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
31 {
32     PB_UNUSED(ostream);
33     if (istream != NULL && field->tag == ListFilesResponse_file_tag)
34     {
35         FileInfo fileinfo = {};
36 
37         if (!pb_decode(istream, FileInfo_fields, &fileinfo))
38             return false;
39 
40         printf("%-10lld %s\n", (long long)fileinfo.inode, fileinfo.name);
41     }
42 
43     return true;
44 }
45 
46 /* This function sends a request to socket 'fd' to list the files in
47  * directory given in 'path'. The results received from server will
48  * be printed to stdout.
49  */
listdir(int fd,char * path)50 bool listdir(int fd, char *path)
51 {
52     /* Construct and send the request to server */
53     {
54         ListFilesRequest request = {};
55         pb_ostream_t output = pb_ostream_from_socket(fd);
56 
57         /* In our protocol, path is optional. If it is not given,
58          * the server will list the root directory. */
59         if (path == NULL)
60         {
61             request.has_path = false;
62         }
63         else
64         {
65             request.has_path = true;
66             if (strlen(path) + 1 > sizeof(request.path))
67             {
68                 fprintf(stderr, "Too long path.\n");
69                 return false;
70             }
71 
72             strcpy(request.path, path);
73         }
74 
75         /* Encode the request. It is written to the socket immediately
76          * through our custom stream. */
77         if (!pb_encode_delimited(&output, ListFilesRequest_fields, &request))
78         {
79             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&output));
80             return false;
81         }
82     }
83 
84     /* Read back the response from server */
85     {
86         ListFilesResponse response = {};
87         pb_istream_t input = pb_istream_from_socket(fd);
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 
main(int argc,char ** argv)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