datalink.c revision 45350
1/*- 2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: datalink.c,v 1.35 1999/03/04 17:42:15 brian Exp $ 27 */ 28 29#include <sys/param.h> 30#include <netinet/in.h> 31#include <netinet/in_systm.h> 32#include <netinet/ip.h> 33#include <sys/un.h> 34 35#include <ctype.h> 36#include <stdio.h> 37#include <stdlib.h> 38#include <string.h> 39#include <sys/uio.h> 40#include <termios.h> 41 42#include "mbuf.h" 43#include "log.h" 44#include "defs.h" 45#include "timer.h" 46#include "fsm.h" 47#include "lcp.h" 48#include "descriptor.h" 49#include "lqr.h" 50#include "hdlc.h" 51#include "async.h" 52#include "throughput.h" 53#include "ccp.h" 54#include "link.h" 55#include "physical.h" 56#include "iplist.h" 57#include "slcompress.h" 58#include "ipcp.h" 59#include "filter.h" 60#include "mp.h" 61#ifndef NORADIUS 62#include "radius.h" 63#endif 64#include "bundle.h" 65#include "chat.h" 66#include "auth.h" 67#include "modem.h" 68#include "prompt.h" 69#include "lcpproto.h" 70#include "pap.h" 71#include "chap.h" 72#include "command.h" 73#include "cbcp.h" 74#include "datalink.h" 75 76static void datalink_LoginDone(struct datalink *); 77static void datalink_NewState(struct datalink *, int); 78 79static void 80datalink_OpenTimeout(void *v) 81{ 82 struct datalink *dl = (struct datalink *)v; 83 84 timer_Stop(&dl->dial.timer); 85 if (dl->state == DATALINK_OPENING) 86 log_Printf(LogPHASE, "%s: Redial timer expired.\n", dl->name); 87} 88 89static int 90datalink_StartDialTimer(struct datalink *dl, int Timeout) 91{ 92 int result = Timeout; 93 94 timer_Stop(&dl->dial.timer); 95 if (Timeout) { 96 if (Timeout > 0) 97 dl->dial.timer.load = Timeout * SECTICKS; 98 else { 99 result = (random() % DIAL_TIMEOUT) + 1; 100 dl->dial.timer.load = result * SECTICKS; 101 } 102 dl->dial.timer.func = datalink_OpenTimeout; 103 dl->dial.timer.name = "dial"; 104 dl->dial.timer.arg = dl; 105 timer_Start(&dl->dial.timer); 106 if (dl->state == DATALINK_OPENING) 107 log_Printf(LogPHASE, "%s: Enter pause (%d) for redialing.\n", 108 dl->name, Timeout); 109 } 110 return result; 111} 112 113static void 114datalink_HangupDone(struct datalink *dl) 115{ 116 if (dl->physical->type == PHYS_DEDICATED && !dl->bundle->CleaningUp && 117 physical_GetFD(dl->physical) != -1) { 118 /* Don't close our modem if the link is dedicated */ 119 datalink_LoginDone(dl); 120 return; 121 } 122 123 modem_Close(dl->physical); 124 dl->phone.chosen = "N/A"; 125 126 if (dl->cbcp.required) { 127 log_Printf(LogPHASE, "Call peer back on %s\n", dl->cbcp.fsm.phone); 128 dl->cfg.callback.opmask = 0; 129 strncpy(dl->cfg.phone.list, dl->cbcp.fsm.phone, 130 sizeof dl->cfg.phone.list - 1); 131 dl->cfg.phone.list[sizeof dl->cfg.phone.list - 1] = '\0'; 132 dl->phone.alt = dl->phone.next = NULL; 133 dl->reconnect_tries = dl->cfg.reconnect.max; 134 dl->dial.tries = dl->cfg.dial.max; 135 dl->dial.incs = 0; 136 dl->script.run = 1; 137 dl->script.packetmode = 1; 138 if (!physical_SetMode(dl->physical, PHYS_BACKGROUND)) 139 log_Printf(LogERROR, "Oops - can't change mode to BACKGROUND (gulp) !\n"); 140 bundle_LinksRemoved(dl->bundle); 141 /* if dial.timeout is < 0 (random), we don't override fsm.delay */ 142 if (dl->cbcp.fsm.delay < dl->cfg.dial.timeout) 143 dl->cbcp.fsm.delay = dl->cfg.dial.timeout; 144 datalink_StartDialTimer(dl, dl->cbcp.fsm.delay); 145 cbcp_Down(&dl->cbcp); 146 datalink_NewState(dl, DATALINK_OPENING); 147 } else if (dl->bundle->CleaningUp || 148 (dl->physical->type == PHYS_DIRECT) || 149 ((!dl->dial.tries || (dl->dial.tries < 0 && !dl->reconnect_tries)) && 150 !(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)))) { 151 datalink_NewState(dl, DATALINK_CLOSED); 152 dl->dial.tries = -1; 153 dl->dial.incs = 0; 154 dl->reconnect_tries = 0; 155 bundle_LinkClosed(dl->bundle, dl); 156 if (!dl->bundle->CleaningUp) 157 datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl)); 158 } else { 159 datalink_NewState(dl, DATALINK_OPENING); 160 if (dl->dial.tries < 0) { 161 datalink_StartDialTimer(dl, dl->cfg.reconnect.timeout); 162 dl->dial.tries = dl->cfg.dial.max; 163 dl->dial.incs = 0; 164 dl->reconnect_tries--; 165 } else { 166 if (dl->phone.next == NULL) 167 datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl)); 168 else 169 datalink_StartDialTimer(dl, dl->cfg.dial.next_timeout); 170 } 171 } 172} 173 174static const char * 175datalink_ChoosePhoneNumber(struct datalink *dl) 176{ 177 char *phone; 178 179 if (dl->phone.alt == NULL) { 180 if (dl->phone.next == NULL) { 181 strncpy(dl->phone.list, dl->cfg.phone.list, sizeof dl->phone.list - 1); 182 dl->phone.list[sizeof dl->phone.list - 1] = '\0'; 183 dl->phone.next = dl->phone.list; 184 } 185 dl->phone.alt = strsep(&dl->phone.next, ":"); 186 } 187 phone = strsep(&dl->phone.alt, "|"); 188 dl->phone.chosen = *phone ? phone : "[NONE]"; 189 if (*phone) 190 log_Printf(LogPHASE, "Phone: %s\n", phone); 191 return phone; 192} 193 194static void 195datalink_LoginDone(struct datalink *dl) 196{ 197 if (!dl->script.packetmode) { 198 dl->dial.tries = -1; 199 dl->dial.incs = 0; 200 datalink_NewState(dl, DATALINK_READY); 201 } else if (modem_Raw(dl->physical, dl->bundle) < 0) { 202 dl->dial.tries = 0; 203 log_Printf(LogWARN, "datalink_LoginDone: Not connected.\n"); 204 if (dl->script.run) { 205 datalink_NewState(dl, DATALINK_HANGUP); 206 modem_Offline(dl->physical); 207 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL); 208 } else { 209 timer_Stop(&dl->physical->Timer); 210 if (dl->physical->type == PHYS_DEDICATED) 211 /* force a redial timeout */ 212 modem_Close(dl->physical); 213 datalink_HangupDone(dl); 214 } 215 } else { 216 dl->dial.tries = -1; 217 dl->dial.incs = 0; 218 219 hdlc_Init(&dl->physical->hdlc, &dl->physical->link.lcp); 220 async_Init(&dl->physical->async); 221 222 lcp_Setup(&dl->physical->link.lcp, dl->state == DATALINK_READY ? 223 0 : dl->physical->link.lcp.cfg.openmode); 224 ccp_Setup(&dl->physical->link.ccp); 225 226 datalink_NewState(dl, DATALINK_LCP); 227 fsm_Up(&dl->physical->link.lcp.fsm); 228 fsm_Open(&dl->physical->link.lcp.fsm); 229 } 230} 231 232static int 233datalink_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 234 int *n) 235{ 236 struct datalink *dl = descriptor2datalink(d); 237 int result; 238 239 result = 0; 240 switch (dl->state) { 241 case DATALINK_CLOSED: 242 if ((dl->physical->type & 243 (PHYS_DIRECT|PHYS_DEDICATED|PHYS_BACKGROUND|PHYS_DDIAL)) && 244 !bundle_IsDead(dl->bundle)) 245 /* 246 * Our first time in - DEDICATED & DDIAL never come down, and 247 * DIRECT & BACKGROUND get deleted when they enter DATALINK_CLOSED. 248 * Go to DATALINK_OPENING via datalink_Up() and fall through. 249 */ 250 datalink_Up(dl, 1, 1); 251 else 252 break; 253 /* fall through */ 254 255 case DATALINK_OPENING: 256 if (dl->dial.timer.state != TIMER_RUNNING) { 257 if (--dl->dial.tries < 0) 258 dl->dial.tries = 0; 259 if (modem_Open(dl->physical, dl->bundle) >= 0) { 260 log_WritePrompts(dl, "%s: Entering terminal mode on %s\r\n" 261 "Type `~?' for help\r\n", dl->name, 262 dl->physical->name.full); 263 if (dl->script.run) { 264 datalink_NewState(dl, DATALINK_DIAL); 265 chat_Init(&dl->chat, dl->physical, dl->cfg.script.dial, 1, 266 datalink_ChoosePhoneNumber(dl)); 267 if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) && 268 dl->cfg.dial.max) 269 log_Printf(LogCHAT, "%s: Dial attempt %u of %d\n", 270 dl->name, dl->cfg.dial.max - dl->dial.tries, 271 dl->cfg.dial.max); 272 } else 273 datalink_LoginDone(dl); 274 return datalink_UpdateSet(d, r, w, e, n); 275 } else { 276 if (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) && 277 dl->cfg.dial.max) 278 log_Printf(LogCHAT, "Failed to open modem (attempt %u of %d)\n", 279 dl->cfg.dial.max - dl->dial.tries, dl->cfg.dial.max); 280 else 281 log_Printf(LogCHAT, "Failed to open modem\n"); 282 283 if (dl->bundle->CleaningUp || 284 (!(dl->physical->type & (PHYS_DDIAL|PHYS_DEDICATED)) && 285 dl->cfg.dial.max && dl->dial.tries == 0)) { 286 datalink_NewState(dl, DATALINK_CLOSED); 287 dl->reconnect_tries = 0; 288 dl->dial.tries = -1; 289 log_WritePrompts(dl, "Failed to open %s\n", 290 dl->physical->name.full); 291 bundle_LinkClosed(dl->bundle, dl); 292 } 293 if (!dl->bundle->CleaningUp) { 294 int timeout; 295 296 timeout = datalink_StartDialTimer(dl, datalink_GetDialTimeout(dl)); 297 log_WritePrompts(dl, "Failed to open %s, pause %d seconds\n", 298 dl->physical->name.full, timeout); 299 } 300 } 301 } 302 break; 303 304 case DATALINK_HANGUP: 305 case DATALINK_DIAL: 306 case DATALINK_LOGIN: 307 result = descriptor_UpdateSet(&dl->chat.desc, r, w, e, n); 308 switch (dl->chat.state) { 309 case CHAT_DONE: 310 /* script succeeded */ 311 chat_Destroy(&dl->chat); 312 switch(dl->state) { 313 case DATALINK_HANGUP: 314 datalink_HangupDone(dl); 315 break; 316 case DATALINK_DIAL: 317 datalink_NewState(dl, DATALINK_LOGIN); 318 chat_Init(&dl->chat, dl->physical, dl->cfg.script.login, 0, NULL); 319 return datalink_UpdateSet(d, r, w, e, n); 320 case DATALINK_LOGIN: 321 dl->phone.alt = NULL; 322 datalink_LoginDone(dl); 323 return datalink_UpdateSet(d, r, w, e, n); 324 } 325 break; 326 case CHAT_FAILED: 327 /* Going down - script failed */ 328 log_Printf(LogWARN, "Chat script failed\n"); 329 chat_Destroy(&dl->chat); 330 switch(dl->state) { 331 case DATALINK_HANGUP: 332 datalink_HangupDone(dl); 333 break; 334 case DATALINK_DIAL: 335 case DATALINK_LOGIN: 336 datalink_NewState(dl, DATALINK_HANGUP); 337 modem_Offline(dl->physical); /* Is this required ? */ 338 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL); 339 return datalink_UpdateSet(d, r, w, e, n); 340 } 341 break; 342 } 343 break; 344 345 case DATALINK_READY: 346 case DATALINK_LCP: 347 case DATALINK_AUTH: 348 case DATALINK_CBCP: 349 case DATALINK_OPEN: 350 result = descriptor_UpdateSet(&dl->chap.desc, r, w, e, n) + 351 descriptor_UpdateSet(&dl->physical->desc, r, w, e, n); 352 break; 353 } 354 return result; 355} 356 357int 358datalink_RemoveFromSet(struct datalink *dl, fd_set *r, fd_set *w, fd_set *e) 359{ 360 return physical_RemoveFromSet(dl->physical, r, w, e); 361} 362 363static int 364datalink_IsSet(struct descriptor *d, const fd_set *fdset) 365{ 366 struct datalink *dl = descriptor2datalink(d); 367 368 switch (dl->state) { 369 case DATALINK_CLOSED: 370 case DATALINK_OPENING: 371 break; 372 373 case DATALINK_HANGUP: 374 case DATALINK_DIAL: 375 case DATALINK_LOGIN: 376 return descriptor_IsSet(&dl->chat.desc, fdset); 377 378 case DATALINK_READY: 379 case DATALINK_LCP: 380 case DATALINK_AUTH: 381 case DATALINK_CBCP: 382 case DATALINK_OPEN: 383 return descriptor_IsSet(&dl->chap.desc, fdset) ? 1 : 384 descriptor_IsSet(&dl->physical->desc, fdset); 385 } 386 return 0; 387} 388 389static void 390datalink_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset) 391{ 392 struct datalink *dl = descriptor2datalink(d); 393 394 switch (dl->state) { 395 case DATALINK_CLOSED: 396 case DATALINK_OPENING: 397 break; 398 399 case DATALINK_HANGUP: 400 case DATALINK_DIAL: 401 case DATALINK_LOGIN: 402 descriptor_Read(&dl->chat.desc, bundle, fdset); 403 break; 404 405 case DATALINK_READY: 406 case DATALINK_LCP: 407 case DATALINK_AUTH: 408 case DATALINK_CBCP: 409 case DATALINK_OPEN: 410 if (descriptor_IsSet(&dl->chap.desc, fdset)) 411 descriptor_Read(&dl->chap.desc, bundle, fdset); 412 if (descriptor_IsSet(&dl->physical->desc, fdset)) 413 descriptor_Read(&dl->physical->desc, bundle, fdset); 414 break; 415 } 416} 417 418static int 419datalink_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset) 420{ 421 struct datalink *dl = descriptor2datalink(d); 422 int result = 0; 423 424 switch (dl->state) { 425 case DATALINK_CLOSED: 426 case DATALINK_OPENING: 427 break; 428 429 case DATALINK_HANGUP: 430 case DATALINK_DIAL: 431 case DATALINK_LOGIN: 432 result = descriptor_Write(&dl->chat.desc, bundle, fdset); 433 break; 434 435 case DATALINK_READY: 436 case DATALINK_LCP: 437 case DATALINK_AUTH: 438 case DATALINK_CBCP: 439 case DATALINK_OPEN: 440 if (descriptor_IsSet(&dl->chap.desc, fdset)) 441 result += descriptor_Write(&dl->chap.desc, bundle, fdset); 442 if (descriptor_IsSet(&dl->physical->desc, fdset)) 443 result += descriptor_Write(&dl->physical->desc, bundle, fdset); 444 break; 445 } 446 447 return result; 448} 449 450static void 451datalink_ComeDown(struct datalink *dl, int how) 452{ 453 if (how != CLOSE_NORMAL) { 454 dl->dial.tries = -1; 455 dl->reconnect_tries = 0; 456 if (dl->state >= DATALINK_READY && how == CLOSE_LCP) 457 dl->stayonline = 1; 458 } 459 460 if (dl->state >= DATALINK_READY && dl->stayonline) { 461 dl->stayonline = 0; 462 timer_Stop(&dl->physical->Timer); 463 datalink_NewState(dl, DATALINK_READY); 464 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) { 465 modem_Offline(dl->physical); 466 chat_Destroy(&dl->chat); 467 if (dl->script.run && dl->state != DATALINK_OPENING) { 468 datalink_NewState(dl, DATALINK_HANGUP); 469 chat_Init(&dl->chat, dl->physical, dl->cfg.script.hangup, 1, NULL); 470 } else 471 datalink_HangupDone(dl); 472 } 473} 474 475static void 476datalink_LayerStart(void *v, struct fsm *fp) 477{ 478 /* The given FSM is about to start up ! */ 479 struct datalink *dl = (struct datalink *)v; 480 481 if (fp->proto == PROTO_LCP) 482 (*dl->parent->LayerStart)(dl->parent->object, fp); 483} 484 485static void 486datalink_LayerUp(void *v, struct fsm *fp) 487{ 488 /* The given fsm is now up */ 489 struct datalink *dl = (struct datalink *)v; 490 struct lcp *lcp = &dl->physical->link.lcp; 491 492 if (fp->proto == PROTO_LCP) { 493 datalink_GotAuthname(dl, ""); 494 lcp->auth_ineed = lcp->want_auth; 495 lcp->auth_iwait = lcp->his_auth; 496 if (lcp->his_auth || lcp->want_auth) { 497 if (bundle_Phase(dl->bundle) != PHASE_NETWORK) 498 bundle_NewPhase(dl->bundle, PHASE_AUTHENTICATE); 499 log_Printf(LogPHASE, "%s: his = %s, mine = %s\n", dl->name, 500 Auth2Nam(lcp->his_auth, lcp->his_authtype), 501 Auth2Nam(lcp->want_auth, lcp->want_authtype)); 502 if (lcp->his_auth == PROTO_PAP) 503 auth_StartReq(&dl->pap); 504 if (lcp->want_auth == PROTO_CHAP) 505 auth_StartReq(&dl->chap.auth); 506 } else 507 datalink_AuthOk(dl); 508 } 509} 510 511static void 512datalink_AuthReInit(struct datalink *dl) 513{ 514 auth_StopTimer(&dl->pap); 515 auth_StopTimer(&dl->chap.auth); 516 chap_ReInit(&dl->chap); 517} 518 519void 520datalink_GotAuthname(struct datalink *dl, const char *name) 521{ 522 strncpy(dl->peer.authname, name, sizeof dl->peer.authname - 1); 523 dl->peer.authname[sizeof dl->peer.authname - 1] = '\0'; 524} 525 526void 527datalink_NCPUp(struct datalink *dl) 528{ 529 int ccpok = ccp_SetOpenMode(&dl->physical->link.ccp); 530 531 if (dl->physical->link.lcp.want_mrru && dl->physical->link.lcp.his_mrru) { 532 /* we've authenticated in multilink mode ! */ 533 switch (mp_Up(&dl->bundle->ncp.mp, dl)) { 534 case MP_LINKSENT: 535 /* We've handed the link off to another ppp (well, we will soon) ! */ 536 return; 537 case MP_UP: 538 /* First link in the bundle */ 539 auth_Select(dl->bundle, dl->peer.authname); 540 /* fall through */ 541 case MP_ADDED: 542 /* We're in multilink mode ! */ 543 dl->physical->link.ccp.fsm.open_mode = OPEN_PASSIVE; /* override */ 544 break; 545 case MP_FAILED: 546 datalink_AuthNotOk(dl); 547 return; 548 } 549 } else if (bundle_Phase(dl->bundle) == PHASE_NETWORK) { 550 log_Printf(LogPHASE, "%s: Already in NETWORK phase\n", dl->name); 551 datalink_NewState(dl, DATALINK_OPEN); 552 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm); 553 return; 554 } else { 555 dl->bundle->ncp.mp.peer = dl->peer; 556 ipcp_SetLink(&dl->bundle->ncp.ipcp, &dl->physical->link); 557 auth_Select(dl->bundle, dl->peer.authname); 558 } 559 560 if (ccpok) { 561 fsm_Up(&dl->physical->link.ccp.fsm); 562 fsm_Open(&dl->physical->link.ccp.fsm); 563 } 564 datalink_NewState(dl, DATALINK_OPEN); 565 bundle_NewPhase(dl->bundle, PHASE_NETWORK); 566 (*dl->parent->LayerUp)(dl->parent->object, &dl->physical->link.lcp.fsm); 567} 568 569void 570datalink_CBCPComplete(struct datalink *dl) 571{ 572 datalink_NewState(dl, DATALINK_LCP); 573 datalink_AuthReInit(dl); 574 fsm_Close(&dl->physical->link.lcp.fsm); 575} 576 577void 578datalink_CBCPFailed(struct datalink *dl) 579{ 580 cbcp_Down(&dl->cbcp); 581 datalink_CBCPComplete(dl); 582} 583 584void 585datalink_AuthOk(struct datalink *dl) 586{ 587 if ((dl->physical->link.lcp.his_callback.opmask & 588 CALLBACK_BIT(CALLBACK_CBCP) || 589 dl->physical->link.lcp.want_callback.opmask & 590 CALLBACK_BIT(CALLBACK_CBCP)) && 591 !(dl->physical->link.lcp.want_callback.opmask & 592 CALLBACK_BIT(CALLBACK_AUTH))) { 593 /* We must have agreed CBCP if AUTH isn't there any more */ 594 datalink_NewState(dl, DATALINK_CBCP); 595 cbcp_Up(&dl->cbcp); 596 } else if (dl->physical->link.lcp.want_callback.opmask) { 597 /* It's not CBCP */ 598 log_Printf(LogPHASE, "%s: Shutdown and await peer callback\n", dl->name); 599 datalink_NewState(dl, DATALINK_LCP); 600 datalink_AuthReInit(dl); 601 fsm_Close(&dl->physical->link.lcp.fsm); 602 } else 603 switch (dl->physical->link.lcp.his_callback.opmask) { 604 case 0: 605 datalink_NCPUp(dl); 606 break; 607 608 case CALLBACK_BIT(CALLBACK_AUTH): 609 auth_SetPhoneList(dl->peer.authname, dl->cbcp.fsm.phone, 610 sizeof dl->cbcp.fsm.phone); 611 if (*dl->cbcp.fsm.phone == '\0' || !strcmp(dl->cbcp.fsm.phone, "*")) { 612 log_Printf(LogPHASE, "%s: %s cannot be called back\n", dl->name, 613 dl->peer.authname); 614 *dl->cbcp.fsm.phone = '\0'; 615 } else { 616 char *ptr = strchr(dl->cbcp.fsm.phone, ','); 617 if (ptr) 618 *ptr = '\0'; /* Call back on the first number */ 619 log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name, 620 dl->cbcp.fsm.phone); 621 dl->cbcp.required = 1; 622 } 623 dl->cbcp.fsm.delay = 0; 624 datalink_NewState(dl, DATALINK_LCP); 625 datalink_AuthReInit(dl); 626 fsm_Close(&dl->physical->link.lcp.fsm); 627 break; 628 629 case CALLBACK_BIT(CALLBACK_E164): 630 strncpy(dl->cbcp.fsm.phone, dl->physical->link.lcp.his_callback.msg, 631 sizeof dl->cbcp.fsm.phone - 1); 632 dl->cbcp.fsm.phone[sizeof dl->cbcp.fsm.phone - 1] = '\0'; 633 log_Printf(LogPHASE, "%s: Calling peer back on %s\n", dl->name, 634 dl->cbcp.fsm.phone); 635 dl->cbcp.required = 1; 636 dl->cbcp.fsm.delay = 0; 637 datalink_NewState(dl, DATALINK_LCP); 638 datalink_AuthReInit(dl); 639 fsm_Close(&dl->physical->link.lcp.fsm); 640 break; 641 642 default: 643 log_Printf(LogPHASE, "%s: Oops - Should have NAK'd peer callback !\n", 644 dl->name); 645 datalink_NewState(dl, DATALINK_LCP); 646 datalink_AuthReInit(dl); 647 fsm_Close(&dl->physical->link.lcp.fsm); 648 break; 649 } 650} 651 652void 653datalink_AuthNotOk(struct datalink *dl) 654{ 655 datalink_NewState(dl, DATALINK_LCP); 656 datalink_AuthReInit(dl); 657 fsm_Close(&dl->physical->link.lcp.fsm); 658} 659 660static void 661datalink_LayerDown(void *v, struct fsm *fp) 662{ 663 /* The given FSM has been told to come down */ 664 struct datalink *dl = (struct datalink *)v; 665 666 if (fp->proto == PROTO_LCP) { 667 switch (dl->state) { 668 case DATALINK_OPEN: 669 peerid_Init(&dl->peer); 670 fsm2initial(&dl->physical->link.ccp.fsm); 671 datalink_NewState(dl, DATALINK_LCP); /* before parent TLD */ 672 (*dl->parent->LayerDown)(dl->parent->object, fp); 673 /* fall through (just in case) */ 674 675 case DATALINK_CBCP: 676 if (!dl->cbcp.required) 677 cbcp_Down(&dl->cbcp); 678 /* fall through (just in case) */ 679 680 case DATALINK_AUTH: 681 timer_Stop(&dl->pap.authtimer); 682 timer_Stop(&dl->chap.auth.authtimer); 683 } 684 datalink_NewState(dl, DATALINK_LCP); 685 datalink_AuthReInit(dl); 686 } 687} 688 689static void 690datalink_LayerFinish(void *v, struct fsm *fp) 691{ 692 /* The given fsm is now down */ 693 struct datalink *dl = (struct datalink *)v; 694 695 if (fp->proto == PROTO_LCP) { 696 fsm2initial(fp); 697 (*dl->parent->LayerFinish)(dl->parent->object, fp); 698 datalink_ComeDown(dl, CLOSE_NORMAL); 699 } else if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 700 fsm_Open(fp); /* CCP goes to ST_STOPPED */ 701} 702 703struct datalink * 704datalink_Create(const char *name, struct bundle *bundle, int type) 705{ 706 struct datalink *dl; 707 708 dl = (struct datalink *)malloc(sizeof(struct datalink)); 709 if (dl == NULL) 710 return dl; 711 712 dl->desc.type = DATALINK_DESCRIPTOR; 713 dl->desc.UpdateSet = datalink_UpdateSet; 714 dl->desc.IsSet = datalink_IsSet; 715 dl->desc.Read = datalink_Read; 716 dl->desc.Write = datalink_Write; 717 718 dl->state = DATALINK_CLOSED; 719 720 *dl->cfg.script.dial = '\0'; 721 *dl->cfg.script.login = '\0'; 722 *dl->cfg.script.hangup = '\0'; 723 *dl->cfg.phone.list = '\0'; 724 *dl->phone.list = '\0'; 725 dl->phone.next = NULL; 726 dl->phone.alt = NULL; 727 dl->phone.chosen = "N/A"; 728 dl->stayonline = 0; 729 dl->script.run = 1; 730 dl->script.packetmode = 1; 731 mp_linkInit(&dl->mp); 732 733 dl->bundle = bundle; 734 dl->next = NULL; 735 736 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer); 737 738 dl->dial.tries = 0; 739 dl->cfg.dial.max = 1; 740 dl->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 741 dl->cfg.dial.timeout = DIAL_TIMEOUT; 742 dl->cfg.dial.inc = 0; 743 dl->cfg.dial.maxinc = 10; 744 745 dl->reconnect_tries = 0; 746 dl->cfg.reconnect.max = 0; 747 dl->cfg.reconnect.timeout = RECONNECT_TIMEOUT; 748 749 dl->cfg.callback.opmask = 0; 750 dl->cfg.cbcp.delay = 0; 751 *dl->cfg.cbcp.phone = '\0'; 752 dl->cfg.cbcp.fsmretry = DEF_FSMRETRY; 753 754 dl->name = strdup(name); 755 peerid_Init(&dl->peer); 756 dl->parent = &bundle->fsm; 757 dl->fsmp.LayerStart = datalink_LayerStart; 758 dl->fsmp.LayerUp = datalink_LayerUp; 759 dl->fsmp.LayerDown = datalink_LayerDown; 760 dl->fsmp.LayerFinish = datalink_LayerFinish; 761 dl->fsmp.object = dl; 762 763 if ((dl->physical = modem_Create(dl, type)) == NULL) { 764 free(dl->name); 765 free(dl); 766 return NULL; 767 } 768 769 pap_Init(&dl->pap, dl->physical); 770 chap_Init(&dl->chap, dl->physical); 771 cbcp_Init(&dl->cbcp, dl->physical); 772 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 773 774 log_Printf(LogPHASE, "%s: Created in %s state\n", 775 dl->name, datalink_State(dl)); 776 777 return dl; 778} 779 780struct datalink * 781datalink_Clone(struct datalink *odl, const char *name) 782{ 783 struct datalink *dl; 784 785 dl = (struct datalink *)malloc(sizeof(struct datalink)); 786 if (dl == NULL) 787 return dl; 788 789 dl->desc.type = DATALINK_DESCRIPTOR; 790 dl->desc.UpdateSet = datalink_UpdateSet; 791 dl->desc.IsSet = datalink_IsSet; 792 dl->desc.Read = datalink_Read; 793 dl->desc.Write = datalink_Write; 794 795 dl->state = DATALINK_CLOSED; 796 797 memcpy(&dl->cfg, &odl->cfg, sizeof dl->cfg); 798 mp_linkInit(&dl->mp); 799 *dl->phone.list = '\0'; 800 dl->phone.next = NULL; 801 dl->phone.alt = NULL; 802 dl->phone.chosen = "N/A"; 803 dl->bundle = odl->bundle; 804 dl->next = NULL; 805 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer); 806 dl->dial.tries = 0; 807 dl->reconnect_tries = 0; 808 dl->name = strdup(name); 809 peerid_Init(&dl->peer); 810 dl->parent = odl->parent; 811 memcpy(&dl->fsmp, &odl->fsmp, sizeof dl->fsmp); 812 dl->fsmp.object = dl; 813 814 if ((dl->physical = modem_Create(dl, PHYS_INTERACTIVE)) == NULL) { 815 free(dl->name); 816 free(dl); 817 return NULL; 818 } 819 pap_Init(&dl->pap, dl->physical); 820 dl->pap.cfg = odl->pap.cfg; 821 822 chap_Init(&dl->chap, dl->physical); 823 dl->chap.auth.cfg = odl->chap.auth.cfg; 824 825 memcpy(&dl->physical->cfg, &odl->physical->cfg, sizeof dl->physical->cfg); 826 memcpy(&dl->physical->link.lcp.cfg, &odl->physical->link.lcp.cfg, 827 sizeof dl->physical->link.lcp.cfg); 828 memcpy(&dl->physical->link.ccp.cfg, &odl->physical->link.ccp.cfg, 829 sizeof dl->physical->link.ccp.cfg); 830 memcpy(&dl->physical->async.cfg, &odl->physical->async.cfg, 831 sizeof dl->physical->async.cfg); 832 833 cbcp_Init(&dl->cbcp, dl->physical); 834 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 835 836 log_Printf(LogPHASE, "%s: Cloned in %s state\n", 837 dl->name, datalink_State(dl)); 838 839 return dl; 840} 841 842struct datalink * 843datalink_Destroy(struct datalink *dl) 844{ 845 struct datalink *result; 846 847 if (dl->state != DATALINK_CLOSED) { 848 log_Printf(LogERROR, "Oops, destroying a datalink in state %s\n", 849 datalink_State(dl)); 850 switch (dl->state) { 851 case DATALINK_HANGUP: 852 case DATALINK_DIAL: 853 case DATALINK_LOGIN: 854 chat_Destroy(&dl->chat); /* Gotta blat the timers ! */ 855 break; 856 } 857 } 858 859 timer_Stop(&dl->dial.timer); 860 result = dl->next; 861 modem_Destroy(dl->physical); 862 free(dl->name); 863 free(dl); 864 865 return result; 866} 867 868void 869datalink_Up(struct datalink *dl, int runscripts, int packetmode) 870{ 871 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 872 /* Ignore scripts */ 873 runscripts = 0; 874 875 switch (dl->state) { 876 case DATALINK_CLOSED: 877 if (bundle_Phase(dl->bundle) == PHASE_DEAD || 878 bundle_Phase(dl->bundle) == PHASE_TERMINATE) 879 bundle_NewPhase(dl->bundle, PHASE_ESTABLISH); 880 datalink_NewState(dl, DATALINK_OPENING); 881 dl->reconnect_tries = 882 dl->physical->type == PHYS_DIRECT ? 0 : dl->cfg.reconnect.max; 883 dl->dial.tries = dl->cfg.dial.max; 884 dl->script.run = runscripts; 885 dl->script.packetmode = packetmode; 886 break; 887 888 case DATALINK_OPENING: 889 if (!dl->script.run && runscripts) 890 dl->script.run = 1; 891 /* fall through */ 892 893 case DATALINK_DIAL: 894 case DATALINK_LOGIN: 895 case DATALINK_READY: 896 if (!dl->script.packetmode && packetmode) { 897 dl->script.packetmode = 1; 898 if (dl->state == DATALINK_READY) 899 datalink_LoginDone(dl); 900 } 901 break; 902 } 903} 904 905void 906datalink_Close(struct datalink *dl, int how) 907{ 908 /* Please close */ 909 switch (dl->state) { 910 case DATALINK_OPEN: 911 peerid_Init(&dl->peer); 912 fsm2initial(&dl->physical->link.ccp.fsm); 913 /* fall through */ 914 915 case DATALINK_CBCP: 916 case DATALINK_AUTH: 917 case DATALINK_LCP: 918 datalink_AuthReInit(dl); 919 fsm_Close(&dl->physical->link.lcp.fsm); 920 if (how != CLOSE_NORMAL) { 921 dl->dial.tries = -1; 922 dl->reconnect_tries = 0; 923 if (how == CLOSE_LCP) 924 dl->stayonline = 1; 925 } 926 break; 927 928 default: 929 datalink_ComeDown(dl, how); 930 } 931} 932 933void 934datalink_Down(struct datalink *dl, int how) 935{ 936 /* Carrier is lost */ 937 switch (dl->state) { 938 case DATALINK_OPEN: 939 peerid_Init(&dl->peer); 940 fsm2initial(&dl->physical->link.ccp.fsm); 941 /* fall through */ 942 943 case DATALINK_CBCP: 944 case DATALINK_AUTH: 945 case DATALINK_LCP: 946 fsm2initial(&dl->physical->link.lcp.fsm); 947 /* fall through */ 948 949 default: 950 datalink_ComeDown(dl, how); 951 } 952} 953 954void 955datalink_StayDown(struct datalink *dl) 956{ 957 dl->reconnect_tries = 0; 958} 959 960void 961datalink_DontHangup(struct datalink *dl) 962{ 963 if (dl->state >= DATALINK_LCP) 964 dl->stayonline = 1; 965} 966 967int 968datalink_Show(struct cmdargs const *arg) 969{ 970 prompt_Printf(arg->prompt, "Name: %s\n", arg->cx->name); 971 prompt_Printf(arg->prompt, " State: %s\n", 972 datalink_State(arg->cx)); 973 prompt_Printf(arg->prompt, " Peer name: "); 974 if (*arg->cx->peer.authname) 975 prompt_Printf(arg->prompt, "%s\n", arg->cx->peer.authname); 976 else if (arg->cx->state == DATALINK_OPEN) 977 prompt_Printf(arg->prompt, "None requested\n"); 978 else 979 prompt_Printf(arg->prompt, "N/A\n"); 980 prompt_Printf(arg->prompt, " Discriminator: %s\n", 981 mp_Enddisc(arg->cx->peer.enddisc.class, 982 arg->cx->peer.enddisc.address, 983 arg->cx->peer.enddisc.len)); 984 985 prompt_Printf(arg->prompt, "\nDefaults:\n"); 986 prompt_Printf(arg->prompt, " Phone List: %s\n", 987 arg->cx->cfg.phone.list); 988 if (arg->cx->cfg.dial.max) 989 prompt_Printf(arg->prompt, " Dial tries: %d, delay ", 990 arg->cx->cfg.dial.max); 991 else 992 prompt_Printf(arg->prompt, " Dial tries: infinite, delay "); 993 if (arg->cx->cfg.dial.next_timeout >= 0) 994 prompt_Printf(arg->prompt, "%ds/", arg->cx->cfg.dial.next_timeout); 995 else 996 prompt_Printf(arg->prompt, "random/"); 997 if (arg->cx->cfg.dial.timeout >= 0) 998 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.dial.timeout); 999 else 1000 prompt_Printf(arg->prompt, "random\n"); 1001 prompt_Printf(arg->prompt, " Reconnect tries: %d, delay ", 1002 arg->cx->cfg.reconnect.max); 1003 if (arg->cx->cfg.reconnect.timeout > 0) 1004 prompt_Printf(arg->prompt, "%ds\n", arg->cx->cfg.reconnect.timeout); 1005 else 1006 prompt_Printf(arg->prompt, "random\n"); 1007 prompt_Printf(arg->prompt, " Callback %s ", arg->cx->physical->type == 1008 PHYS_DIRECT ? "accepted: " : "requested:"); 1009 if (!arg->cx->cfg.callback.opmask) 1010 prompt_Printf(arg->prompt, "none\n"); 1011 else { 1012 int comma = 0; 1013 1014 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_NONE)) { 1015 prompt_Printf(arg->prompt, "none"); 1016 comma = 1; 1017 } 1018 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_AUTH)) { 1019 prompt_Printf(arg->prompt, "%sauth", comma ? ", " : ""); 1020 comma = 1; 1021 } 1022 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_E164)) { 1023 prompt_Printf(arg->prompt, "%sE.164", comma ? ", " : ""); 1024 if (arg->cx->physical->type != PHYS_DIRECT) 1025 prompt_Printf(arg->prompt, " (%s)", arg->cx->cfg.callback.msg); 1026 comma = 1; 1027 } 1028 if (arg->cx->cfg.callback.opmask & CALLBACK_BIT(CALLBACK_CBCP)) { 1029 prompt_Printf(arg->prompt, "%scbcp\n", comma ? ", " : ""); 1030 prompt_Printf(arg->prompt, " CBCP: delay: %ds\n", 1031 arg->cx->cfg.cbcp.delay); 1032 prompt_Printf(arg->prompt, " phone: "); 1033 if (!strcmp(arg->cx->cfg.cbcp.phone, "*")) { 1034 if (arg->cx->physical->type & PHYS_DIRECT) 1035 prompt_Printf(arg->prompt, "Caller decides\n"); 1036 else 1037 prompt_Printf(arg->prompt, "Dialback server decides\n"); 1038 } else 1039 prompt_Printf(arg->prompt, "%s\n", arg->cx->cfg.cbcp.phone); 1040 prompt_Printf(arg->prompt, " timeout: %lds\n", 1041 arg->cx->cfg.cbcp.fsmretry); 1042 } else 1043 prompt_Printf(arg->prompt, "\n"); 1044 } 1045 1046 prompt_Printf(arg->prompt, " Dial Script: %s\n", 1047 arg->cx->cfg.script.dial); 1048 prompt_Printf(arg->prompt, " Login Script: %s\n", 1049 arg->cx->cfg.script.login); 1050 prompt_Printf(arg->prompt, " Hangup Script: %s\n", 1051 arg->cx->cfg.script.hangup); 1052 return 0; 1053} 1054 1055int 1056datalink_SetReconnect(struct cmdargs const *arg) 1057{ 1058 if (arg->argc == arg->argn+2) { 1059 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]); 1060 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]); 1061 return 0; 1062 } 1063 return -1; 1064} 1065 1066int 1067datalink_SetRedial(struct cmdargs const *arg) 1068{ 1069 const char *sep, *osep; 1070 int timeout, inc, maxinc, tries; 1071 1072 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) { 1073 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 && 1074 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) { 1075 arg->cx->cfg.dial.timeout = -1; 1076 randinit(); 1077 } else { 1078 timeout = atoi(arg->argv[arg->argn]); 1079 1080 if (timeout >= 0) 1081 arg->cx->cfg.dial.timeout = timeout; 1082 else { 1083 log_Printf(LogWARN, "Invalid redial timeout\n"); 1084 return -1; 1085 } 1086 } 1087 1088 sep = strchr(arg->argv[arg->argn], '+'); 1089 if (sep) { 1090 inc = atoi(++sep); 1091 osep = sep; 1092 if (inc >= 0) 1093 arg->cx->cfg.dial.inc = inc; 1094 else { 1095 log_Printf(LogWARN, "Invalid timeout increment\n"); 1096 return -1; 1097 } 1098 sep = strchr(sep, '-'); 1099 if (sep) { 1100 maxinc = atoi(++sep); 1101 if (maxinc >= 0) 1102 arg->cx->cfg.dial.maxinc = maxinc; 1103 else { 1104 log_Printf(LogWARN, "Invalid maximum timeout increments\n"); 1105 return -1; 1106 } 1107 } else { 1108 /* Default timeout increment */ 1109 arg->cx->cfg.dial.maxinc = 10; 1110 sep = osep; 1111 } 1112 } else { 1113 /* Default timeout increment & max increment */ 1114 arg->cx->cfg.dial.inc = 0; 1115 arg->cx->cfg.dial.maxinc = 10; 1116 sep = arg->argv[arg->argn]; 1117 } 1118 1119 sep = strchr(sep, '.'); 1120 if (sep) { 1121 if (strcasecmp(++sep, "random") == 0) { 1122 arg->cx->cfg.dial.next_timeout = -1; 1123 randinit(); 1124 } else { 1125 timeout = atoi(sep); 1126 if (timeout >= 0) 1127 arg->cx->cfg.dial.next_timeout = timeout; 1128 else { 1129 log_Printf(LogWARN, "Invalid next redial timeout\n"); 1130 return -1; 1131 } 1132 } 1133 } else 1134 /* Default next timeout */ 1135 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 1136 1137 if (arg->argc == arg->argn+2) { 1138 tries = atoi(arg->argv[arg->argn+1]); 1139 1140 if (tries >= 0) { 1141 arg->cx->cfg.dial.max = tries; 1142 } else { 1143 log_Printf(LogWARN, "Invalid retry value\n"); 1144 return 1; 1145 } 1146 } 1147 return 0; 1148 } 1149 1150 return -1; 1151} 1152 1153static const char *states[] = { 1154 "closed", 1155 "opening", 1156 "hangup", 1157 "dial", 1158 "login", 1159 "ready", 1160 "lcp", 1161 "auth", 1162 "cbcp", 1163 "open" 1164}; 1165 1166const char * 1167datalink_State(struct datalink *dl) 1168{ 1169 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0]) 1170 return "unknown"; 1171 return states[dl->state]; 1172} 1173 1174static void 1175datalink_NewState(struct datalink *dl, int state) 1176{ 1177 if (state != dl->state) { 1178 if (state >= 0 && state < sizeof states / sizeof states[0]) { 1179 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl), 1180 states[state]); 1181 dl->state = state; 1182 } else 1183 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state); 1184 } 1185} 1186 1187struct datalink * 1188iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov, 1189 int fd) 1190{ 1191 struct datalink *dl, *cdl; 1192 struct fsm_retry copy; 1193 char *oname; 1194 1195 dl = (struct datalink *)iov[(*niov)++].iov_base; 1196 dl->name = iov[*niov].iov_base; 1197 1198 if (dl->name[DATALINK_MAXNAME-1]) { 1199 dl->name[DATALINK_MAXNAME-1] = '\0'; 1200 if (strlen(dl->name) == DATALINK_MAXNAME - 1) 1201 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name); 1202 } 1203 1204 /* Make sure the name is unique ! */ 1205 oname = NULL; 1206 do { 1207 for (cdl = bundle->links; cdl; cdl = cdl->next) 1208 if (!strcasecmp(dl->name, cdl->name)) { 1209 if (oname) 1210 free(datalink_NextName(dl)); 1211 else 1212 oname = datalink_NextName(dl); 1213 break; /* Keep renaming 'till we have no conflicts */ 1214 } 1215 } while (cdl); 1216 1217 if (oname) { 1218 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name); 1219 free(oname); 1220 } else { 1221 dl->name = strdup(dl->name); 1222 free(iov[*niov].iov_base); 1223 } 1224 (*niov)++; 1225 1226 dl->desc.type = DATALINK_DESCRIPTOR; 1227 dl->desc.UpdateSet = datalink_UpdateSet; 1228 dl->desc.IsSet = datalink_IsSet; 1229 dl->desc.Read = datalink_Read; 1230 dl->desc.Write = datalink_Write; 1231 1232 mp_linkInit(&dl->mp); 1233 *dl->phone.list = '\0'; 1234 dl->phone.next = NULL; 1235 dl->phone.alt = NULL; 1236 dl->phone.chosen = "N/A"; 1237 1238 dl->bundle = bundle; 1239 dl->next = NULL; 1240 memset(&dl->dial.timer, '\0', sizeof dl->dial.timer); 1241 dl->dial.tries = 0; 1242 dl->reconnect_tries = 0; 1243 dl->parent = &bundle->fsm; 1244 dl->fsmp.LayerStart = datalink_LayerStart; 1245 dl->fsmp.LayerUp = datalink_LayerUp; 1246 dl->fsmp.LayerDown = datalink_LayerDown; 1247 dl->fsmp.LayerFinish = datalink_LayerFinish; 1248 dl->fsmp.object = dl; 1249 1250 dl->physical = iov2modem(dl, iov, niov, maxiov, fd); 1251 1252 if (!dl->physical) { 1253 free(dl->name); 1254 free(dl); 1255 dl = NULL; 1256 } else { 1257 copy = dl->pap.cfg.fsm; 1258 pap_Init(&dl->pap, dl->physical); 1259 dl->pap.cfg.fsm = copy; 1260 1261 copy = dl->chap.auth.cfg.fsm; 1262 chap_Init(&dl->chap, dl->physical); 1263 dl->chap.auth.cfg.fsm = copy; 1264 1265 cbcp_Init(&dl->cbcp, dl->physical); 1266 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 1267 1268 log_Printf(LogPHASE, "%s: Transferred in %s state\n", 1269 dl->name, datalink_State(dl)); 1270 } 1271 1272 return dl; 1273} 1274 1275int 1276datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, 1277 pid_t newpid) 1278{ 1279 /* If `dl' is NULL, we're allocating before a Fromiov() */ 1280 int link_fd; 1281 1282 if (dl) { 1283 timer_Stop(&dl->dial.timer); 1284 /* The following is purely for the sake of paranoia */ 1285 cbcp_Down(&dl->cbcp); 1286 timer_Stop(&dl->pap.authtimer); 1287 timer_Stop(&dl->chap.auth.authtimer); 1288 } 1289 1290 if (*niov >= maxiov - 1) { 1291 log_Printf(LogERROR, "Toiov: No room for datalink !\n"); 1292 if (dl) { 1293 free(dl->name); 1294 free(dl); 1295 } 1296 return -1; 1297 } 1298 1299 iov[*niov].iov_base = dl ? dl : malloc(sizeof *dl); 1300 iov[(*niov)++].iov_len = sizeof *dl; 1301 iov[*niov].iov_base = 1302 dl ? realloc(dl->name, DATALINK_MAXNAME) : malloc(DATALINK_MAXNAME); 1303 iov[(*niov)++].iov_len = DATALINK_MAXNAME; 1304 1305 link_fd = modem2iov(dl ? dl->physical : NULL, iov, niov, maxiov, newpid); 1306 1307 if (link_fd == -1 && dl) { 1308 free(dl->name); 1309 free(dl); 1310 } 1311 1312 return link_fd; 1313} 1314 1315void 1316datalink_Rename(struct datalink *dl, const char *name) 1317{ 1318 free(dl->name); 1319 dl->physical->link.name = dl->name = strdup(name); 1320} 1321 1322char * 1323datalink_NextName(struct datalink *dl) 1324{ 1325 int f, n; 1326 char *name, *oname; 1327 1328 n = strlen(dl->name); 1329 name = (char *)malloc(n+3); 1330 for (f = n - 1; f >= 0; f--) 1331 if (!isdigit(dl->name[f])) 1332 break; 1333 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name); 1334 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1); 1335 oname = dl->name; 1336 dl->name = name; 1337 /* our physical link name isn't updated (it probably isn't created yet) */ 1338 return oname; 1339} 1340 1341int 1342datalink_SetMode(struct datalink *dl, int mode) 1343{ 1344 if (!physical_SetMode(dl->physical, mode)) 1345 return 0; 1346 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 1347 dl->script.run = 0; 1348 if (dl->physical->type == PHYS_DIRECT) 1349 dl->reconnect_tries = 0; 1350 if (mode & (PHYS_DDIAL|PHYS_BACKGROUND) && dl->state <= DATALINK_READY) 1351 datalink_Up(dl, 1, 1); 1352 return 1; 1353} 1354 1355int 1356datalink_GetDialTimeout(struct datalink *dl) 1357{ 1358 int result = dl->cfg.dial.timeout + dl->dial.incs * dl->cfg.dial.inc; 1359 1360 if (dl->dial.incs < dl->cfg.dial.maxinc) 1361 dl->dial.incs++; 1362 1363 return result; 1364} 1365