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
35 #include <stdbool.h>
\r
39 #include <winsock2.h>
\r
40 #include <ws2tcpip.h>
\r
41 #include <windows.h>
\r
42 static int inet_pton(int af, const char *src, void *dst);
\r
45 #include <sys/socket.h>
\r
46 #include <netinet/in.h>
\r
47 #include <arpa/inet.h>
\r
48 #include <sys/types.h>
\r
51 static int winsock_init(void);
\r
52 static void winsock_fini(void);
\r
54 /**@brief Default server addres.*/
\r
55 static char *server_addr = "127.0.0.1";
\r
57 /**@brief Default connection port.*/
\r
58 static int connection_port = 1234;
\r
61 static char *op_code;
\r
63 static const char *usage = " \n\
\r
64 Welcome in lwext4_client. \n\
\r
65 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
\r
67 --call (-c) - call opt \n\
\r
68 --port (-p) - server port \n\
\r
69 --addr (-a) - server ip address \n\
\r
74 static int client_connect(void)
\r
77 struct sockaddr_in serv_addr;
\r
79 if(winsock_init() < 0) {
\r
80 printf("winsock_init error\n");
\r
84 memset(&serv_addr, '0', sizeof(serv_addr));
\r
85 fd = socket(AF_INET, SOCK_STREAM, 0);
\r
87 printf("socket() error: %s\n", strerror(errno));
\r
91 serv_addr.sin_family = AF_INET;
\r
92 serv_addr.sin_port = htons(connection_port);
\r
94 if(!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)){
\r
95 printf("inet_pton() error\n");
\r
99 if(connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))){
\r
100 printf("connect() error: %s\n", strerror(errno));
\r
109 static bool parse_opt(int argc, char **argv)
\r
111 int option_index = 0;
\r
114 static struct option long_options[] =
\r
116 {"call", required_argument, 0, 'c'},
\r
117 {"port", required_argument, 0, 'p'},
\r
118 {"addr", required_argument, 0, 'a'},
\r
122 while(-1 != (c = getopt_long (argc, argv, "c:p:a:", long_options, &option_index))) {
\r
126 server_addr = optarg;
\r
129 connection_port = atoi(optarg);
\r
135 printf("%s", usage);
\r
144 int main(int argc, char *argv[])
\r
149 char recvBuff[1024];
\r
151 if(!parse_opt(argc, argv))
\r
154 sockfd = client_connect();
\r
157 n = send(sockfd, op_code, strlen(op_code), 0);
\r
159 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
\r
163 n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
\r
165 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
\r
169 printf("rc: %d %s\n", rc, strerror(rc));
\r
171 printf("\t%s\n",op_code);
\r
176 static int winsock_init(void)
\r
180 static WSADATA wsaData;
\r
181 rc = WSAStartup(MAKEWORD(2,2), &wsaData);
\r
189 static void winsock_fini(void)
\r
198 static int inet_pton(int af, const char *src, void *dst)
\r
200 struct sockaddr_storage ss;
\r
201 int size = sizeof(ss);
\r
202 char src_copy[INET6_ADDRSTRLEN+1];
\r
204 ZeroMemory(&ss, sizeof(ss));
\r
205 /* stupid non-const API */
\r
206 strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
\r
207 src_copy[INET6_ADDRSTRLEN] = 0;
\r
209 if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
\r
212 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
\r
215 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
\r