-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathconnection.c
More file actions
223 lines (187 loc) · 5.82 KB
/
connection.c
File metadata and controls
223 lines (187 loc) · 5.82 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* DroidCam & DroidCamX (C) 2010-2021
* https://github.com/dev47apps
*
* 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.
*/
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include "common.h"
#include "connection.h"
SOCKET wifiServerSocket = INVALID_SOCKET;
extern bool v_running;
const char* DROIDCAM_CONNECT_ERROR = \
"Connect failed, please try again.\n"
"Check IP and Port.\n"
"Check network connection.\n";
SOCKET Connect(const char* ip, int port, const char **errormsg) {
int flags;
struct sockaddr_in sin;
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
dbgprint("connect to %s:%d\n", ip, port);
if(sock == INVALID_SOCKET) {
errprint("socket() error %d '%s'\n", errno, strerror(errno));
*errormsg = strerror(errno);
goto _error_out;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ip);
sin.sin_port = htons(port);
flags = fcntl(sock, F_GETFL, NULL);
if(flags < 0) {
errprint("fcntl() error %d '%s'\n", errno, strerror(errno));
*errormsg = strerror(errno);
close(sock);
sock = INVALID_SOCKET;
goto _error_out;
}
flags |= O_NONBLOCK;
fcntl(sock, F_SETFL, flags);
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
fd_set set;
FD_ZERO(&set);
FD_SET(sock, &set);
connect(sock, (struct sockaddr*)&sin, sizeof(sin));
if (!(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS) || (select(sock+1, NULL, &set, NULL, &timeout) <= 0)) {
dbgprint("connect timeout/error=%d '%s'\n", errno, strerror(errno));
*errormsg = DROIDCAM_CONNECT_ERROR;
close(sock);
sock = INVALID_SOCKET;
goto _error_out;
}
flags = fcntl(sock, F_GETFL, NULL);
flags &= ~O_NONBLOCK;
fcntl(sock, F_SETFL, flags);
timeout.tv_sec = 5;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0
|| setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
perror("setsockopt failed");
_error_out:
dbgprint(" - return fd: %d\n", sock);
return sock;
}
bool Send(const char * buffer, int bytes, SOCKET s) {
ssize_t w = 0;
char *ptr = (char*) buffer;
while (bytes > 0) {
w = send(s, ptr, bytes, 0);
if (w <= 0) {
return false;
}
bytes -= w;
ptr += w;
}
return true;
}
ssize_t Recv(const char* buffer, int bytes, SOCKET s) {
return recv(s, (char*)buffer, bytes, 0);
}
ssize_t RecvAll(const char* buffer, int bytes, SOCKET s) {
return recv(s, (char*)buffer, bytes, MSG_WAITALL);
}
ssize_t RecvNonBlock(char * buffer, int bytes, SOCKET s) {
int res = recv(s, buffer, bytes, MSG_DONTWAIT);
return (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) ? 0 : res;
}
ssize_t RecvNonBlockUDP(char * buffer, int bytes, SOCKET s) {
struct sockaddr_in from;
socklen_t fromLen = sizeof(from);
int res = recvfrom(s, buffer, bytes, MSG_DONTWAIT, (struct sockaddr *)&from, &fromLen);
return (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) ? 0 : res;
}
ssize_t SendUDPMessage(SOCKET s, const char *message, int length, char *ip, int port) {
struct sockaddr_in sin;
sin.sin_port = htons((uint16_t)port);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ip);
return sendto(s, message, length, 0, (struct sockaddr *)&sin, sizeof(sin));
}
SOCKET CreateUdpSocket(void) {
return socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
static int StartInetServer(int port)
{
int flags = 0;
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
wifiServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(wifiServerSocket == INVALID_SOCKET)
{
MSG_LASTERROR("Could not create socket");
goto _error_out;
}
if(bind(wifiServerSocket, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
MSG_LASTERROR("Error: bind");
goto _error_out;
}
if(listen(wifiServerSocket, 1) < 0)
{
MSG_LASTERROR("Error: listen");
goto _error_out;
}
flags = fcntl(wifiServerSocket, F_GETFL, NULL);
if(flags < 0)
{
MSG_LASTERROR("Error: fcntl");
goto _error_out;
}
flags |= O_NONBLOCK;
fcntl(wifiServerSocket, F_SETFL, flags);
return 1;
_error_out:
if (wifiServerSocket != INVALID_SOCKET){
close(wifiServerSocket);
wifiServerSocket = INVALID_SOCKET;
}
return 0;
}
void connection_cleanup() {
if (wifiServerSocket != INVALID_SOCKET) {
close(wifiServerSocket);
wifiServerSocket = INVALID_SOCKET;
}
}
void disconnect(SOCKET s) {
close(s);
}
SOCKET accept_connection(int port)
{
int flags;
SOCKET client = INVALID_SOCKET;
dbgprint("serverSocket=%d\n", wifiServerSocket);
if (wifiServerSocket == INVALID_SOCKET && !StartInetServer(port))
goto _error_out;
errprint("waiting on port %d..", port);
while(v_running && (client = accept(wifiServerSocket, NULL, NULL)) == INVALID_SOCKET)
{
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR){
usleep(50000);
continue;
}
MSG_LASTERROR("Accept Failed");
break;
}
errprint("got socket %d\n", client);
if (client != INVALID_SOCKET) {
// Blocking..
flags = fcntl(wifiServerSocket, F_GETFL, NULL);
flags |= O_NONBLOCK;
fcntl(wifiServerSocket, F_SETFL, flags);
}
_error_out:
return client;
}