2 * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
41 static int inet_pton(int af, const char *src, void *dst);
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/types.h>
50 static int winsock_init(void);
51 static void winsock_fini(void);
53 /**@brief Default server addres.*/
54 static char *server_addr = "127.0.0.1";
56 /**@brief Default connection port.*/
57 static int connection_port = 1234;
62 static const char *usage = " \n\
63 Welcome in lwext4_client. \n\
64 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
66 --call (-c) - call opt \n\
67 --port (-p) - server port \n\
68 --addr (-a) - server ip address \n\
71 static int client_connect(void)
74 struct sockaddr_in serv_addr;
76 if (winsock_init() < 0) {
77 printf("winsock_init error\n");
81 memset(&serv_addr, '0', sizeof(serv_addr));
82 fd = socket(AF_INET, SOCK_STREAM, 0);
84 printf("socket() error: %s\n", strerror(errno));
88 serv_addr.sin_family = AF_INET;
89 serv_addr.sin_port = htons(connection_port);
91 if (!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)) {
92 printf("inet_pton() error\n");
96 if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
97 printf("connect() error: %s\n", strerror(errno));
104 static bool parse_opt(int argc, char **argv)
106 int option_index = 0;
109 static struct option long_options[] = {
110 {"call", required_argument, 0, 'c'},
111 {"port", required_argument, 0, 'p'},
112 {"addr", required_argument, 0, 'a'},
113 {"version", no_argument, 0, 'x'},
116 while (-1 != (c = getopt_long(argc, argv, "c:p:a:x", long_options,
121 server_addr = optarg;
124 connection_port = atoi(optarg);
141 int main(int argc, char *argv[])
148 if (!parse_opt(argc, argv))
151 sockfd = client_connect();
153 n = send(sockfd, op_code, strlen(op_code), 0);
155 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
159 n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
161 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
165 printf("rc: %d %s\n", rc, strerror(rc));
167 printf("\t%s\n", op_code);
173 static int winsock_init(void)
177 static WSADATA wsaData;
178 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
186 static void winsock_fini(void)
194 static int inet_pton(int af, const char *src, void *dst)
196 struct sockaddr_storage ss;
197 int size = sizeof(ss);
198 char src_copy[INET6_ADDRSTRLEN + 1];
200 ZeroMemory(&ss, sizeof(ss));
201 /* stupid non-const API */
202 strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
203 src_copy[INET6_ADDRSTRLEN] = 0;
205 if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss,
209 *(struct in_addr *)dst =
210 ((struct sockaddr_in *)&ss)->sin_addr;
213 *(struct in6_addr *)dst =
214 ((struct sockaddr_in6 *)&ss)->sin6_addr;