ext4: remove block cache parameter from device_register function
[lwext4.git] / fs_test / lwext4_client.c
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
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <stdbool.h>
35 #include <getopt.h>
36
37 #ifdef WIN32
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 #include <windows.h>
41 static int inet_pton(int af, const char *src, void *dst);
42
43 #else
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/types.h>
48 #endif
49
50 static int winsock_init(void);
51 static void winsock_fini(void);
52
53 /**@brief   Default server addres.*/
54 static char *server_addr = "127.0.0.1";
55
56 /**@brief   Default connection port.*/
57 static int connection_port = 1234;
58
59 /**@brief   Call op*/
60 static char *op_code;
61
62 static const char *usage = "                                    \n\
63 Welcome in lwext4_client.                                       \n\
64 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
65 Usage:                                                          \n\
66     --call (-c) - call opt                                      \n\
67     --port (-p) - server port                                   \n\
68     --addr (-a) - server ip address                             \n\
69 \n";
70
71 static int client_connect(void)
72 {
73         int fd = 0;
74         struct sockaddr_in serv_addr;
75
76         if (winsock_init() < 0) {
77                 printf("winsock_init error\n");
78                 exit(-1);
79         }
80
81         memset(&serv_addr, '0', sizeof(serv_addr));
82         fd = socket(AF_INET, SOCK_STREAM, 0);
83         if (fd < 0) {
84                 printf("socket() error: %s\n", strerror(errno));
85                 exit(-1);
86         }
87
88         serv_addr.sin_family = AF_INET;
89         serv_addr.sin_port = htons(connection_port);
90
91         if (!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)) {
92                 printf("inet_pton() error\n");
93                 exit(-1);
94         }
95
96         if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
97                 printf("connect() error: %s\n", strerror(errno));
98                 exit(-1);
99         }
100
101         return fd;
102 }
103
104 static bool parse_opt(int argc, char **argv)
105 {
106         int option_index = 0;
107         int c;
108
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'},
114                         {0, 0, 0, 0}};
115
116         while (-1 != (c = getopt_long(argc, argv, "c:p:a:x", long_options,
117                         &option_index))) {
118
119                 switch (c) {
120                 case 'a':
121                         server_addr = optarg;
122                         break;
123                 case 'p':
124                         connection_port = atoi(optarg);
125                         break;
126                 case 'c':
127                         op_code = optarg;
128                         break;
129                 case 'x':
130                         puts(VERSION);
131                         exit(0);
132                         break;
133                 default:
134                         printf("%s", usage);
135                         return false;
136                 }
137         }
138         return true;
139 }
140
141 int main(int argc, char *argv[])
142 {
143         int sockfd;
144         int n;
145         int rc;
146         char recvBuff[1024];
147
148         if (!parse_opt(argc, argv))
149                 return -1;
150
151         sockfd = client_connect();
152
153         n = send(sockfd, op_code, strlen(op_code), 0);
154         if (n < 0) {
155                 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
156                 return -1;
157         }
158
159         n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
160         if (n < 0) {
161                 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
162                 return -1;
163         }
164
165         printf("rc: %d %s\n", rc, strerror(rc));
166         if (rc)
167                 printf("\t%s\n", op_code);
168
169         winsock_fini();
170         return rc;
171 }
172
173 static int winsock_init(void)
174 {
175 #if WIN32
176         int rc;
177         static WSADATA wsaData;
178         rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
179         if (rc != 0) {
180                 return -1;
181         }
182 #endif
183         return 0;
184 }
185
186 static void winsock_fini(void)
187 {
188 #if WIN32
189         WSACleanup();
190 #endif
191 }
192
193 #if WIN32
194 static int inet_pton(int af, const char *src, void *dst)
195 {
196         struct sockaddr_storage ss;
197         int size = sizeof(ss);
198         char src_copy[INET6_ADDRSTRLEN + 1];
199
200         ZeroMemory(&ss, sizeof(ss));
201         /* stupid non-const API */
202         strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
203         src_copy[INET6_ADDRSTRLEN] = 0;
204
205         if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss,
206                         &size) == 0) {
207                 switch (af) {
208                 case AF_INET:
209                         *(struct in_addr *)dst =
210                                         ((struct sockaddr_in *)&ss)->sin_addr;
211                         return 1;
212                 case AF_INET6:
213                         *(struct in6_addr *)dst =
214                                         ((struct sockaddr_in6 *)&ss)->sin6_addr;
215                         return 1;
216                 }
217         }
218         return 0;
219 }
220 #endif