10 #include <winsock2.h>
\r
11 #include <ws2tcpip.h>
\r
12 #include <windows.h>
\r
13 static int inet_pton(int af, const char *src, void *dst);
\r
16 #include <sys/socket.h>
\r
17 #include <netinet/in.h>
\r
18 #include <arpa/inet.h>
\r
19 #include <sys/types.h>
\r
22 static int winsock_init(void);
\r
23 static void winsock_fini(void);
\r
25 /**@brief Default server addres.*/
\r
26 static char *server_addr = "127.0.0.1";
\r
28 /**@brief Default connection port.*/
\r
29 static int connection_port = 1234;
\r
32 static char *op_code;
\r
34 static const char *usage = " \n\
\r
35 Welcome in lwext4_client. \n\
\r
36 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
\r
38 --call (-c) - call opt \n\
\r
39 --port (-p) - server port \n\
\r
40 --addr (-a) - server ip address \n\
\r
45 static int client_connect(void)
\r
48 struct sockaddr_in serv_addr;
\r
50 if(winsock_init() < 0) {
\r
51 printf("winsock_init error\n");
\r
55 memset(&serv_addr, '0', sizeof(serv_addr));
\r
56 fd = socket(AF_INET, SOCK_STREAM, 0);
\r
58 printf("socket() error: %s\n", strerror(errno));
\r
62 serv_addr.sin_family = AF_INET;
\r
63 serv_addr.sin_port = htons(connection_port);
\r
65 if(!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)){
\r
66 printf("inet_pton() error\n");
\r
70 if(connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))){
\r
71 printf("connect() error: %s\n", strerror(errno));
\r
80 static bool parse_opt(int argc, char **argv)
\r
82 int option_index = 0;
\r
85 static struct option long_options[] =
\r
87 {"call", required_argument, 0, 'c'},
\r
88 {"port", required_argument, 0, 'p'},
\r
89 {"addr", required_argument, 0, 'a'},
\r
93 while(-1 != (c = getopt_long (argc, argv, "c:p:a:", long_options, &option_index))) {
\r
97 server_addr = optarg;
\r
100 connection_port = atoi(optarg);
\r
106 printf("%s", usage);
\r
115 int main(int argc, char *argv[])
\r
120 char recvBuff[1024];
\r
122 if(!parse_opt(argc, argv))
\r
125 sockfd = client_connect();
\r
128 n = send(sockfd, op_code, strlen(op_code), 0);
\r
130 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
\r
134 n = recv(sockfd, (void *)&rc, sizeof(rc), 0);
\r
136 printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd);
\r
140 printf("rc: %d %s\n", rc, strerror(rc));
\r
142 printf("\t%s\n",op_code);
\r
147 static int winsock_init(void)
\r
151 static WSADATA wsaData;
\r
152 rc = WSAStartup(MAKEWORD(2,2), &wsaData);
\r
160 static void winsock_fini(void)
\r
169 static int inet_pton(int af, const char *src, void *dst)
\r
171 struct sockaddr_storage ss;
\r
172 int size = sizeof(ss);
\r
173 char src_copy[INET6_ADDRSTRLEN+1];
\r
175 ZeroMemory(&ss, sizeof(ss));
\r
176 /* stupid non-const API */
\r
177 strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
\r
178 src_copy[INET6_ADDRSTRLEN] = 0;
\r
180 if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
\r
183 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
\r
186 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
\r