lwext4_client.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include <stdbool.h>
  34. #include <getopt.h>
  35. #ifdef WIN32
  36. #include <winsock2.h>
  37. #include <ws2tcpip.h>
  38. #include <windows.h>
  39. static int inet_pton(int af, const char *src, void *dst);
  40. #else
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <sys/types.h>
  45. #endif
  46. static int winsock_init(void);
  47. static void winsock_fini(void);
  48. /**@brief Default server addres.*/
  49. static char *server_addr = "127.0.0.1";
  50. /**@brief Default connection port.*/
  51. static int connection_port = 1234;
  52. /**@brief Call op*/
  53. static char *op_code;
  54. static const char *usage = " \n\
  55. Welcome in lwext4_client. \n\
  56. Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
  57. Usage: \n\
  58. --call (-c) - call opt \n\
  59. --port (-p) - server port \n\
  60. --addr (-a) - server ip address \n\
  61. \n";
  62. static int client_connect(void)
  63. {
  64. int fd = 0;
  65. struct sockaddr_in serv_addr;
  66. if (winsock_init() < 0) {
  67. printf("winsock_init error\n");
  68. exit(-1);
  69. }
  70. memset(&serv_addr, '0', sizeof(serv_addr));
  71. fd = socket(AF_INET, SOCK_STREAM, 0);
  72. if (fd < 0) {
  73. printf("socket() error: %s\n", strerror(errno));
  74. exit(-1);
  75. }
  76. serv_addr.sin_family = AF_INET;
  77. serv_addr.sin_port = htons(connection_port);
  78. if (!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)) {
  79. printf("inet_pton() error\n");
  80. exit(-1);
  81. }
  82. if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
  83. printf("connect() error: %s\n", strerror(errno));
  84. exit(-1);
  85. }
  86. return fd;
  87. }
  88. static bool parse_opt(int argc, char **argv)
  89. {
  90. int option_index = 0;
  91. int c;
  92. static struct option long_options[] = {
  93. {"call", required_argument, 0, 'c'},
  94. {"port", required_argument, 0, 'p'},
  95. {"addr", required_argument, 0, 'a'},
  96. {"version", no_argument, 0, 'x'},
  97. {0, 0, 0, 0}};
  98. while (-1 != (c = getopt_long(argc, argv, "c:p:a:x", long_options,
  99. &option_index))) {
  100. switch (c) {
  101. case 'a':
  102. server_addr = optarg;
  103. break;
  104. case 'p':
  105. connection_port = atoi(optarg);
  106. break;
  107. case 'c':
  108. op_code = optarg;
  109. break;
  110. case 'x':
  111. puts(VERSION);
  112. exit(0);
  113. break;
  114. default:
  115. printf("%s", usage);
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. int sockfd;
  124. int n;
  125. int rc;
  126. char recvBuff[1024];
  127. if (!parse_opt(argc, argv))
  128. return -1;
  129. sockfd = client_connect();
  130. n = send(sockfd, op_code, strlen(op_code), 0);
  131. if (n < 0) {
  132. printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
  133. return -1;
  134. }
  135. n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
  136. if (n < 0) {
  137. printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
  138. return -1;
  139. }
  140. printf("rc: %d %s\n", rc, strerror(rc));
  141. if (rc)
  142. printf("\t%s\n", op_code);
  143. winsock_fini();
  144. return rc;
  145. }
  146. static int winsock_init(void)
  147. {
  148. #if WIN32
  149. int rc;
  150. static WSADATA wsaData;
  151. rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
  152. if (rc != 0) {
  153. return -1;
  154. }
  155. #endif
  156. return 0;
  157. }
  158. static void winsock_fini(void)
  159. {
  160. #if WIN32
  161. WSACleanup();
  162. #endif
  163. }
  164. #if WIN32
  165. static int inet_pton(int af, const char *src, void *dst)
  166. {
  167. struct sockaddr_storage ss;
  168. int size = sizeof(ss);
  169. char src_copy[INET6_ADDRSTRLEN + 1];
  170. ZeroMemory(&ss, sizeof(ss));
  171. /* stupid non-const API */
  172. strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
  173. src_copy[INET6_ADDRSTRLEN] = 0;
  174. if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss,
  175. &size) == 0) {
  176. switch (af) {
  177. case AF_INET:
  178. *(struct in_addr *)dst =
  179. ((struct sockaddr_in *)&ss)->sin_addr;
  180. return 1;
  181. case AF_INET6:
  182. *(struct in6_addr *)dst =
  183. ((struct sockaddr_in6 *)&ss)->sin6_addr;
  184. return 1;
  185. }
  186. }
  187. return 0;
  188. }
  189. #endif