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.18 1998/08/09 15:34:11 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 73 static void datalink_LoginDone(struct datalink *); 74 static void datalink_NewState(struct datalink *, int); 75 76 static void 77 datalink_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 86 static void 87 datalink_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 106 static void 107 datalink_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 163 static const char * 164 datalink_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 183 static void 184 datalink_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 219 static int 220 datalink_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 340 int 341 datalink_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 346 static int 347 datalink_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 371 static void 372 datalink_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 397 static int 398 datalink_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 426 static void 427 datalink_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 451 static void 452 datalink_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 461 static void 462 datalink_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 486 void 487 datalink_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 495 void 496 datalink_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 538 void 539 datalink_CBCPComplete(struct datalink *dl) 540 { 541 datalink_NewState(dl, DATALINK_LCP); 542 fsm_Close(&dl->physical->link.lcp.fsm); 543 } 544 545 void 546 datalink_CBCPFailed(struct datalink *dl) 547 { 548 cbcp_Down(&dl->cbcp); 549 datalink_CBCPComplete(dl); 550 } 551 552 void 553 datalink_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 612 void 613 datalink_AuthNotOk(struct datalink *dl) 614 { 615 datalink_NewState(dl, DATALINK_LCP); 616 fsm_Close(&dl->physical->link.lcp.fsm); 617 } 618 619 static void 620 datalink_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 647 static void 648 datalink_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 661 struct datalink * 662 datalink_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 736 struct datalink * 737 datalink_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 797 struct datalink * 798 datalink_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 822 void 823 datalink_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 859 void 860 datalink_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 886 void 887 datalink_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 907 void 908 datalink_StayDown(struct datalink *dl) 909 { 910 dl->reconnect_tries = 0; 911 } 912 913 void 914 datalink_DontHangup(struct datalink *dl) 915 { 916 if (dl->state >= DATALINK_LCP) 917 dl->stayonline = 1; 918 } 919 920 int 921 datalink_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: %s\n", 988 arg->cx->cfg.cbcp.phone); 989 prompt_Printf(arg->prompt, " timeout: %lds\n", 990 arg->cx->cfg.cbcp.fsmretry); 991 } else 992 prompt_Printf(arg->prompt, "\n"); 993 } 994 995 prompt_Printf(arg->prompt, " Dial Script: %s\n", 996 arg->cx->cfg.script.dial); 997 prompt_Printf(arg->prompt, " Login Script: %s\n", 998 arg->cx->cfg.script.login); 999 prompt_Printf(arg->prompt, " Hangup Script: %s\n", 1000 arg->cx->cfg.script.hangup); 1001 return 0; 1002 } 1003 1004 int 1005 datalink_SetReconnect(struct cmdargs const *arg) 1006 { 1007 if (arg->argc == arg->argn+2) { 1008 arg->cx->cfg.reconnect.timeout = atoi(arg->argv[arg->argn]); 1009 arg->cx->cfg.reconnect.max = atoi(arg->argv[arg->argn+1]); 1010 return 0; 1011 } 1012 return -1; 1013 } 1014 1015 int 1016 datalink_SetRedial(struct cmdargs const *arg) 1017 { 1018 int timeout; 1019 int tries; 1020 char *dot; 1021 1022 if (arg->argc == arg->argn+1 || arg->argc == arg->argn+2) { 1023 if (strncasecmp(arg->argv[arg->argn], "random", 6) == 0 && 1024 (arg->argv[arg->argn][6] == '\0' || arg->argv[arg->argn][6] == '.')) { 1025 arg->cx->cfg.dial.timeout = -1; 1026 randinit(); 1027 } else { 1028 timeout = atoi(arg->argv[arg->argn]); 1029 1030 if (timeout >= 0) 1031 arg->cx->cfg.dial.timeout = timeout; 1032 else { 1033 log_Printf(LogWARN, "Invalid redial timeout\n"); 1034 return -1; 1035 } 1036 } 1037 1038 dot = strchr(arg->argv[arg->argn], '.'); 1039 if (dot) { 1040 if (strcasecmp(++dot, "random") == 0) { 1041 arg->cx->cfg.dial.next_timeout = -1; 1042 randinit(); 1043 } else { 1044 timeout = atoi(dot); 1045 if (timeout >= 0) 1046 arg->cx->cfg.dial.next_timeout = timeout; 1047 else { 1048 log_Printf(LogWARN, "Invalid next redial timeout\n"); 1049 return -1; 1050 } 1051 } 1052 } else 1053 /* Default next timeout */ 1054 arg->cx->cfg.dial.next_timeout = DIAL_NEXT_TIMEOUT; 1055 1056 if (arg->argc == arg->argn+2) { 1057 tries = atoi(arg->argv[arg->argn+1]); 1058 1059 if (tries >= 0) { 1060 arg->cx->cfg.dial.max = tries; 1061 } else { 1062 log_Printf(LogWARN, "Invalid retry value\n"); 1063 return 1; 1064 } 1065 } 1066 return 0; 1067 } 1068 return -1; 1069 } 1070 1071 static const char *states[] = { 1072 "closed", 1073 "opening", 1074 "hangup", 1075 "dial", 1076 "login", 1077 "ready", 1078 "lcp", 1079 "auth", 1080 "cbcp", 1081 "open" 1082 }; 1083 1084 const char * 1085 datalink_State(struct datalink *dl) 1086 { 1087 if (dl->state < 0 || dl->state >= sizeof states / sizeof states[0]) 1088 return "unknown"; 1089 return states[dl->state]; 1090 } 1091 1092 static void 1093 datalink_NewState(struct datalink *dl, int state) 1094 { 1095 if (state != dl->state) { 1096 if (state >= 0 && state < sizeof states / sizeof states[0]) { 1097 log_Printf(LogPHASE, "%s: %s -> %s\n", dl->name, datalink_State(dl), 1098 states[state]); 1099 dl->state = state; 1100 } else 1101 log_Printf(LogERROR, "%s: Can't enter state %d !\n", dl->name, state); 1102 } 1103 } 1104 1105 struct datalink * 1106 iov2datalink(struct bundle *bundle, struct iovec *iov, int *niov, int maxiov, 1107 int fd) 1108 { 1109 struct datalink *dl, *cdl; 1110 u_int retry; 1111 char *oname; 1112 1113 dl = (struct datalink *)iov[(*niov)++].iov_base; 1114 dl->name = iov[*niov].iov_base; 1115 1116 if (dl->name[DATALINK_MAXNAME-1]) { 1117 dl->name[DATALINK_MAXNAME-1] = '\0'; 1118 if (strlen(dl->name) == DATALINK_MAXNAME - 1) 1119 log_Printf(LogWARN, "Datalink name truncated to \"%s\"\n", dl->name); 1120 } 1121 1122 /* Make sure the name is unique ! */ 1123 oname = NULL; 1124 do { 1125 for (cdl = bundle->links; cdl; cdl = cdl->next) 1126 if (!strcasecmp(dl->name, cdl->name)) { 1127 if (oname) 1128 free(datalink_NextName(dl)); 1129 else 1130 oname = datalink_NextName(dl); 1131 break; /* Keep renaming 'till we have no conflicts */ 1132 } 1133 } while (cdl); 1134 1135 if (oname) { 1136 log_Printf(LogPHASE, "Rename link %s to %s\n", oname, dl->name); 1137 free(oname); 1138 } else { 1139 dl->name = strdup(dl->name); 1140 free(iov[*niov].iov_base); 1141 } 1142 (*niov)++; 1143 1144 dl->desc.type = DATALINK_DESCRIPTOR; 1145 dl->desc.UpdateSet = datalink_UpdateSet; 1146 dl->desc.IsSet = datalink_IsSet; 1147 dl->desc.Read = datalink_Read; 1148 dl->desc.Write = datalink_Write; 1149 1150 mp_linkInit(&dl->mp); 1151 *dl->phone.list = '\0'; 1152 dl->phone.next = NULL; 1153 dl->phone.alt = NULL; 1154 dl->phone.chosen = "N/A"; 1155 1156 dl->bundle = bundle; 1157 dl->next = NULL; 1158 memset(&dl->dial_timer, '\0', sizeof dl->dial_timer); 1159 dl->dial_tries = 0; 1160 dl->reconnect_tries = 0; 1161 dl->parent = &bundle->fsm; 1162 dl->fsmp.LayerStart = datalink_LayerStart; 1163 dl->fsmp.LayerUp = datalink_LayerUp; 1164 dl->fsmp.LayerDown = datalink_LayerDown; 1165 dl->fsmp.LayerFinish = datalink_LayerFinish; 1166 dl->fsmp.object = dl; 1167 1168 retry = dl->pap.cfg.fsmretry; 1169 auth_Init(&dl->pap); 1170 dl->pap.cfg.fsmretry = retry; 1171 1172 retry = dl->chap.auth.cfg.fsmretry; 1173 auth_Init(&dl->chap.auth); 1174 dl->chap.auth.cfg.fsmretry = retry; 1175 1176 dl->physical = iov2modem(dl, iov, niov, maxiov, fd); 1177 1178 if (!dl->physical) { 1179 free(dl->name); 1180 free(dl); 1181 dl = NULL; 1182 } else { 1183 cbcp_Init(&dl->cbcp, dl->physical); 1184 chat_Init(&dl->chat, dl->physical, NULL, 1, NULL); 1185 1186 log_Printf(LogPHASE, "%s: Transferred in %s state\n", 1187 dl->name, datalink_State(dl)); 1188 } 1189 1190 return dl; 1191 } 1192 1193 int 1194 datalink2iov(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, 1195 pid_t newpid) 1196 { 1197 /* If `dl' is NULL, we're allocating before a Fromiov() */ 1198 int link_fd; 1199 1200 if (dl) { 1201 timer_Stop(&dl->dial_timer); 1202 /* The following is purely for the sake of paranoia */ 1203 cbcp_Down(&dl->cbcp); 1204 timer_Stop(&dl->pap.authtimer); 1205 timer_Stop(&dl->chap.auth.authtimer); 1206 } 1207 1208 if (*niov >= maxiov - 1) { 1209 log_Printf(LogERROR, "Toiov: No room for datalink !\n"); 1210 if (dl) { 1211 free(dl->name); 1212 free(dl); 1213 } 1214 return -1; 1215 } 1216 1217 iov[*niov].iov_base = dl ? dl : malloc(sizeof *dl); 1218 iov[(*niov)++].iov_len = sizeof *dl; 1219 iov[*niov].iov_base = 1220 dl ? realloc(dl->name, DATALINK_MAXNAME) : malloc(DATALINK_MAXNAME); 1221 iov[(*niov)++].iov_len = DATALINK_MAXNAME; 1222 1223 link_fd = modem2iov(dl ? dl->physical : NULL, iov, niov, maxiov, newpid); 1224 1225 if (link_fd == -1 && dl) { 1226 free(dl->name); 1227 free(dl); 1228 } 1229 1230 return link_fd; 1231 } 1232 1233 void 1234 datalink_Rename(struct datalink *dl, const char *name) 1235 { 1236 free(dl->name); 1237 dl->physical->link.name = dl->name = strdup(name); 1238 } 1239 1240 char * 1241 datalink_NextName(struct datalink *dl) 1242 { 1243 int f, n; 1244 char *name, *oname; 1245 1246 n = strlen(dl->name); 1247 name = (char *)malloc(n+3); 1248 for (f = n - 1; f >= 0; f--) 1249 if (!isdigit(dl->name[f])) 1250 break; 1251 n = sprintf(name, "%.*s-", dl->name[f] == '-' ? f : f + 1, dl->name); 1252 sprintf(name + n, "%d", atoi(dl->name + f + 1) + 1); 1253 oname = dl->name; 1254 dl->name = name; 1255 /* our physical link name isn't updated (it probably isn't created yet) */ 1256 return oname; 1257 } 1258 1259 int 1260 datalink_SetMode(struct datalink *dl, int mode) 1261 { 1262 if (!physical_SetMode(dl->physical, mode)) 1263 return 0; 1264 if (dl->physical->type & (PHYS_DIRECT|PHYS_DEDICATED)) 1265 dl->script.run = 0; 1266 if (dl->physical->type == PHYS_DIRECT) 1267 dl->reconnect_tries = 0; 1268 if (mode & (PHYS_DDIAL|PHYS_BACKGROUND) && dl->state <= DATALINK_READY) 1269 datalink_Up(dl, 1, 1); 1270 return 1; 1271 } 1272