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
|
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dlfcn.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <utime.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "../inc/control.h"
#include "../inc/misc.h"
#include "../inc/telnet.h"
#define SHARED_OBJECT
#include "../inc/port_speed.h"
#include <signal.h>
#include <syslog.h>
#define MAX_PORTS 32
static void *libc = NULL;
static int (*real_tcsetattr)(int fd, int optional_actions, const struct termios *termios_p) = NULL;
static int (*real_tcsendbreak)(int fd, int duration);
static char *cyclades_devices[MAX_PORTS];
static int num_devices;
static int socket_fd = -1;
static int socket_ind = -1;
void libcsc_init()
{
FILE *fp;
libc = dlopen(LIBC, RTLD_LAZY | RTLD_GLOBAL);
if(!libc)
{
printf("Can't map " LIBC "\n");
exit(1);
}
real_tcsetattr = (int (*)(int, int, const struct termios *))dlsym(libc, "tcsetattr");
real_tcsendbreak = (int (*)(int, int))dlsym(libc, "tcsendbreak");
num_devices = 0;
fp = fopen("/etc/cyclades-devices", "r");
if(fp)
{
char str[1024];
while(num_devices < MAX_PORTS && fgets(str, sizeof(str), fp))
{
if(str[0] == '/')
{
strtok(str, ":\r\n");
cyclades_devices[num_devices] = strdup(str);
num_devices++;
}
}
fclose(fp);
}
}
void libcsc_fini()
{
dlclose(libc);
libc = NULL;
}
static void close_socket()
{
close(socket_fd);
socket_fd = -1;
socket_ind = -1;
}
static int open_socket(int ind)
{
struct sockaddr_un addr;
if(socket_ind == ind)
return 0;
if(socket_fd >= 0)
close(socket_fd);
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd == -1)
return -1;
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s.control", cyclades_devices[ind]);
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
socket_ind = ind;
if(connect(socket_fd, (const struct sockaddr *)&addr, sizeof(addr)))
{
close(socket_fd);
socket_fd = -1;
socket_ind = -1;
return -1;
}
return 0;
}
static int get_device_ind(int fd)
{
struct stat device_stat, fd_stat;
int i;
if(fstat(fd, &fd_stat))
return -1;
for(i = 0; i < num_devices; i++)
{
if(!stat(cyclades_devices[i], &device_stat))
{
if(device_stat.st_dev == fd_stat.st_dev
&& device_stat.st_ino == fd_stat.st_ino)
return i;
}
}
return -1;
}
static int send_data(int port_ind, e_operation oper, int val, int extra_timeout)
{
fd_set readfds, exceptfds;
struct timeval timeout;
s_control s;
struct sigaction act, oldact;
int rc;
int no_ign_pipe = 0;
if(open_socket(port_ind) == -1)
return -1;
act.sa_handler = SIG_IGN;
if(sigaction(SIGPIPE, &act, &oldact))
{
syslog(LOG_WARNING, "libcyclades-ser-cli: Can't ignore SIGPIPE.");
no_ign_pipe = 1;
}
s.oper = oper;
s.val = val;
s.size = sizeof(s_control);
if(send(socket_fd, &s, sizeof(s_control), 0) != sizeof(s_control))
{
if(!no_ign_pipe)
{
if(sigaction(SIGPIPE, &oldact, NULL))
syslog(LOG_ERR, "libcyclades-ser-cli: Can't reset SIGPIPE handler.");
}
close_socket();
return -1;
}
FD_ZERO(&readfds);
FD_SET(socket_fd, &readfds);
FD_ZERO(&exceptfds);
FD_SET(socket_fd, &exceptfds);
timeout.tv_sec = extra_timeout + 2;
timeout.tv_usec = 0;
if(select(socket_fd + 1, &readfds, NULL, &exceptfds, &timeout) != 1
|| FD_ISSET(socket_fd, &exceptfds) )
{
if(!no_ign_pipe)
{
if(sigaction(SIGPIPE, &oldact, NULL))
syslog(LOG_ERR, "libcyclades-ser-cli: Can't reset SIGPIPE handler.");
}
close_socket();
return -1;
}
rc = recv(socket_fd, &s, sizeof(s), MSG_WAITALL);
if(!no_ign_pipe)
{
if(sigaction(SIGPIPE, &oldact, NULL))
syslog(LOG_ERR, "libcyclades-ser-cli: Can't reset SIGPIPE handler.");
}
if(rc != sizeof(s) || s.val != val || s.oper != oper || s.size != sizeof(s) )
{
close_socket();
return -1;
}
return 0;
}
static void do_xon_xoff(int ind, int *a_success, int *a_fail
, struct termios *term, const struct termios *termios_p)
{
/* first do XON/XOFF for outbound/both */
if( (term->c_iflag & IXON) != (termios_p->c_iflag & IXON) )
{
int xon = COM_OFLOW_NONE;
if(termios_p->c_iflag & IXON)
xon = COM_OFLOW_SOFT;
if(!send_data(ind, eSET_CONTROL, xon, 0) )
{
/* if we set outbound XON/XOFF we set inbound as well and we need to
* turn it off next */
term->c_iflag &= !(IXON | IXOFF);
#ifdef CRTSCTS
term->c_cflag &= !CRTSCTS;
#endif
if(termios_p->c_iflag & IXON)
term->c_iflag |= (IXON | IXOFF);
*a_success++;
}
else
*a_fail++;
}
if( (term->c_iflag & IXOFF) != (termios_p->c_iflag & IXOFF) )
{
int xon = COM_IFLOW_NONE;
if(termios_p->c_iflag & IXOFF)
xon = COM_IFLOW_SOFT;
if(!send_data(ind, eSET_CONTROL, xon, 0) )
{
term->c_iflag &= !IXOFF;
#ifdef CRTSCTS
term->c_cflag &= !CRTSCTS;
#endif
term->c_iflag |= termios_p->c_iflag & IXON;
*a_success++;
}
else
*a_fail++;
}
}
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
{
int ind = get_device_ind(fd);
struct termios term;
int a_fail = 0, a_success = 0;
speed_t i_sp, o_sp, term_sp;
if(ind == -1)
return real_tcsetattr(fd, optional_actions, termios_p);
if(tcgetattr(fd, &term))
return -1;
if(!memcmp(&term, termios_p, sizeof(struct termios)))
return 0;
if( (term.c_cflag & HUPCL) != (termios_p->c_cflag & HUPCL) )
{
term.c_cflag &= !HUPCL;
term.c_cflag |= termios_p->c_cflag & HUPCL;
a_success++;
}
/* Some really ugly code because termios supports split baud rates while
* RFC 2217 does not. */
i_sp = cfgetispeed(termios_p);
o_sp = cfgetospeed(termios_p);
term_sp = cfgetispeed(&term);
if(i_sp != term_sp || o_sp != term_sp )
{
if(i_sp != o_sp)
{
if(i_sp != term_sp)
term_sp = i_sp;
else
term_sp = o_sp;
}
else
{
term_sp = i_sp;
}
if(!send_data(ind, eSET_SPEED, baud_index_to_int(term_sp), 0) )
{
cfsetispeed(&term, term_sp);
cfsetospeed(&term, term_sp);
a_success++;
}
else
a_fail++;
}
if( (term.c_cflag & CSIZE) != (termios_p->c_cflag & CSIZE) )
{
int csize;
switch(termios_p->c_cflag & CSIZE)
{
case CS5:
csize = 5;
break;
case CS6:
csize = 6;
break;
case CS7:
csize = 7;
break;
case CS8:
csize = 8;
break;
default:
csize = 0;
}
if(csize == 0)
{
a_fail++;
}
else
{
if(!send_data(ind, eSET_CSIZE, csize, 0) )
{
a_success++;
term.c_cflag = (term.c_cflag & (!CSIZE)) | csize;
}
else
a_fail++;
}
}
if( (term.c_iflag & INPCK) != (termios_p->c_iflag & INPCK)
|| (term.c_cflag & (PARENB|PARODD)) != (termios_p->c_cflag & (PARENB|PARODD)) )
{
int parity;
if( termios_p->c_cflag & PARENB )
{
if(termios_p->c_cflag & PARODD)
parity = 2; /* parity Odd */
else
parity = 3; /* parity Even */
}
else
parity = 1; /* parity None */
if(!send_data(ind, eSET_PARITY, parity, 0) )
{
term.c_cflag &= !(PARENB|PARODD);
term.c_cflag |= termios_p->c_cflag & (PARENB|PARODD);
a_success++;
}
else
a_fail++;
}
if( (term.c_cflag & CSTOPB) != (termios_p->c_cflag & CSTOPB) )
{
int stop = 1;
if(termios_p->c_cflag & CSTOPB)
stop = 2;
if(!send_data(ind, eSET_STOPSIZE, stop, 0) )
{
term.c_cflag &= !CSTOPB;
term.c_cflag |= termios_p->c_cflag & CSTOPB;
a_success++;
}
else
a_fail++;
}
do_xon_xoff(ind, &a_success, &a_fail, &term, termios_p);
#ifdef CRTSCTS
/* CRTSCTS is not in POSIX */
if( (term.c_cflag & CRTSCTS) != (termios_p->c_cflag & CRTSCTS) )
{
int rtscts = COM_OFLOW_NONE;
if(termios_p->c_cflag & CRTSCTS)
rtscts = COM_OFLOW_HARD;
if(!send_data(ind, eSET_CONTROL, rtscts, 0) )
{
term.c_cflag &= !CRTSCTS;
term.c_cflag |= termios_p->c_cflag & CRTSCTS;
a_success++;
if(rtscts == COM_OFLOW_NONE)
{
/* if we turn off RTSCTS then it also turns off xon/xof and we need
* to turn it back on. RFC2217 sucks in this regard */
term.c_iflag &= !(IXON | IXOFF);
do_xon_xoff(ind, &a_success, &a_fail, &term, termios_p);
}
}
else
a_fail++;
}
#endif
if(memcmp(&term.c_cc, &termios_p->c_cc, sizeof(term.c_cc)))
{
memcpy(&term.c_cc, &termios_p->c_cc, sizeof(term.c_cc));
a_success++;
}
if(a_success)
real_tcsetattr(fd, optional_actions, &term);
if(a_success || !a_fail)
return 0;
return -1;
}
int tcsendbreak(int fd, int duration)
{
int ind = get_device_ind(fd);
if(ind == -1)
return real_tcsendbreak(fd, duration);
return send_data(ind, eSEND_BREAK, duration, duration % 4 + 1);
}
|