Guest User

Untitled

a guest
Nov 26th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/sendfile.h>
  4. #include <stdio.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12.  
  13. #ifndef _MAX_SIZE_
  14. #define _MAX_SIZE_ 80
  15. #endif /* _MAX_SIZE_ */
  16.  
  17. #ifndef _SERVER_PORT_
  18. #define _SERVER_PORT_ 9734
  19. #endif /* _SERVER_PORT_ */
  20.  
  21. int err_num = 0;
  22.  
  23. /* destination address */
  24. struct sockaddr_in dest_addr;
  25.  
  26. void send_usrname_credentials(char *usrname, int sock_fd);
  27.  
  28. void send_password_credentials(char *password, int sock_fd);
  29.  
  30. /* send file to the server */
  31. void deliver_mail(const char *filename, int sock_fd);
  32.  
  33. /* sendfile */
  34. ssize_t do_sendfile(int sock_fd, int input_fd, off_t offset, size_t count);
  35.  
  36. int main(int argc, char **argv)
  37. {
  38. if (argc != 2)
  39. {
  40. (void)fprintf(stderr, "Usage: %s <username>\n", argv[0]);
  41.  
  42. exit(EXIT_FAILURE);
  43. }
  44.  
  45. /* username */
  46. char *username = argv[1];
  47.  
  48. /* password */
  49. char *password = NULL;
  50.  
  51. int err_num;
  52.  
  53. /* socket fd */
  54. int socket_fd;
  55.  
  56. int length;
  57.  
  58. int string_len;
  59.  
  60. int result;
  61.  
  62. /* create the socket for the client */
  63. socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  64.  
  65. /* checking for "socket" errors */
  66. if (socket_fd < 0)
  67. {
  68. err_num = errno;
  69.  
  70. (void)fprintf(stdout, "\"socket\" error: %s\n", strerror(err_num));
  71.  
  72. exit(EXIT_FAILURE);
  73. }
  74.  
  75. /* Initialize the socket address structure */
  76. memset(&dest_addr, 0, sizeof(dest_addr));
  77. dest_addr.sin_family = AF_INET;
  78. dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  79. dest_addr.sin_port = htons(_SERVER_PORT_);
  80. length = sizeof(dest_addr);
  81.  
  82. /* connect the socket to the server's socket */
  83. result = connect(socket_fd, (struct sockaddr *)&dest_addr, length);
  84.  
  85. /* checking for "connect" errors */
  86. if (result < 0)
  87. {
  88. err_num = errno;
  89.  
  90. (void)fprintf(stderr, "\"connect\" error: %s\n", strerror(err_num));
  91.  
  92. close(socket_fd);
  93.  
  94. exit(EXIT_FAILURE);
  95. }
  96.  
  97. (void)fprintf(stdout, "Connect completed\n");
  98.  
  99. deliver_mail("mail", socket_fd);
  100.  
  101. /* send the username to the server
  102. send_usrname_credentials(username, socket_fd);
  103.  
  104. (void)fprintf(stdout, "Utente: %s\n", username);
  105. */
  106.  
  107. return 0;
  108. }
  109.  
  110. /* send the username to the server */
  111. void send_usrname_credentials(char *usrname, int sock_fd)
  112. {
  113. struct msghdr username_cred;
  114.  
  115. struct iovec iov;
  116.  
  117. username_cred.msg_name = &dest_addr;
  118. username_cred.msg_namelen = sizeof(dest_addr);
  119. username_cred.msg_iov = &iov;
  120. username_cred.msg_iovlen = 1;
  121. username_cred.msg_iov->iov_base = usrname;
  122. username_cred.msg_iov->iov_len = (strlen(usrname) + 1);
  123. username_cred.msg_control = 0;
  124. username_cred.msg_controllen = 0;
  125. username_cred.msg_flags = 0;
  126.  
  127. sendmsg(sock_fd, &username_cred, 0);
  128. }
  129.  
  130. /* send the password to the server */
  131. void send_password_credentials(char *password, int sock_fd)
  132. {
  133. struct msghdr password_cred;
  134.  
  135. struct iovec iov;
  136.  
  137. password_cred.msg_name = &dest_addr;
  138. password_cred.msg_namelen = sizeof(dest_addr);
  139. password_cred.msg_iov = &iov;
  140. password_cred.msg_iovlen = 1;
  141. password_cred.msg_iov->iov_base = password;
  142. password_cred.msg_iov->iov_len = (strlen(password) + 1);
  143. password_cred.msg_control = 0;
  144. password_cred.msg_controllen = 0;
  145. password_cred.msg_flags = 0;
  146.  
  147. sendmsg(sock_fd, &password_cred, 0);
  148. }
  149.  
  150. /* send file to the server */
  151. void deliver_mail(const char *filename, int sock_fd)
  152. {
  153. /* file descriptor for file to send */
  154. int file_descriptor;
  155.  
  156. /* argument to fstat */
  157. struct stat stat_buf;
  158.  
  159. /* file offset */
  160. off_t offset = 0;
  161.  
  162. int ret_val;
  163.  
  164. size_t total_bytes_sent = 0;
  165.  
  166. /* open the file to be sent */
  167. file_descriptor = open(filename, O_RDONLY);
  168.  
  169. /* checking for "open" errors */
  170. if (file_descriptor < 0)
  171. { exit(EXIT_FAILURE); }
  172.  
  173. /* get the size of the file to be sent */
  174. fstat(file_descriptor, &stat_buf);
  175.  
  176. offset = 0;
  177.  
  178. /* sendfile */
  179. total_bytes_sent = do_sendfile(sock_fd, file_descriptor, offset, stat_buf.st_size);
  180.  
  181. (void)fprintf(stdout, "Total bytes sent: %d\n", total_bytes_sent);
  182.  
  183. close(file_descriptor);
  184. }
  185.  
  186. /* sendfile */
  187. ssize_t do_sendfile(int sock_fd, int input_fd, off_t offset, size_t count)
  188. {
  189. ssize_t bytes_sent;
  190.  
  191. size_t total_bytes_sent = 0;
  192.  
  193. while (total_bytes_sent < count)
  194. {
  195. if ((bytes_sent = sendfile(sock_fd, input_fd, &offset, (count - total_bytes_sent))) <= 0)
  196. {
  197. if (errno == EINTR || errno == EAGAIN)
  198. { continue; }
  199.  
  200. err_num = errno;
  201.  
  202. (void)fprintf(stderr, "\"sendfile\" error:%s\n", strerror(err_num));
  203.  
  204. return (-1);
  205. }
  206.  
  207. total_bytes_sent += bytes_sent;
  208. }
  209.  
  210. return (total_bytes_sent);
  211. }
Add Comment
Please, Sign In to add comment