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