2 * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
34 #include <stdbool.h>
\r
38 #include <winsock2.h>
\r
39 #include <ws2tcpip.h>
\r
40 #include <windows.h>
\r
41 static int inet_pton(int af, const char *src, void *dst);
\r
44 #include <sys/socket.h>
\r
45 #include <netinet/in.h>
\r
46 #include <arpa/inet.h>
\r
47 #include <sys/types.h>
\r
50 static int winsock_init(void);
\r
51 static void winsock_fini(void);
\r
53 /**@brief Default server addres.*/
\r
54 static char *server_addr = "127.0.0.1";
\r
56 /**@brief Default connection port.*/
\r
57 static int connection_port = 1234;
\r
60 static char *op_code;
\r
62 static const char *usage = " \n\
\r
63 Welcome in lwext4_client. \n\
\r
64 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
\r
66 --call (-c) - call opt \n\
\r
67 --port (-p) - server port \n\
\r
68 --addr (-a) - server ip address \n\
\r
71 static int client_connect(void)
\r
74 struct sockaddr_in serv_addr;
\r
76 if (winsock_init() < 0) {
\r
77 printf("winsock_init error\n");
\r
81 memset(&serv_addr, '0', sizeof(serv_addr));
\r
82 fd = socket(AF_INET, SOCK_STREAM, 0);
\r
84 printf("socket() error: %s\n", strerror(errno));
\r
88 serv_addr.sin_family = AF_INET;
\r
89 serv_addr.sin_port = htons(connection_port);
\r
91 if (!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)) {
\r
92 printf("inet_pton() error\n");
\r
96 if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
\r
97 printf("connect() error: %s\n", strerror(errno));
\r
104 static bool parse_opt(int argc, char **argv)
\r
106 int option_index = 0;
\r
109 static struct option long_options[] = {
\r
110 {"call", required_argument, 0, 'c'},
\r
111 {"port", required_argument, 0, 'p'},
\r
112 {"addr", required_argument, 0, 'a'},
\r
115 while (-1 != (c = getopt_long(argc, argv, "c:p:a:", long_options,
\r
120 server_addr = optarg;
\r
123 connection_port = atoi(optarg);
\r
129 printf("%s", usage);
\r
136 int main(int argc, char *argv[])
\r
141 char recvBuff[1024];
\r
143 if (!parse_opt(argc, argv))
\r
146 sockfd = client_connect();
\r
148 n = send(sockfd, op_code, strlen(op_code), 0);
\r
150 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
\r
154 n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
\r
156 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
\r
160 printf("rc: %d %s\n", rc, strerror(rc));
\r
162 printf("\t%s\n", op_code);
\r
168 static int winsock_init(void)
\r
172 static WSADATA wsaData;
\r
173 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
\r
181 static void winsock_fini(void)
\r
189 static int inet_pton(int af, const char *src, void *dst)
\r
191 struct sockaddr_storage ss;
\r
192 int size = sizeof(ss);
\r
193 char src_copy[INET6_ADDRSTRLEN + 1];
\r
195 ZeroMemory(&ss, sizeof(ss));
\r
196 /* stupid non-const API */
\r
197 strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
\r
198 src_copy[INET6_ADDRSTRLEN] = 0;
\r
200 if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss,
\r
204 *(struct in_addr *)dst =
\r
205 ((struct sockaddr_in *)&ss)->sin_addr;
\r
208 *(struct in6_addr *)dst =
\r
209 ((struct sockaddr_in6 *)&ss)->sin6_addr;
\r