FIX: ext4_extent_full.c and ext4_extent.c lack endian conversion when doing checksum.
[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                         {0, 0, 0, 0}};
114
115         while (-1 != (c = getopt_long(argc, argv, "c:p:a:", long_options,
116                         &option_index))) {
117
118                 switch (c) {
119                 case 'a':
120                         server_addr = optarg;
121                         break;
122                 case 'p':
123                         connection_port = atoi(optarg);
124                         break;
125                 case 'c':
126                         op_code = optarg;
127                         break;
128                 default:
129                         printf("%s", usage);
130                         return false;
131                 }
132         }
133         return true;
134 }
135
136 int main(int argc, char *argv[])
137 {
138         int sockfd;
139         int n;
140         int rc;
141         char recvBuff[1024];
142
143         if (!parse_opt(argc, argv))
144                 return -1;
145
146         sockfd = client_connect();
147
148         n = send(sockfd, op_code, strlen(op_code), 0);
149         if (n < 0) {
150                 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
151                 return -1;
152         }
153
154         n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
155         if (n < 0) {
156                 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
157                 return -1;
158         }
159
160         printf("rc: %d %s\n", rc, strerror(rc));
161         if (rc)
162                 printf("\t%s\n", op_code);
163
164         winsock_fini();
165         return rc;
166 }
167
168 static int winsock_init(void)
169 {
170 #if WIN32
171         int rc;
172         static WSADATA wsaData;
173         rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
174         if (rc != 0) {
175                 return -1;
176         }
177 #endif
178         return 0;
179 }
180
181 static void winsock_fini(void)
182 {
183 #if WIN32
184         WSACleanup();
185 #endif
186 }
187
188 #if WIN32
189 static int inet_pton(int af, const char *src, void *dst)
190 {
191         struct sockaddr_storage ss;
192         int size = sizeof(ss);
193         char src_copy[INET6_ADDRSTRLEN + 1];
194
195         ZeroMemory(&ss, sizeof(ss));
196         /* stupid non-const API */
197         strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
198         src_copy[INET6_ADDRSTRLEN] = 0;
199
200         if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss,
201                         &size) == 0) {
202                 switch (af) {
203                 case AF_INET:
204                         *(struct in_addr *)dst =
205                                         ((struct sockaddr_in *)&ss)->sin_addr;
206                         return 1;
207                 case AF_INET6:
208                         *(struct in6_addr *)dst =
209                                         ((struct sockaddr_in6 *)&ss)->sin6_addr;
210                         return 1;
211                 }
212         }
213         return 0;
214 }
215 #endif