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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/*
* xtel - Emulateur MINITEL sous X11
*
* Copyright (C) 1991-1998 Lectra Systemes & Pierre Ficheux
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
static char rcsid[] = "$Id: modem.c,v 1.2 2001/02/13 09:39:23 pierre Exp $";
/* Init du modem */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef NO_TERMIO
#include <sgtty.h>
#else
#ifdef USE_TERMIOS
#include <sys/ioctl.h>
#include <termios.h>
#else
#include <termio.h>
#endif /* USE_TERMIOS */
#endif
#if (defined(lectra) && defined(SVR4) && !defined(sun)) || defined(hpux)
#include <sys/termiox.h>
#endif
#ifdef USE_SYSLOG
#include <syslog.h>
#endif /* USE_SYSLOG */
#include "demon.h"
#include "globald.h"
#ifdef NO_TERMIO
static struct sgttyb term_sauve, term;
#else
#ifdef USE_TERMIOS
static struct termios term_sauve, term;
#else
static struct termio term_sauve, term;
#endif /* USE_TERMIOS */
#endif /* NO_TERMIO */
#if (defined(lectra) && defined(SVR4) && !defined(sun)) || defined(hpux)
static struct termiox termx;
#endif /* lectra && SVR4 && !sun || hpux */
#ifndef USE_SYSLOG
static FILE *fp_console;
#endif
static char prefix[256];
static struct timeval delai_maxi;
void init_debug (char *s)
{
strcpy (prefix, s);
#ifdef USE_SYSLOG
openlog(s, LOG_PID | LOG_CONS, LOG_INFO);
#else
if ((fp_console = fopen ("/dev/console", "w")) == NULL) {
perror ("/dev/console");
exit (1);
}
#endif /* USE_SYSLOG */
}
void close_debug ()
{
#ifndef USE_SYSLOG
fclose (fp_console);
#endif
}
/* Syslog or not syslog ? */
#ifdef USE_SYSLOG
void log_debug (fmt, p1, p2, p3, p4, p5, p6, p7, p8)
char *fmt;
int p1, p2, p3, p4, p5, p6, p7, p8;
{
char msg[256];
sprintf (msg, fmt, p1, p2, p3, p4, p5, p6, p7, p8);
syslog (LOG_INFO, "%s", msg);
}
void log_err (s)
char *s;
{
syslog (LOG_ERR, "%s", s);
}
#else
void log_debug (fmt, p1, p2, p3, p4, p5, p6, p7, p8)
char *fmt;
int p1, p2, p3, p4, p5, p6, p7, p8;
{
fprintf (fp_console, "%s[%d] ", prefix, getpid());
fprintf (fp_console, fmt, p1, p2, p3, p4, p5, p6, p7, p8);
fprintf (fp_console, "\n\r");
}
void log_err (s)
char *s;
{
fprintf (fp_console, "%s[%d] ", prefix, getpid());
while (*s) {
if (*s != '%')
fputc (*s, fp_console);
else if (*(s+1) == 'm') {
fprintf (fp_console, "%s", strerror(errno));
s++;
}
s++;
}
fprintf (fp_console, "\n\r");
}
#endif /* USE_SYSLOG */
/* Envoi d'une commande MODEM */
static void writemodem (fd, buf, n)
int fd;
char *buf;
int n;
{
register int i;
for (i = 0 ; i != n ; i++) {
write (fd, buf+i, 1);
usleep (30000);
}
}
/* Init des parametres de ligne */
void init_tty (int fd, int speed, int csize, int parity, int flags, int dialer, int tempo)
{
#ifdef NO_TERMIO
ioctl (fd, TIOCGETP, &term);
usleep(tempo);
memcpy ((char *)&term_sauve, (char *)&term, sizeof(struct sgttyb));
term.sg_flags |= RAW;
ioctl (fd, TIOCSETP, &term);
usleep(tempo);
/* Flags, pour l'instant RTS/CTS */
/* FIXME: comment passer la ligne en RTS/CTS sans termio ? */
#else
#ifdef USE_TERMIOS
ioctl (fd, TIOCGETA, &term);
usleep(tempo);
memcpy ((char *)&term_sauve, (char *)&term, sizeof(struct termios));
#else
ioctl (fd, TCGETA, &term);
usleep(tempo);
memcpy ((char *)&term_sauve, (char *)&term, sizeof(struct termio));
#endif /* USE_TERMIOS */
/* Parametrage de la ligne */
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
term.c_iflag &= ~(IXON|IXOFF|ICRNL);
term.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
#ifdef USE_TERMIOS
/*
* FreeBSD 1.1 (Beta) n'a pas l'air d'apprecier qu'on mette clocal
* a 0... (blocage au premier write sur /dev/cua01)
*/
term.c_cflag &= ~(CSIZE|CSTOPB);
term.c_cflag |= (CREAD|HUPCL);
cfsetispeed (&term, speed);
cfsetospeed (&term, speed);
#else
term.c_cflag &= ~(CSIZE|CBAUD|CLOCAL);
/* Vitesse */
term.c_cflag |= speed;
#endif /* USE_TERMIOS */
/* Taille caractere */
term.c_cflag |= csize;
/* Parite */
if (parity == SANS)
term.c_cflag &= ~PARENB;
else {
term.c_cflag |= PARENB;
if (parity == PAIR)
term.c_cflag &= ~PARODD;
}
/* Flags, pour l'instant RTS/CTS */
if (flags & FLAG_RTS_CTS) {
#ifndef __USLC__
#if !(defined(hpux) || defined(sgi) || (defined(lectra) && defined(SVR4) && !defined(sun)))
term.c_cflag |= CRTSCTS;
#else
ioctl (fd, TCGETX, &termx);
termx.x_hflag |= (RTSXOFF|CTSXON);
ioctl(fd, TCSETXW, &termx);
#endif /* HP/UX | SGI | LS/UX */
#endif /* !__USLC__ */
}
#ifdef sun
if (csize != CS8)
term.c_iflag = ISTRIP;
#endif /* sun */
#ifdef USE_TERMIOS
term.c_iflag &= ~(IGNCR|ICRNL|INLCR|IMAXBEL);
term.c_iflag |= (ISTRIP|INPCK);
term.c_lflag &= ~(ECHOCTL|IEXTEN);
term.c_oflag &= ~OPOST;
#endif /* USE_TERMIOS */
/* Cas du Minitel 1/2 */
if (dialer != DIALER_MODEM)
term.c_cflag |= (CREAD|CLOCAL);
/* Affectation des parametres */
#ifdef USE_TERMIOS
ioctl (fd, TIOCSETA, &term);
usleep(tempo);
#else
ioctl (fd, TCSETA, &term);
usleep(tempo);
#endif /* USE_TERMIOS */
#endif /* NO_TERMIO */
}
/* Restauration des parametres */
void restore_tty (int fd)
{
#ifdef ultrix
int temp = 0;
#endif
int tempo = 0;
/* remet la ligne en l'etat */
#ifdef NO_TERMIO
term.sg_ispeed = term.sg_ospeed = B0;
ioctl (fd, TIOCSETP, &term);
usleep(tempo);
ioctl (fd, TIOCSETP, &term_sauve);
usleep(tempo);
#else
#ifdef USE_TERMIOS
cfsetispeed (&term, B0);
cfsetospeed (&term, B0);
ioctl (fd, TIOCSETAW, &term);
usleep(tempo);
ioctl (fd, TIOCSETA, &term_sauve);
usleep(tempo);
#else
term.c_cflag &= ~CBAUD;
term.c_cflag |= B0;
ioctl (fd, TCSETAW, &term);
usleep(tempo);
ioctl (fd, TCSETA, &term_sauve);
usleep(tempo);
#endif /* USE_TERMIOS */
#endif /* NO_TERMIO */
#ifdef ultrix
ioctl (fd, TIOCNMODEM, &temp);
ioctl (fd, TIOCSINUSE, NULL);
ioctl (fd, TIOCHPCL, 0);
#endif /* ultrix */
}
/*
* Dialogue avec le Modem (chatons, chatons...)
*/
int do_chat (fd, chat_script, tmax, tempo, telno, reply_buf, reply_size)
int fd;
char *chat_script;
unsigned long tmax;
int tempo;
char *telno, *reply_buf;
int reply_size;
{
fd_set a_lire, t_a_lire;
int i, erreur, fin, nbread, cmodem = 0;
char *pt_chat, c, *q;
erreur = 0;
pt_chat = chat_script;
fin = 0;
FD_ZERO (&a_lire);
FD_SET (fd, &a_lire);
q = reply_buf;
#ifdef DEBUG_XTELD
log_debug ("do_chat: chat= \"%s\" tmax = %ld", chat_script, tmax);
#endif
#ifndef NO_TERMIO
/*
* Passe en controle local si necessaire. Cela permet d'eviter que le
* script se plante lorsque certains modem font bouger le DCD sur un AT&F
* (comme le Hayes Optima par exemple)...
*/
#ifdef USE_TERMIOS
ioctl (fd, TIOCGETA, &term);
usleep(tempo);
#else
ioctl (fd, TCGETA, &term);
usleep(tempo);
#endif /* USE_TERMIOS */
if ((term.c_cflag | CLOCAL) == 0) {
term.c_cflag |= CLOCAL;
#ifdef USE_TERMIOS
ioctl (fd, TIOCSETA, &term);
usleep(tempo);
#else
ioctl (fd, TCSETA, &term);
usleep(tempo);
#endif /* USE_TERMIOS */
cmodem = 1;
}
#endif /* NO_TERMIO */
while (!fin) {
/*
* On commence par emettre la commande au Modem... (les chaines sont
* separees par des blancs). On attend ensuite la reponse.
*/
#ifdef DEBUG_XTELD
{
char ci, *p;
int fi;
p = strdup (pt_chat);
for (i = 0 ; *(p+i) != ' ' && *(p+i) != 0 ; i++)
;
ci = p[i];
p[i] = 0;
log_debug ("J'envoie: \"%s\"", p);
p[i] = ci;
if (ci) {
i++; fi = i;
for ( ; *(p+i) != ' ' && *(p+i) != 0 ; i++);
ci = p[i];
p[i] = 0;
log_debug ("J'attends: \"%s\"", p + fi);
p[i] = ci;
}
free (p);
}
#endif
while (*pt_chat && *pt_chat != ' ') {
/*
* On reconnait les sequences '\' suivantes :
*
* \T numero de telephone
* \d tempo de 1 s
* \n LF
* \r CR
* \t VT
* \a BELL
* \abc valeur DECIMALE entre 0 et 255 sur 3 caracteres
* (ex: \010 pour envoyer 10)
* \xab valeur hexadecimale
*/
if (*pt_chat != '\\') {
#ifdef DEBUG_CHAT
log_debug ("envoie: '%c'", *pt_chat);
#endif
writemodem (fd, pt_chat, 1);
} else {
pt_chat++;
switch (*pt_chat) {
case 'd' :
sleep (1);
break;
case 'n' :
#ifdef DEBUG_CHAT
log_debug ("envoie: '\\n'");
#endif
writemodem (fd, "\n", 1);
break;
case 'r' :
#ifdef DEBUG_CHAT
log_debug ("envoie: '\\r'");
#endif
writemodem (fd, "\r", 1);
break;
case 't' :
#ifdef DEBUG_CHAT
log_debug ("envoie: '\\t'");
#endif
writemodem (fd, "\t", 1);
break;
case 'a' :
#ifdef DEBUG_CHAT
log_debug ("envoie: '\\a'");
#endif
writemodem (fd, "\a", 1);
break;
case 'T' :
if (telno) {
#ifdef DEBUG_CHAT
log_debug ("envoie: \"%s\"", telno);
#endif
writemodem (fd, telno, strlen(telno));
}
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
char nb = 2;
char n = *pt_chat++ - '0';
while (nb-- && isdigit(*pt_chat)) {
n *= 10;
n += *pt_chat++ - '0';
}
pt_chat--;
#ifdef DEBUG_CHAT
log_debug ("envoie: '\\0x%x'", n);
#endif
writemodem (fd, &n, 1);
break;
}
case 'x': {
char n = 0, nb = 2;
pt_chat++;
while (nb-- && isxdigit(*pt_chat)) {
n *= 16;
if (isdigit(*pt_chat)) {
n += *pt_chat - '0';
} else {
if (islower(*pt_chat)) {
n += *pt_chat + (10 - 'a');
} else {
n += *pt_chat + (10 - 'A');
}
}
pt_chat++;
}
pt_chat--;
#ifdef DEBUG_CHAT
log_debug ("envoie: '\\0x%x'", n);
#endif
writemodem (fd, &n, 1);
break;
}
default :
#ifdef DEBUG_CHAT
log_debug ("envoie: '%c'", *pt_chat);
#endif
writemodem(fd, pt_chat, 1);
break;
}
}
pt_chat++;
}
if (*pt_chat == 0)
break;
/*
* On attend la reponse.
*/
/* pointe la chaine a recevoir */
pt_chat++;
i = 0;
for (;;) {
t_a_lire = a_lire;
delai_maxi.tv_sec = tmax;
nbread = select (32, &t_a_lire, NULL, NULL, &delai_maxi);
/* Si il y a qque chose a lire */
if (nbread > 0 && FD_ISSET (fd, &t_a_lire)) {
if (read (fd, &c, 1) == 1) {
char voulu;
if (q && ((q - reply_buf) < reply_size-1))
*q++ = c;
#ifdef DEBUG_CHAT
log_debug ("recu: '%c'", c);
#endif
if (pt_chat[i] != '\\') {
voulu = pt_chat[i];
} else {
i++;
switch(pt_chat[i]) {
case 'n':
voulu = '\n';
break;
case 'r':
voulu = '\r';
break;
case 't':
voulu = '\t';
break;
case 'a':
voulu = '\a';
break;
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9': {
char nb = 2;
voulu = pt_chat[i++] - '0';
while (nb-- && isdigit(pt_chat[i])) {
voulu *= 10;
voulu += pt_chat[i++] - '0';
}
i--;
break;
}
case 'x': {
char nb = 2;
voulu = 0;
i++;
while (nb-- && isxdigit(pt_chat[i])) {
voulu *= 16;
if (isdigit(pt_chat[i])) {
voulu += pt_chat[i] - '0';
} else {
if (islower(pt_chat[i])) {
voulu += pt_chat[i] + (10 - 'a');
} else {
voulu += pt_chat[i] + (10 - 'A');
}
}
i++;
}
i--;
break;
}
default:
voulu = pt_chat[i];
break;
}
}
if (voulu == c) {
i++;
#ifdef DEBUG_CHAT
log_debug ("voulu '%c':Ok", voulu);
#endif
if (*(pt_chat+i) == ' ' || *(pt_chat+i) == 0) {
#ifdef DEBUG_XTELD
log_debug (" -Ok");
#endif
break;
}
}
else {
#ifdef DEBUG_CHAT
log_debug ("voulu '%c':erreur", voulu);
#endif
i = 0;
}
}
/* erreur read */
else {
#ifdef DEBUG_XTELD
log_debug ("Erreur read ! (errno=%d)",errno);
#endif
/*erreur = 1;*/
if (++erreur > 10)
break;
}
}
/* timeout */
else if (nbread == 0) {
#ifdef DEBUG_XTELD
log_debug ("Erreur timeout !");
#endif
erreur = 1;
break;
}
}
/* Commande suivante */
pt_chat += (i+1);
fin = ((erreur != 0 || *(pt_chat-1) == 0) ? 1 : 0);
usleep (30000);
}
#ifndef NO_TERMIO
/* Repasse en controle modem */
if (cmodem) {
term.c_cflag &= ~CLOCAL;
#ifdef USE_TERMIOS
ioctl (fd, TIOCSETA, &term);
usleep(tempo);
#else
ioctl (fd, TCSETA, &term);
usleep(tempo);
#endif /* USE_TERMIOS */
}
#endif
if (q)
*q = 0;
return erreur;
}
|