-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths.c
176 lines (146 loc) · 4.22 KB
/
s.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (C) 2016-2021 Davidson Francis <davidsondfgl@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* clang-format off */
#ifndef _WIN32
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
typedef int socklen_t;
#endif
/* clang-format on */
/* Windows and macOS seems to not have MSG_NOSIGNAL */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#include <unistd.h>
#include "ws/ws.h"
#include "ws/utf8.h"
void onmessage(struct ws_frame_data *wfd, const unsigned char *msg, uint64_t size, int type) {
#ifndef disable_verbose
printf("i receive a message: %s (size: %lu, type: %d), from: %s:%d/%d\n",
msg, size, type, wfd->ip, wfd->port, wfd->sock);
#endif
/**
* mimicks the same frame type received and re-send it again
*
* please note that we could just use a ws_sendframe_txt()
* or ws_sendframe_bin() here, but we're just being safe
* and re-sending the very same frame type and content
* again.
*/
//ws_sendframe(wfd->sock, (char *)msg, size, type);
}
int main(int argc, char **argv) {
struct sockaddr_in serveraddr;
if(argc != 3) {
printf("usage: ./c <ip> <port>\n");
return 0;
}
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
if(inet_pton(AF_INET, argv[1], &serveraddr.sin_addr) <= 0){
logd("inet_pton error");
abort();
}
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
logd("WSAStartup failed!");
abort();
}
/**
* Sets stdout to be non-buffered.
*
* According to the docs from MSDN (setvbuf page), Windows do not
* really supports line buffering but full-buffering instead.
*
* Quote from the docs:
* "... _IOLBF For some systems, this provides line buffering.
* However, for Win32, the behavior is the same as _IOFBF"
*/
setvbuf(stdout, NULL, _IONBF, 0);
#endif
int serversock = socket(AF_INET, SOCK_STREAM, 0);
if (serversock < 0) {
logd("Could not create socket");
abort();
}
int reuse = 1;
/* Reuse previous address. */
if (setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)) < 0) {
logd("setsockopt(SO_REUSEADDR) failed");
abort();
}
if (connect(serversock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
logd("Failed to connect");
return 0;
}
ws_frame_data_t *wfd = ws_establishconnection(serversock);
/* Do handshake. */
if (!do_handshake(wfd)) {
goto closed;
}
if (!finish_handshake(wfd)) {
goto closed;
}
/* Read next frame until client disconnects or an error occur. */
char buf[4096] = {0};
fprintf(stderr, "[%d]: wait for data to send ...\n", __LINE__);
while (scanf("%s", buf) != EOF) {
fprintf(stderr, "Buffer [%d]: %s\n", (int)strlen(buf), buf);
ws_sendframe_txt(wfd->sock, buf);
if(next_frame(wfd) >= 0) {
onmessage(wfd, wfd->msg, wfd->frame_size, wfd->frame_type);
}
if (wfd->frame_type == WS_FR_OP_CLSE && !wfd->error) {
/* Close event. */
/*
* We only send a CLOSE frame once, if we're already
* in CLOSING state, there is no need to send.
*/
if (wfd->state != WS_STATE_CLOSING) {
wfd->state = WS_STATE_CLOSING;
/* We only send a close frameSend close frame */
do_close(wfd, -1);
goto closed;
}
free(wfd->msg);
break;
}
free(wfd->msg);
}
closed:
logd("Closed...");
if(wfd) free(wfd);
return (0);
}