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