-/*\r
- * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- *\r
- * - Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * - Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * - The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#include <unistd.h>\r
-#include <errno.h>\r
-#include <stdbool.h>\r
-#include <getopt.h>\r
-\r
-#ifdef WIN32\r
-#include <winsock2.h>\r
-#include <ws2tcpip.h>\r
-#include <windows.h>\r
-static int inet_pton(int af, const char *src, void *dst);\r
-\r
-#else\r
-#include <sys/socket.h>\r
-#include <netinet/in.h>\r
-#include <arpa/inet.h>\r
-#include <sys/types.h>\r
-#endif\r
-\r
-static int winsock_init(void);\r
-static void winsock_fini(void);\r
-\r
-/**@brief Default server addres.*/\r
-static char *server_addr = "127.0.0.1";\r
-\r
-/**@brief Default connection port.*/\r
-static int connection_port = 1234;\r
-\r
-/**@brief Call op*/\r
-static char *op_code;\r
-\r
-static const char *usage = " \n\\r
-Welcome in lwext4_client. \n\\r
-Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\\r
-Usage: \n\\r
- --call (-c) - call opt \n\\r
- --port (-p) - server port \n\\r
- --addr (-a) - server ip address \n\\r
-\n";\r
-\r
-\r
-\r
-static int client_connect(void)\r
-{\r
- int fd = 0;\r
- struct sockaddr_in serv_addr;\r
-\r
- if(winsock_init() < 0) {\r
- printf("winsock_init error\n");\r
- exit(-1);\r
- }\r
-\r
- memset(&serv_addr, '0', sizeof(serv_addr));\r
- fd = socket(AF_INET, SOCK_STREAM, 0);\r
- if(fd < 0) {\r
- printf("socket() error: %s\n", strerror(errno));\r
- exit(-1);\r
- }\r
-\r
- serv_addr.sin_family = AF_INET;\r
- serv_addr.sin_port = htons(connection_port);\r
-\r
- if(!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)){\r
- printf("inet_pton() error\n");\r
- exit(-1);\r
- }\r
-\r
- if(connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))){\r
- printf("connect() error: %s\n", strerror(errno));\r
- exit(-1);\r
- }\r
-\r
- winsock_fini();\r
- return fd;\r
-}\r
-\r
-\r
-\r
-static bool parse_opt(int argc, char **argv)\r
-{\r
- int option_index = 0;\r
- int c;\r
-\r
- static struct option long_options[] =\r
- {\r
- {"call", required_argument, 0, 'c'},\r
- {"port", required_argument, 0, 'p'},\r
- {"addr", required_argument, 0, 'a'},\r
- {0, 0, 0, 0}\r
- };\r
-\r
- while(-1 != (c = getopt_long (argc, argv, "c:p:a:", long_options, &option_index))) {\r
-\r
- switch(c){\r
- case 'a':\r
- server_addr = optarg;\r
- break;\r
- case 'p':\r
- connection_port = atoi(optarg);\r
- break;\r
- case 'c':\r
- op_code = optarg;\r
- break;\r
- default:\r
- printf("%s", usage);\r
- return false;\r
-\r
- }\r
- }\r
- return true;\r
-}\r
-\r
-\r
-int main(int argc, char *argv[])\r
-{\r
- int sockfd;\r
- int n;\r
- int rc;\r
- char recvBuff[1024];\r
-\r
- if(!parse_opt(argc, argv))\r
- return -1;\r
-\r
- sockfd = client_connect();\r
-\r
-\r
- n = send(sockfd, op_code, strlen(op_code), 0);\r
- if(n < 0) {\r
- printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);\r
- return -1;\r
- }\r
-\r
- n = recv(sockfd, (void *)&rc, sizeof(rc), 0);\r
- if(n < 0) {\r
- printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);\r
- return -1;\r
- }\r
-\r
- printf("rc: %d %s\n", rc, strerror(rc));\r
- if(rc)\r
- printf("\t%s\n",op_code);\r
-\r
- return rc;\r
-}\r
-\r
-static int winsock_init(void)\r
-{\r
-#if WIN32\r
- int rc;\r
- static WSADATA wsaData;\r
- rc = WSAStartup(MAKEWORD(2,2), &wsaData);\r
- if (rc != 0) {\r
- return -1;\r
- }\r
-#endif\r
- return 0;\r
-}\r
-\r
-static void winsock_fini(void)\r
-{\r
-#if WIN32\r
- WSACleanup();\r
-#endif\r
-}\r
-\r
-\r
-#if WIN32\r
-static int inet_pton(int af, const char *src, void *dst)\r
-{\r
- struct sockaddr_storage ss;\r
- int size = sizeof(ss);\r
- char src_copy[INET6_ADDRSTRLEN+1];\r
-\r
- ZeroMemory(&ss, sizeof(ss));\r
- /* stupid non-const API */\r
- strncpy (src_copy, src, INET6_ADDRSTRLEN+1);\r
- src_copy[INET6_ADDRSTRLEN] = 0;\r
-\r
- if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {\r
- switch(af) {\r
- case AF_INET:\r
- *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;\r
- return 1;\r
- case AF_INET6:\r
- *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;\r
- return 1;\r
- }\r
- }\r
- return 0;\r
-}\r
-#endif\r
+/*
+ * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <getopt.h>
+
+#ifdef WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <windows.h>
+static int inet_pton(int af, const char *src, void *dst);
+
+#else
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#endif
+
+static int winsock_init(void);
+static void winsock_fini(void);
+
+/**@brief Default server addres.*/
+static char *server_addr = "127.0.0.1";
+
+/**@brief Default connection port.*/
+static int connection_port = 1234;
+
+/**@brief Call op*/
+static char *op_code;
+
+static const char *usage = " \n\
+Welcome in lwext4_client. \n\
+Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
+Usage: \n\
+ --call (-c) - call opt \n\
+ --port (-p) - server port \n\
+ --addr (-a) - server ip address \n\
+\n";
+
+static int client_connect(void)
+{
+ int fd = 0;
+ struct sockaddr_in serv_addr;
+
+ if (winsock_init() < 0) {
+ printf("winsock_init error\n");
+ exit(-1);
+ }
+
+ memset(&serv_addr, '0', sizeof(serv_addr));
+ fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (fd < 0) {
+ printf("socket() error: %s\n", strerror(errno));
+ exit(-1);
+ }
+
+ serv_addr.sin_family = AF_INET;
+ serv_addr.sin_port = htons(connection_port);
+
+ if (!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)) {
+ printf("inet_pton() error\n");
+ exit(-1);
+ }
+
+ if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
+ printf("connect() error: %s\n", strerror(errno));
+ exit(-1);
+ }
+
+ return fd;
+}
+
+static bool parse_opt(int argc, char **argv)
+{
+ int option_index = 0;
+ int c;
+
+ static struct option long_options[] = {
+ {"call", required_argument, 0, 'c'},
+ {"port", required_argument, 0, 'p'},
+ {"addr", required_argument, 0, 'a'},
+ {0, 0, 0, 0}};
+
+ while (-1 != (c = getopt_long(argc, argv, "c:p:a:", long_options,
+ &option_index))) {
+
+ switch (c) {
+ case 'a':
+ server_addr = optarg;
+ break;
+ case 'p':
+ connection_port = atoi(optarg);
+ break;
+ case 'c':
+ op_code = optarg;
+ break;
+ default:
+ printf("%s", usage);
+ return false;
+ }
+ }
+ return true;
+}
+
+int main(int argc, char *argv[])
+{
+ int sockfd;
+ int n;
+ int rc;
+ char recvBuff[1024];
+
+ if (!parse_opt(argc, argv))
+ return -1;
+
+ sockfd = client_connect();
+
+ n = send(sockfd, op_code, strlen(op_code), 0);
+ if (n < 0) {
+ printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
+ return -1;
+ }
+
+ n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
+ if (n < 0) {
+ printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
+ return -1;
+ }
+
+ printf("rc: %d %s\n", rc, strerror(rc));
+ if (rc)
+ printf("\t%s\n", op_code);
+
+ winsock_fini();
+ return rc;
+}
+
+static int winsock_init(void)
+{
+#if WIN32
+ int rc;
+ static WSADATA wsaData;
+ rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
+ if (rc != 0) {
+ return -1;
+ }
+#endif
+ return 0;
+}
+
+static void winsock_fini(void)
+{
+#if WIN32
+ WSACleanup();
+#endif
+}
+
+#if WIN32
+static int inet_pton(int af, const char *src, void *dst)
+{
+ struct sockaddr_storage ss;
+ int size = sizeof(ss);
+ char src_copy[INET6_ADDRSTRLEN + 1];
+
+ ZeroMemory(&ss, sizeof(ss));
+ /* stupid non-const API */
+ strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
+ src_copy[INET6_ADDRSTRLEN] = 0;
+
+ if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss,
+ &size) == 0) {
+ switch (af) {
+ case AF_INET:
+ *(struct in_addr *)dst =
+ ((struct sockaddr_in *)&ss)->sin_addr;
+ return 1;
+ case AF_INET6:
+ *(struct in6_addr *)dst =
+ ((struct sockaddr_in6 *)&ss)->sin6_addr;
+ return 1;
+ }
+ }
+ return 0;
+}
+#endif