server.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* This is a simple TCP server that listens on port 1234 and provides lists
  2. * of files to clients, using a protocol defined in file_server.proto.
  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. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <dirent.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <pb_encode.h>
  19. #include <pb_decode.h>
  20. #include "fileproto.pb.h"
  21. #include "common.h"
  22. /* This callback function will be called during the encoding.
  23. * It will write out any number of FileInfo entries, without consuming unnecessary memory.
  24. * This is accomplished by fetching the filenames one at a time and encoding them
  25. * immediately.
  26. */
  27. bool ListFilesResponse_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
  28. {
  29. PB_UNUSED(istream);
  30. if (ostream != NULL && field->tag == ListFilesResponse_file_tag)
  31. {
  32. DIR *dir = *(DIR**)field->pData;
  33. struct dirent *file;
  34. FileInfo fileinfo = {};
  35. while ((file = readdir(dir)) != NULL)
  36. {
  37. fileinfo.inode = file->d_ino;
  38. strncpy(fileinfo.name, file->d_name, sizeof(fileinfo.name));
  39. fileinfo.name[sizeof(fileinfo.name) - 1] = '\0';
  40. /* This encodes the header for the field, based on the constant info
  41. * from pb_field_t. */
  42. if (!pb_encode_tag_for_field(ostream, field))
  43. return false;
  44. /* This encodes the data for the field, based on our FileInfo structure. */
  45. if (!pb_encode_submessage(ostream, FileInfo_fields, &fileinfo))
  46. return false;
  47. }
  48. /* Because the main program uses pb_encode_delimited(), this callback will be
  49. * called twice. Rewind the directory for the next call. */
  50. rewinddir(dir);
  51. }
  52. return true;
  53. }
  54. /* Handle one arriving client connection.
  55. * Clients are expected to send a ListFilesRequest, terminated by a '0'.
  56. * Server will respond with a ListFilesResponse message.
  57. */
  58. void handle_connection(int connfd)
  59. {
  60. DIR *directory = NULL;
  61. /* Decode the message from the client and open the requested directory. */
  62. {
  63. ListFilesRequest request = {};
  64. pb_istream_t input = pb_istream_from_socket(connfd);
  65. if (!pb_decode_delimited(&input, ListFilesRequest_fields, &request))
  66. {
  67. printf("Decode failed: %s\n", PB_GET_ERROR(&input));
  68. return;
  69. }
  70. directory = opendir(request.path);
  71. printf("Listing directory: %s\n", request.path);
  72. }
  73. /* List the files in the directory and transmit the response to client */
  74. {
  75. ListFilesResponse response = {};
  76. pb_ostream_t output = pb_ostream_from_socket(connfd);
  77. if (directory == NULL)
  78. {
  79. perror("opendir");
  80. /* Directory was not found, transmit error status */
  81. response.has_path_error = true;
  82. response.path_error = true;
  83. }
  84. else
  85. {
  86. /* Directory was found, transmit filenames */
  87. response.has_path_error = false;
  88. response.file = directory;
  89. }
  90. if (!pb_encode_delimited(&output, ListFilesResponse_fields, &response))
  91. {
  92. printf("Encoding failed: %s\n", PB_GET_ERROR(&output));
  93. }
  94. }
  95. if (directory != NULL)
  96. closedir(directory);
  97. }
  98. int main(int argc, char **argv)
  99. {
  100. int listenfd, connfd;
  101. struct sockaddr_in servaddr;
  102. int reuse = 1;
  103. /* Listen on localhost:1234 for TCP connections */
  104. listenfd = socket(AF_INET, SOCK_STREAM, 0);
  105. setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  106. memset(&servaddr, 0, sizeof(servaddr));
  107. servaddr.sin_family = AF_INET;
  108. servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  109. servaddr.sin_port = htons(1234);
  110. if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0)
  111. {
  112. perror("bind");
  113. return 1;
  114. }
  115. if (listen(listenfd, 5) != 0)
  116. {
  117. perror("listen");
  118. return 1;
  119. }
  120. for(;;)
  121. {
  122. /* Wait for a client */
  123. connfd = accept(listenfd, NULL, NULL);
  124. if (connfd < 0)
  125. {
  126. perror("accept");
  127. return 1;
  128. }
  129. printf("Got connection.\n");
  130. handle_connection(connfd);
  131. printf("Closing connection.\n");
  132. close(connfd);
  133. }
  134. return 0;
  135. }