-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathttysocket.c
More file actions
368 lines (353 loc) · 10.2 KB
/
ttysocket.c
File metadata and controls
368 lines (353 loc) · 10.2 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* This file is part of the ttyterm project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@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/>.
*/
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h> // unix socket
#include "dbg.h"
#include "string_functions.h"
#include "ttysocket.h"
static int sec = 0, usec = 100; // timeout
static FILE *dupfile = NULL; // file for output
static chardevice *device = NULL; // current opened device
// TODO: if unix socket name starts with \0 translate it as \\0 to d->name!
// set Read_tty timeout in milliseconds
void settimeout(int tmout){
sec = 0;
if(tmout > 999){
sec = tmout / 1000;
tmout -= sec * 1000;
}
usec = tmout * 1000L;
}
/**
* wait for answer from socket
* @param sock - socket fd
* @return 0 in case of timeout, 1 in case of socket ready, -1 if error
*/
static int waittoread(int fd){
fd_set fds;
struct timeval timeout;
timeout.tv_sec = sec;
timeout.tv_usec = usec;
FD_ZERO(&fds);
FD_SET(fd, &fds);
do{
int rc = select(fd+1, &fds, NULL, NULL, &timeout);
if(rc < 0){
if(errno != EINTR){
WARN("select()");
return -1;
}
continue;
}
break;
}while(1);
if(FD_ISSET(fd, &fds)){
//DBG("FD_ISSET");
return 1;
}
return 0;
}
// get data drom TTY
static uint8_t *getttydata(int *len){
if(!device || !device->dev) return NULL;
sl_tty_t *D = device->dev;
if(D->comfd < 0) return NULL;
int L = 0;
int length = D->bufsz - 1; // -1 for terminating zero
uint8_t *ptr = (uint8_t*)D->buf;
int s = 0;
do{
if(!(s = waittoread(D->comfd))) break;
if(s < 0){
if(len) *len = 0;
return NULL;
}
int l = read(D->comfd, ptr, length);
if(l < 1){ // disconnected
if(len) *len = -1;
return NULL;
}
ptr += l; L += l;
length -= l;
}while(length);
D->buflen = L;
D->buf[L] = 0; // for text buffers
if(len) *len = L;
if(!L) return NULL;
DBG("buffer len: %zd, content: =%s=", D->buflen, D->buf);
return (uint8_t*)D->buf;
}
static uint8_t *getsockdata(int *len){
if(!device || !device->dev) return NULL;
sl_tty_t *D = device->dev;
if(D->comfd < 0) return NULL;
uint8_t *ptr = NULL;
int n = waittoread(D->comfd);
if(n == 1){
n = read(D->comfd, D->buf, D->bufsz-1);
if(n > 0){
ptr = (uint8_t*)D->buf;
ptr[n] = 0;
D->buflen = n;
DBG("got %d: ..%s..", n, ptr);
}else{
DBG("Got nothing");
n = -1;
}
}
if(len) *len = n;
return ptr;
}
/**
* @brief ReadData - get data from serial device or socket
* @param d - device
* @param len (o) - length of data read (-1 if device disconnected)
* @return NULL or string
*/
uint8_t *ReadData(int *len){
if(!device || !device->dev) return NULL;
if(len) *len = -1;
uint8_t *r = NULL;
switch(device->type){
case DEV_TTY:
r = getttydata(len);
break;
case DEV_NETSOCKET:
case DEV_UNIXSOCKET:
r = getsockdata(len);
break;
default:
break;
}
if(r && dupfile){
fwrite("< ", 1, 2, dupfile);
fwrite(r, 1, *len, dupfile);
}
return r;
}
/**
* @brief SendData - send data to tty or socket
* @param d - device
* @param data - buffer with data
* @return 0 if error or empty string, -1 if disconnected
*/
int SendData(const uint8_t *data, size_t len){
if(!device) return -1;
if(!data || len == 0) return 0;
int ret = 0;
DBG("Send %d bytes", len);
if(0 == pthread_mutex_lock(&device->mutex)){
switch(device->type){
case DEV_TTY:
if(sl_tty_write(device->dev->comfd, (const char*)data, len)) ret = 0;
else ret = len;
break;
case DEV_NETSOCKET:
case DEV_UNIXSOCKET:
if(len != (size_t)send(device->dev->comfd, data, len, MSG_NOSIGNAL)) ret = 0;
else ret = len;
break;
default:
data = NULL;
break;
}
if(data && dupfile){
fwrite("> ", 1, 2, dupfile);
fwrite(data, 1, len, dupfile);
}
pthread_mutex_unlock(&device->mutex);
}else ret = -1;
DBG("ret=%d", ret);
return ret;
}
static const int socktypes[] = {SOCK_STREAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET, SOCK_DCCP, SOCK_PACKET, SOCK_DGRAM, 0};
static sl_tty_t* opensocket(){
if(!device) return FALSE;
sl_tty_t *descr = MALLOC(sl_tty_t, 1); // only for `buf` and bufsz/buflen
descr->buf = MALLOC(char, BUFSIZ);
descr->bufsz = BUFSIZ;
// now try to open a socket
descr->comfd = -1;
struct hostent *host;
struct sockaddr_in addr = {0};
struct sockaddr_un saddr = {0};
struct sockaddr *sa = NULL;
socklen_t addrlen = 0;
int domain = -1;
if(device->type == DEV_NETSOCKET){
DBG("NETSOCK to %s", device->name);
sa = (struct sockaddr*) &addr;
addrlen = sizeof(addr);
if((host = gethostbyname(device->name)) == NULL ){
WARN("gethostbyname()");
FREE(descr->buf);
FREE(descr);
return NULL;
}
struct in_addr *ia = (struct in_addr*)host->h_addr_list[0];
DBG("addr: %s", inet_ntoa(*ia));
addr.sin_family = AF_INET;
int p = atoi(device->port); DBG("PORT: %s - %d", device->port, p);
addr.sin_port = htons(p);
//addr.sin_addr.s_addr = *(long*)(host->h_addr);
addr.sin_addr.s_addr = ia->s_addr;
domain = AF_INET;
}else{
DBG("UNSOCK");
sa = (struct sockaddr*) &saddr;
addrlen = sizeof(saddr);
saddr.sun_family = AF_UNIX;
if(*(device->name) == 0){ // if sun_path[0] == 0 then don't create a file
DBG("convert name");
saddr.sun_path[0] = 0;
strncpy(saddr.sun_path+1, device->name+1, 105);
}
else if(strncmp("\\0", device->name, 2) == 0){
DBG("convert name");
saddr.sun_path[0] = 0;
strncpy(saddr.sun_path+1, device->name+2, 105);
}else strncpy(saddr.sun_path, device->name, 106);
domain = AF_UNIX;
}
const int *type = socktypes;
while(*type){
DBG("type = %d", *type);
if((descr->comfd = socket(domain, *type, 0)) > -1){
if(connect(descr->comfd, sa, addrlen) < 0){
DBG("CANT connect");
close(descr->comfd);
}else break;
}
++type;
}
if(descr->comfd < 0){
DBG("NO types");
WARNX("No types can be choosen");
FREE(descr->buf);
FREE(descr);
return NULL;
}
return descr;
}
static sl_tty_t* opentty(){
if(!device->name){
/// ïÔÓÕÔÓÔ×ÕÅÔ ÉÍÑ ÐÏÒÔÁ
WARNX(_("Port name is missing"));
return NULL;
}
sl_tty_t *descr = sl_tty_new(device->name, device->speed, 4096);
if(!descr){
WARN("Can't init %s", device->name);
return NULL;
}
descr->format = device->port;
descr = sl_tty_open(descr, device->exclusive);
return descr;
}
/**
* @brief opendev - open TTY or socket output device
* @param d - device type
* @return FALSE if failed
*/
int opendev(chardevice *d, char *path){
if(!d) return FALSE;
DBG("Try to open device");
device = MALLOC(chardevice, 1);
memcpy(device, d, sizeof(chardevice));
device->name = strdup(d->name);
if(d->port) device->port = strdup(d->port);
DBG("devtype=%d", device->type);
switch(device->type){
case DEV_TTY:
DBG("Serial");
device->dev = opentty();
if(!device->dev){
WARN("Can't open device %s", device->name);
DBG("CANT OPEN");
return FALSE;
}
break;
case DEV_NETSOCKET:
case DEV_UNIXSOCKET:
DBG("Socket");
device->dev = opensocket();
if(!device->dev){
WARNX("Can't open socket");
DBG("CANT OPEN");
return FALSE;
}
break;
default:
return FALSE;
}
if(path){ // open logging file
dupfile = fopen(path, "a");
if(!dupfile){
WARN("Can't open %s", path);
closedev();
return FALSE;
}
}
changeeol(device->eol); // allow string functions to know EOL
memcpy(d, device, sizeof(chardevice));
return TRUE;
}
void closedev(){
if(!device) return;
pthread_mutex_unlock(&device->mutex);
pthread_mutex_trylock(&device->mutex);
if(dupfile){
fclose(dupfile);
dupfile = NULL;
}
switch(device->type){
case DEV_TTY:
if(device->dev){
sl_tty_t *t = device->dev;
ioctl(t->comfd, TCSETS2, &t->oldtty); // return TTY to previous state
close(t->comfd);
}
break;
case DEV_NETSOCKET:
if(device->dev){
close(device->dev->comfd);
FREE(device->dev);
}
break;
default:
return;
}
if(device->dev){
FREE(device->dev->format);
FREE(device->dev->portname);
FREE(device->dev->buf);
FREE(device->dev);
}
FREE(device->name);
FREE(device);
DBG("Device closed");
}