-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnections.c
418 lines (336 loc) · 10.2 KB
/
connections.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "modembank.h"
const speed_t baud_list[ BAUDLIST_SIZE] = { B300, B1200, B4800, B9600, B19200, B38400, B57600, B115200 };
const int baud_alias[BAUDLIST_SIZE] = { 300, 1200, 4800, 9600, 19200, 38400, 57600, 115200 };
int loadModemConfig( conn ** headconn )
{
char modbuf[256];
char * path, * magic;
int baud, mod_count = 0;
FILE * fd = fopen( "config/modems.csv", "r" );
if ( fd == NULL )
{
zlog( "Cannot open modem config file: \"config/modem.csv\"\n" );
return 0;
}
zlog( "Initializing modems...\n" );
while ( fgets( modbuf, 256, fd ) != NULL )
{
// Get the tty file to open
path = strtok( modbuf, "," );
// Get the baud rate index
magic = strtok( NULL, "," );
// No baud rate specified, skip this one
if ( magic == NULL )
{
zlog( "Bad line, no baud rate specified for %s\n", path );
continue;
}
// Convert to integer
baud = atoi( magic );
// Get init string
magic = strtok( NULL, "\n" );
mod_count += configureModem( headconn, path, baud, magic );
}
// Remember to close the config file
fclose( fd );
zlog( "Initialized %d modems\n", mod_count );
return mod_count;
}
int configureModem( conn ** headconn, const char * path, int baud, const char * magic )
{
struct termios modopt;
char atbuf[256];
int i, mfd, atsize;
// Open the serial port
mfd = open( path, O_RDWR );
// Couldn't open port
if ( mfd < 0 )
{
zlog( "Failed to open modem %s\n", path );
return 0;
}
// This isn't a serial port
if ( !isatty(mfd) )
{
zlog( "Failed, %s is not a TTY\n", path );
close( mfd );
return 0;
}
// DO NOT CHANGE THE VALUE OF "I" BEFORE SETTING BAUD RATE
for ( i = 0; i < BAUDLIST_SIZE; i++ )
{
if ( baud == baud_alias[i] ) break;
}
if ( i == BAUDLIST_SIZE )
{
zlog( "Failed, invalid baud rate %d for modem %s\n", baud, path );
close( mfd );
return 0;
}
// Get current config
tcgetattr( mfd, &modopt );
// Un-set iFlag params
modopt.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
// Un-set oFlag params
modopt.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OLCUC | OPOST);
// Un-set cFlag params
modopt.c_cflag &= ~(CSIZE | PARENB | CLOCAL);
// Set cFlag params
modopt.c_cflag |= (CS8 | CRTSCTS | CREAD);
// Un-set lFlag params
modopt.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
// Set 1 second timeout
modopt.c_cc[VTIME] = INIT_DURATION * 10;
modopt.c_cc[VMIN] = 0;
// Configure baud rate
cfsetispeed( &modopt, baud_list[i] );
cfsetospeed( &modopt, baud_list[i] );
// Flash settings to the tty after flushing
tcsetattr( mfd, TCSAFLUSH, &modopt );
zlog( "Attempting to contact modem %s\n", path );
// Attempt to intialize the modem
for ( i = 0; i < INIT_ATTEMPTS; i++ )
{
printf( "Sending 'AT' to modem (attempt %d)... ", i );
fflush(stdout);
// Send an AT command for baud rate detection
write( mfd, "AT\r", 3 );
// Wait for read
atsize = read( mfd, atbuf, 255 );
// Check if we got any data
if ( atsize > 0 )
{
// Recived an OK command
if ( strncmp( atbuf, "\r\nOK\r\n", 6 ) == 0 )
{
printf( "responded 'OK'\n" );
break;
}
else
{
// Make the string printable
atbuf[atsize] = '\0';
printf( "unknown response: >>>%s<<<\n", atbuf );
for ( int k = 0; k < atsize; k++ ) printf( "%02x|", atbuf[k] & 0xFF );
printf( "\n" );
}
}
else
{
printf( "no response\n" );
}
}
// Could not reach modem
if ( i == INIT_ATTEMPTS )
{
close( mfd );
zlog( "Modem %s non-responsive to AT commands\n", path );
return 0;
}
// Set blocking
modopt.c_cc[VTIME] = 0;
modopt.c_cc[VMIN] = 1;
// Re-flash settings
tcsetattr( mfd, TCSANOW, &modopt );
// Create a new conn
conn * newconn = malloc( sizeof(conn) );
// Record time
time( &(newconn->first) );
time( &(newconn->last ) );
// Save modem information
newconn->flags = FLAG_MODM;
newconn->fd = mfd;
newconn->buflen = 0;
// Store the path to this modem
strcpy( newconn->org.path, path );
// Store the name of this modem
strcpy( newconn->name, "Temp Modem" );
// Insert node
newconn->next = *headconn;
*headconn = newconn;
// No baud rate specified, skip this one
if ( magic != NULL && !getDCD( newconn ) )
{
// Transmit string
write( mfd, magic, strlen(magic) );
// Terminate string
write( mfd, "\r", 1 );
}
// Make sure we're ready to accept calls
setDTR( newconn, 1 );
ylog( newconn, "Intialized at %d baud\n", baud );
return 1;
}
int telnetOptions( user * muser )
{
// Get our input buffer
conn * mconn = muser->stdin;
if ( mconn->buflen <= 0 ) return 0;
if ( mconn->buf[0] != '\xFF' ) return 0; // All options start with 0xFF
int i;
char * ctok = mconn->buf;
/*
printf( "Telnet options string:\n" );
for ( i = 0; i < mconn->buflen; i++ )
{
printf( "%02x", mconn->buf[i] & 0xFF );
if ( i < mconn->buflen - 1 ) printf("|");
}
printf( "\n" );
*/
while ( ctok < mconn->buf + mconn->buflen )
{
if ( ctok[1] == '\xFA' ) // Sub negotiate params
{
switch ( ctok[2] ) // What type of option?
{
case '\x1F': // Negotiate About Window Size (NAWS)
muser->width = (ctok[3] << 8) + ctok[4];
muser->height = (ctok[5] << 8) + ctok[6];
xlog( muser, "Set terminal size to: %d by %d\n", muser->width, muser->height );
break;
case '\x18': // Terminal type
for ( i = 0; i < TERMTYPE_LEN && i + 4 + ctok < mconn->buf + mconn->buflen; i++ )
{
// End of negotiation
if ( ctok[i + 4] == '\xFF' && ctok[i + 5] == '\xF0' ) break;
// Copy char
muser->termtype[i] = ctok[i + 4];
}
muser->termtype[i] = '\0';
xlog( muser, "Set terminal type to: '%s'\n", muser->termtype );
break;
}
}
else if ( ctok[1] == '\xFB' ) // WILL
{
switch ( ctok[2] ) // What option?
{
case '\x18': // Terminal type
// Request terminal name
write( mconn->fd, "\xFF\xFA\x18\x01\xFF\xF0", 6 );
break;
}
}
for ( ctok++; ctok < mconn->buf + mconn->buflen && ctok[0] != '\xFF'; ctok++ );
}
// Empty the buffer
mconn->buflen = 0;
return 1;
}
int getDCD( conn * mconn )
{
// Not a modem
if ( !(mconn->flags & FLAG_MODM) ) return -1;
int status;
// Get status pins
ioctl( mconn->fd, TIOCMGET, &status );
// Return status of Data Carrier Detect
return status & TIOCM_CAR;
}
int setDTR( conn * mconn, int set )
{
// Not a modem
if ( !(mconn->flags & FLAG_MODM) ) return -1;
int status;
// Get status pins
ioctl( mconn->fd, TIOCMGET, &status );
int result = status & TIOCM_DTR;
// Flip the bit
if ( set )
{
status |= TIOCM_DTR;
}
else
{
status &= ~TIOCM_DTR;
}
// Set status pins
ioctl( mconn->fd, TIOCMSET, &status );
// Return true if changed
return result ^ (status & TIOCM_DTR);
}
int setBlocking( conn * mconn, int set )
{
// Read in socket settings
int status;
if ( (status = fcntl( mconn->fd, F_GETFL, NULL )) < 0 ) return -1;
int result = result & O_NONBLOCK;
// Toggle non-blocking mode
if ( !set )
{
status |= O_NONBLOCK;
}
else
{
status &= ~O_NONBLOCK;
}
// Update socket settings
if ( fcntl( mconn->fd, F_SETFL, status ) < 0 ) return -1;
return result ^ (status & O_NONBLOCK);
}
int getBlocking( conn * mconn )
{
// Read in socket settings
int status;
if ( (status = fcntl( mconn->fd, F_GETFL, NULL )) < 0 ) return -1;
return status & O_NONBLOCK;
}
int connGarbage( conn ** headconn )
{
conn * icn, * nextconn, * prevconn = NULL;
int conn_count = 0;
for ( icn = *headconn; icn != NULL; icn = nextconn )
{
// Save this for later
nextconn = icn->next;
// Nothing to do here
if ( !(icn->flags & FLAG_GARB) )
{
/*** Intentionally left empty ***/
}
// Handle modems
else if ( icn->flags & FLAG_MODM )
{
// Do we need to hangup?
if ( getDCD( icn ) )
{
// Drop the Data Terminal Ready line to hangup
setDTR( icn, 0 );
}
else
{
ylog( icn, "Reset an %s modem\n", icn->flags & FLAG_OUTG ? "outgoing" : "incoming");
// Tell the modem we're ready now
setDTR( icn, 1 );
// Finished reseting modem, mark it as not garbage, and available
icn->flags &= ~(FLAG_GARB | FLAG_CALL);
}
}
else
{
// Shutdown socket
shutdown( icn->fd, SHUT_RDWR );
// Close socket
close( icn->fd );
ylog( icn, "Terminated an %s connection\n", icn->flags & FLAG_OUTG ? "outgoing" : "incoming" );
// Check if we're the head
if ( prevconn == NULL )
{
*headconn = icn->next;
}
else
{
prevconn->next = icn->next;
}
// Free the node
free( icn );
// Decrement count
conn_count++;
// Don't update prevconn
continue;
}
prevconn = icn;
}
return conn_count;
}