LanMan 35.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
*
*  LanMan.C -- Lan Manager network client main module
*
*  Versions
*
*  28-02-94 INH Derived from FSinC (added c.Statics)
*
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
Stewart Brodie's avatar
Stewart Brodie committed
29
#include <locale.h>
30 31 32 33 34
#include <string.h>

#include "kernel.h"
#include "swis.h"

Ben Avison's avatar
Ben Avison committed
35
#include "Global/FileTypes.h"
36 37 38 39 40 41 42 43 44 45
#include "stdtypes.h"
#include "lanman.h"
#include "lmvars.h"
#include "Params.h"
#include "corefn.h"

#include "buflib.h"
#include "netbios.h"
#include "SMB.h"
#include "xlate.h"
Ben Avison's avatar
Ben Avison committed
46
#include "VersionNum"
47 48 49 50 51
#include "omni.h"
#include "printers.h"
#include "RPC.h"
#include "stats.h"
#include "logon.h"
Stewart Brodie's avatar
Stewart Brodie committed
52
#include "sys/dcistructs.h"
53 54 55 56 57

#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"

Stewart Brodie's avatar
Stewart Brodie committed
58 59
#include "LanMan_MH.h"

60
static volatile int callbackflag = 0;
61 62 63 64 65 66

/* ---------------------------------- */

/* Added SNB 980224 */
extern char *LM_Status(void);

67
#define DEFAULT_ETHER_TYPE 	"_default_"
68 69

typedef void (*pfnShutdown)(void);
Stewart Brodie's avatar
Stewart Brodie committed
70
typedef void (*pfnTransportInit)(void);
71 72 73 74 75 76 77 78

enum {
        LMInitState_Uninitialised,
        LMInitState_PreInit,
        LMInitState_FullyInited,
        LMInitState_Boot
};

Stewart Brodie's avatar
Stewart Brodie committed
79 80 81
static void startcallbacks(void);
static void stopcallbacks(void);

82 83
static _kernel_oserror *LM_init_phase_2(void);

Stewart Brodie's avatar
Stewart Brodie committed
84 85 86 87
/* --------------------- */

struct LMvars LM_Vars;

88 89 90 91
/* ---------------------------------- */

int  LM_pw;          /* Our module's private word */
struct NETBIOS_TRANSPORT *NB_ActiveTransport; /* Selected net transport */
Stewart Brodie's avatar
Stewart Brodie committed
92
static pfnTransportInit NB_InitedTransport; /* Identify active transport */
93 94
static bool LM_Declared;  /* True if we're registered as a filing system */

Ben Avison's avatar
Ben Avison committed
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

#define CMOS_FSNUMBER   5
/* Contains number of FS to be booted */
#define CMOS_FSFLAGS    16
/* Contains flags, bit 4 is set if booting is enabled */
#define FSF_BOOT        0x10

#define CMOS_FSSTAT     1 /* NAS 11/Feb/97 */
#define CMOS_FSERVER1   2 /* NAS 11/Feb/97 */
#define CMOS_FSERVER    158 /* NAS 11/Feb/97 */
#define FSERVER_LEN     (172-159+1)
/* Locations in CMOS containing file server name.
   It's assumed this is less than NAME_LIMIT. If it's not, put FSERVER_LEN
   at NAME_LIMIT-1, and it'll have to be truncated. */

#define CMOS_NB_TYPE    0x6F
#define	NBTYPE_IP_BIT	(1<<0)

/* Can select IP or NetBEUI transport with this bit in CMOS. It's 0 if
   we should use NetBEUI, 1 if IP */


/* ------------------ */

int RdCMOS ( int addr )
{
  return (_kernel_osbyte ( 161, addr, 0 ) & 0xFF00 ) >> 8;
}

/* ------------------ */

Stewart Brodie's avatar
Stewart Brodie committed
127
static void WrCMOS ( int addr, unsigned int data )
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
  _kernel_osbyte ( 162, addr, data );
}

/* --------------------- */

static void SetFSName(char *buf_in)
{
  int i, t;

  WrCMOS(CMOS_FSSTAT, 0);
  WrCMOS(CMOS_FSERVER1, buf_in[0]);
  if (buf_in[0] == 0) return;

  for (i=1; i < FSERVER_LEN; i++)
  {
    /* NAS 11/Feb/97 */
    t = buf_in[i];
    if (t <= 0x20 || t > 0x7F) t = 0;
    WrCMOS ( CMOS_FSERVER+i-1, t);
    if (t == 0) break;
  }
}

/* --------------------- */

static void GetFSName ( char *buf_out )
{
  int i;

Stewart Brodie's avatar
Stewart Brodie committed
158 159 160 161
  if (!_kernel_getenv("Inet$ServerName", buf_out, CMOS_FSERVER)) {
          return;
  }

162 163 164 165 166 167 168 169 170 171 172
  if ( RdCMOS ( CMOS_FSSTAT ) == 0 )
  {
    /* NAS 11/Feb/97 */
    *buf_out++ = RdCMOS ( CMOS_FSERVER1 );
    for ( i=1; i < FSERVER_LEN; i++ )
      *buf_out++ = RdCMOS ( CMOS_FSERVER+i-1 );
  }

  *buf_out = 0;
}

173 174 175 176 177 178 179 180 181 182 183
/* --------------------- */

enum dps_reason {
  protocolstatus_STARTING,
  protocolstatus_TERMINATING
};

static void lanman_announce_lanmanfs(enum dps_reason r2, void *pw)
{
#ifndef NO_NETBEUI
  /* Send the service call announcing the presence/absence of a protocol module */
184
  if (NB_InitedTransport == NB_NetBEUI_Setup)
185 186 187 188 189 190
    (void) _swix(OS_ServiceCall, _INR(0,4), pw,
      Service_DCIProtocolStatus, r2, DCIVERSION, Module_Title);
#endif
}


191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
/* Finalisation code ------------------------------------ */

static void LM_Undeclare ( void )
{
  _kernel_swi_regs r;

  if ( LM_Declared )
  {
    r.r[0] = FSControl_RemoveFilingSystem;
    r.r[1] = (int)FilingSystemName;

    _kernel_swi( XOS_Bit | OS_FSControl, &r, &r );
    LM_Declared = false;
  }
}

/* ------------------ */

Stewart Brodie's avatar
Stewart Brodie committed
209
static void LM_GracefulClosedown(void)
210
{
211
  /*if (LM_Vars.initialised == LMInitState_FullyInited)*/ {
Stewart Brodie's avatar
Stewart Brodie committed
212
    stopcallbacks();
213 214
    SMB_Shutdown();            /* Dismounts all SMB mounts first as */
    Omni_Shutdown();           /* Omni_Shutdown zeros the Mounts lists */
215
    OmniS_ResourceShutdown();
216 217 218
    Prn_Shutdown();            /* Free print jobs */
    NB_Shutdown();             /* Also calls LLC_Shutdown */
    Buf_Shutdown();            /* Tell MBufManager we're done */
219 220
    LM_Vars.initialised = LMInitState_PreInit;
  }
Stewart Brodie's avatar
Stewart Brodie committed
221 222 223 224 225 226 227
}

_kernel_oserror * LM_Finalise (int fatal, int podule, void *pw)
{
  debug0("Finalise\n");
  (void) fatal;
  (void) podule;
228

229
  /* Dismount all (NB.Before announcing we're done with the protocol) */
Stewart Brodie's avatar
Stewart Brodie committed
230
  LM_GracefulClosedown();
231 232 233
  /* Tell the DCI driver the NetBEUI protocol is gone */
  lanman_announce_lanmanfs(protocolstatus_TERMINATING, pw);
  /* Now mark this filing system as gone */
234
  LM_Undeclare();
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249
  return NULL;
}


/* LM_declare ------------------------------------------ */

static _kernel_oserror *LM_Declare ( void )
{
  _kernel_oserror *err;
  int InfoBlk [ Information_Block_Size ];

  LM_Declared = false;

  InfoBlk[0] = (int)FilingSystemName             - (int)Image_RO_Base;
Stewart Brodie's avatar
Stewart Brodie committed
250
  InfoBlk[1] = -1; /* Self-ident - request call FSFunc_PrintStartupBanner */
251 252 253 254 255 256 257 258 259
  InfoBlk[2] = (int) veneer_fsentry_open         - (int)Image_RO_Base;
  InfoBlk[3] = (int) veneer_fsentry_getbytes     - (int)Image_RO_Base;
  InfoBlk[4] = (int) veneer_fsentry_putbytes     - (int)Image_RO_Base;
  InfoBlk[5] = (int) veneer_fsentry_args         - (int)Image_RO_Base;
  InfoBlk[6] = (int) veneer_fsentry_close        - (int)Image_RO_Base;
  InfoBlk[7] = (int) veneer_fsentry_file         - (int)Image_RO_Base;
  InfoBlk[8] = Information_Word;
  InfoBlk[9] = (int) veneer_fsentry_func         - (int)Image_RO_Base;
  InfoBlk[10] = (int)veneer_fsentry_gbpb         - (int)Image_RO_Base;
Stewart Brodie's avatar
Stewart Brodie committed
260
  InfoBlk[11] = Information2_Word;
261

Stewart Brodie's avatar
Stewart Brodie committed
262 263
  err = _swix(OS_FSControl, _INR(0,3), FSControl_AddFilingSystem, Image_RO_Base,
    (int)InfoBlk - (int)Image_RO_Base, LM_pw);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  if ( err == NULL )
    LM_Declared=true;

  return err;
}


/* *Command processor ------------------------------------ */

#define MAX_ARGS    20
#define MAX_CMDLEN  256

static char Cmdbuf[ MAX_CMDLEN ];

/* --------------------- */

Stewart Brodie's avatar
Stewart Brodie committed
280
static int GetArgs ( const char *args, char *argv_out[], int maxargs )
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
{
  /* Splits "args" into a number of separate arguments. Each one is
     copied into Cmdbuf so it is held statically. Pointers to the
     start of each one are put in argv_out. Any unused spaces in
     argv_out are set to NULL for convenience. Returns the number
     of arguments actually found.  */

  int i, argc;

  for (i=0; i < maxargs; i++ ) argv_out[i] = NULL;

  i=0; /* In this loop, i counts characters in Cmdbuf */
  argc=0;

  do
  {
    while ( *args == ' ' ) /* Skip spaces */
      args++;

    if ( *args < ' ' )  /* The end */
      break;

    /* Got one */

    if ( argc >= maxargs )  /* Too many! */
      break;

    argv_out[argc++] = Cmdbuf+i;

    while ( i < MAX_CMDLEN-1 && *args > ' ' ) /* Copy it */
      Cmdbuf[i++] = *args++;

    Cmdbuf[i++] = 0;     /* Terminate it */
  }
    while ( i < MAX_CMDLEN-1 );

  return argc;
}

/* --------------------- */

322
static _kernel_oserror *Cmd_LanMan ( const char *args )
323 324 325 326 327 328 329 330 331 332 333 334 335
{
  /* No args! */
  _kernel_swi_regs r;
  (void) args;

  r.r[0] = FSControl_SelectFilingSystem;
  r.r[1] = (int)FilingSystemName;

  return _kernel_swi( XOS_Bit | OS_FSControl, &r, &r );
}

/* --------------------- */

336
static _kernel_oserror *Cmd_Free ( const char *args )
Stewart Brodie's avatar
Stewart Brodie committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
{
  _kernel_swi_regs r;
  _kernel_oserror *e;
  int values[6];
  char *argv[1];

  if (GetArgs(args, argv, sizeof(argv)/sizeof(*argv)) == 0)
    argv[0] = getenv("FileSwitch$" FilingSystemName "$CSD");

  if (argv[0] == NULL)
    return Xlt_Error( EBADDRV );

  r.r[0] = 4; /* 64-bit free space - will not fail! */
  r.r[1] = Our_FS_Number;
  r.r[2] = (int) values;
  r.r[3] = (int) argv[0];
  e = Omni_FreeOp_SWI(&r);

  if (e == NULL) {
    if (values[3] == 0 && values[5] == 0) {
Ben Avison's avatar
Ben Avison committed
357
      printf("Bytes free &%08X\n", values[2] );
Stewart Brodie's avatar
Stewart Brodie committed
358
      if (values[4] != -1) {
Ben Avison's avatar
Ben Avison committed
359
        printf("Bytes used &%08X\n", values[4] );
Stewart Brodie's avatar
Stewart Brodie committed
360 361 362 363 364 365
      }
      else {
        printf("Bytes used unavailable\n");
      }
    }
    else {
Ben Avison's avatar
Ben Avison committed
366
      printf("Bytes free &%08X%08X\n", values[3], values[2] );
Stewart Brodie's avatar
Stewart Brodie committed
367
      if (values[4] != -1) {
Ben Avison's avatar
Ben Avison committed
368
        printf("Bytes used &%08X%08X\n", values[5], values[4] );
Stewart Brodie's avatar
Stewart Brodie committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382
      }
      else {
        printf("Bytes used unavailable\n");
      }
    }
  }

#ifdef TRACE
  module_printf(NULL);
#endif

  return e;
}

Stewart Brodie's avatar
Stewart Brodie committed
383
/* --------------------- */
384
static _kernel_oserror *Cmd_ListFS ( const char *args )
Stewart Brodie's avatar
Stewart Brodie committed
385 386 387 388 389 390 391 392 393 394 395
{
  char *argv[1];

  if (GetArgs(args, argv, sizeof(argv)/sizeof(*argv)) == 0) {
    return Omni_DumpServers();
  }
  else {
    return Omni_DumpShares(argv[0]);
  }
}

Stewart Brodie's avatar
Stewart Brodie committed
396 397
/* --------------------- */

398
static _kernel_oserror *Cmd_LMConnect ( const char *args )
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
{
  char *argv[5];
  err_t res;
  int   tmp;

  if ( GetArgs ( args, argv, 5 ) < 3 )
    return Xlt_Error(EBADPARAM);

  /* Convert '-' user name to NULL */

  if ( argv[3] != NULL    &&
       argv[3][0] == '-'  &&
       argv[3][1] == 0 )
    argv[3] = NULL;

  res =  Omni_MountServer ( argv[1],  /* Server */
          argv[3], /* User name or NULL */
          argv[4], /* password or NULL */
          argv[0], /* Mountname */
          argv[2], /* Drive name */
          &tmp ); /* MountID_out */

  Omni_RecheckInfo ( RI_MOUNTS );
  return Xlt_Error(res);
}

/* --------------------- */

427
static _kernel_oserror *Cmd_LMDisconnect ( const char *args )
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
{
  char *argv[1];
  int mountID;
  err_t res;

  if ( GetArgs( args, argv, 1 ) < 1 )
    return Xlt_Error(EBADPARAM);

  mountID = Omni_GetMountID(argv[0]);
  if ( mountID == 0 ) return Xlt_Error ( EBADDRV );

  res = Omni_DismountServer(mountID);
  Omni_RecheckInfo ( RI_MOUNTS );
  return Xlt_Error (res);
}


/* --------------------- */

Stewart Brodie's avatar
Stewart Brodie committed
447 448 449 450 451 452
static void LanMan_InitTransport(pfnTransportInit i)
{
        NB_InitedTransport = i;
        (*i)();
}

453 454 455 456 457 458
static void SetDefaultVars ( void )
{
  char *name;
  char fs_name [ FSERVER_LEN+1 ];

#ifdef DEBUG
Ben Avison's avatar
Ben Avison committed
459
  LM_Vars.verbose = true; /* DEBUG NAS */
460
#else
Ben Avison's avatar
Ben Avison committed
461
  LM_Vars.verbose = false;
462 463 464
#endif

  LM_Vars.initialised = LMInitState_Uninitialised;
Ben Avison's avatar
Ben Avison committed
465 466 467
  LM_Vars.namemode = NM_FIRSTCAPS;

  /* See where IP packets are going to be sent */
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  name = getenv("Inet$EtherType");
  strcpyn ( LM_Vars.drivername,
                (name==NULL) ? DEFAULT_ETHER_TYPE : name, NAME_LIMIT );

  name = getenv("Inet$HostName");
  /* Trap added to catch 'ARM_NoName', as set by STB OS */
  if ((name == NULL) || (stricmp(name,"ARM_NoName")==0))
    strcpy ( LM_Vars.machinename,   "" );
  else
  {
    strcpyn_upper ( LM_Vars.machinename, name, NAME_LIMIT );
    /* Truncate at first dot - it might be a full domain name */
    name = strchr ( LM_Vars.machinename, '.' );
    if ( name != NULL ) *name = 0;
  }

Ben Avison's avatar
Ben Avison committed
484 485 486 487 488 489 490 491 492 493 494 495 496
  /* See if the user has overridden the default filetype */
  name = getenv("LanMan$DefaultType");
  if (name != NULL)
  {
    _swix(OS_FSControl, _INR(0,1)|_OUT(2), 31, name, &LM_Vars.default_type);
  }
  else
  {
    LM_Vars.default_type = CNV_DEFAULT_TYPE;
  }

  /* Read the base of the OS ROM so there's an address to use for
   * untyped load/exec address, ensuring they would abort if used
497
   */
Ben Avison's avatar
Ben Avison committed
498 499
  _swix(OS_Module, _INR(0,2)|_OUT(3), 12, 0, 0, &LM_Vars.untyped_address);
  LM_Vars.untyped_address = LM_Vars.untyped_address & ~((1<<20) - 1);
500

Ben Avison's avatar
Ben Avison committed
501 502 503
  /* Following 'get from CMOS' bits added 980127:RCE
   * to address user complaints that it has to be typed in every time
   */
504 505 506 507 508 509 510 511
  Lgn_Init();
  GetFSName(fs_name);
  if (fs_name[0] != '\0')
  {
    strcpy(LM_Vars.workgroup,fs_name);
  }

  /* Set transport type from CMOS, unless overridden on command line */
512 513 514
#ifdef NO_NETBEUI
  LanMan_InitTransport(NB_NBIP_Setup);
#else
Stewart Brodie's avatar
Stewart Brodie committed
515
  LanMan_InitTransport(RdCMOS ( CMOS_NB_TYPE ) & NBTYPE_IP_BIT  ?
516 517
    NB_NBIP_Setup : NB_NetBEUI_Setup);
#endif
518 519 520 521
}

/* --------------------- */

522
static _kernel_oserror *Cmd_LMLogoff( const char *args )
523 524 525 526 527 528 529 530
{
  (void) args;
  Lgn_Logoff();
  return NULL;
}

/* --------------------- */

531
static _kernel_oserror *Cmd_LMLogon( const char *args )
532 533 534 535 536 537 538 539 540 541 542 543 544
{
  char *argv[3];
  if ( GetArgs(args, argv, 3) < 2 )
    return Xlt_Error(EBADPARAM);

  /* argv[2] may be NULL for blank password */

  return Xlt_Error (Lgn_Logon ( argv[0], argv[1], argv[2] ) );
}


/* --------------------- */

545
static _kernel_oserror *Cmd_LMInfo ( const char *args )
546 547 548 549
{
  (void) args;
  Omni_Debug();
  if (LM_Vars.verbose)
Ben Avison's avatar
Ben Avison committed
550 551 552 553
  {
    printf("Status: %s\n", LM_Status());
    Stat_Show();
  }
554 555 556 557 558
  return NULL;
}

/* --------------------- */

559
static _kernel_oserror *Cmd_LMNameMode ( const char *args )
560 561 562 563 564 565 566
{
  char *argv[1];
  int tmp;

  if ( GetArgs( args, argv, 1 ) < 1 ||
       sscanf( argv[0], "%d", &tmp) != 1 ||
       tmp < 0 ||
Stewart Brodie's avatar
Stewart Brodie committed
567 568 569
#ifdef TRACE
       0
#else
570
       tmp > 2
Stewart Brodie's avatar
Stewart Brodie committed
571
#endif
572 573 574 575 576 577 578 579 580
     )
    return Xlt_Error (EBADPARAM);

  LM_Vars.namemode = tmp;
  return NULL;
}

/* --------------------- */

581
static _kernel_oserror *Cmd_LMServer ( const char *args )
582 583 584 585 586 587 588 589 590
{
  char *argv[MAX_ARGS];
  int i;

  if ( GetArgs( args, argv, MAX_ARGS ) < 1 )
    return Xlt_Error ( EBADPARAM );

  if ( argv[1] == NULL ) /* Just name of server */
  {
Stewart Brodie's avatar
Stewart Brodie committed
591
    Omni_AddInfo ( OAI_SERVER, argv[0], NULL, NULL );
592 593 594 595
  }
  else
  {
    for ( i=1; i < MAX_ARGS && argv[i] != NULL; i++ )
Stewart Brodie's avatar
Stewart Brodie committed
596
      Omni_AddInfo ( OAI_DISK, argv[0], argv[i], NULL );
597 598 599 600 601 602 603 604
  }

  Omni_RecheckInfo (RI_SERVERS);
  return NULL;
}

/* --------------------- */

605
static _kernel_oserror *Cmd_LMPrinters ( const char *args )
606 607 608 609 610 611 612 613 614
{
  char *argv[MAX_ARGS];
  int i;

  if ( GetArgs( args, argv, MAX_ARGS ) < 1 )
    return Xlt_Error ( EBADPARAM );

  if ( argv[1] == NULL ) /* Just name of server */
  {
Stewart Brodie's avatar
Stewart Brodie committed
615
    Omni_AddInfo ( OAI_SERVER, argv[0], NULL, NULL );
616 617 618 619
  }
  else
  {
    for ( i=1; i < MAX_ARGS && argv[i] != NULL; i++ )
Stewart Brodie's avatar
Stewart Brodie committed
620
      Omni_AddInfo ( OAI_PRINTER, argv[0], argv[i], NULL );
621 622 623 624 625 626 627 628 629
  }

  Omni_RecheckInfo (RI_SERVERS);
  Omni_RecheckInfo (RI_PRINTERS);
  return NULL;
}

/* --------------------- */

Stewart Brodie's avatar
Stewart Brodie committed
630
static _kernel_oserror *Cmd_FS ( const char *args )
631 632 633 634 635 636 637 638 639 640 641 642 643
{
  /* This is a *Configure/Status command handler - it's therefore unusual */
  char *argv[1];

  if ( (int) args == 0 ) /* Show 'Configure' options */
  {                      /* Syntax corrected 980127 RCE */
    printf("FS           <domain-name> | <file-server>\n");
    return NULL;
  }
  else if ( (int) args == 1 ) /* Show FS status */
  {
    char buf[NAME_LIMIT];
    GetFSName(buf);
644
    printf("FS           %s\n", buf[0] ? buf : "<unset>");
645 646 647 648 649 650 651 652 653 654 655 656
    return NULL;
  }

  if ( GetArgs( args, argv, 1 ) < 1 )
    return Xlt_Error ( EBADPARAM );

  SetFSName(argv[0]);
  return NULL;
}

/* --------------------- */

657
static _kernel_oserror *Cmd_LMTransport ( const char *args )
658 659 660 661 662
{
  /* This is a *Configure/Status command handler */
  char *argv[1];
  char *tp1 = "IP";

663 664 665 666 667 668 669 670 671
#ifdef NO_NETBEUI
  if (args == arg_CONFIGURE_SYNTAX || args == arg_STATUS)
  {
    printf("LMTransport  IP\n");
    return NULL;
  }
#else
  char *tp0 = "NetBEUI";
  if ( args == arg_CONFIGURE_SYNTAX ) /* Show 'Configure' options */
672 673 674 675
  {
    printf("LMTransport  [%s | %s]\n", tp0, tp1);
    return NULL;
  }
676
  else if ( args == arg_STATUS ) /* Show FS status */
677 678 679 680 681
  {
    printf("LMTransport  %s\n",
      (RdCMOS(CMOS_NB_TYPE) & NBTYPE_IP_BIT) ? tp1:tp0);
    return NULL;
  }
682
#endif
683 684 685
  if ( GetArgs( args, argv, 1 ) < 1 )
    return Xlt_Error ( EBADPARAM );

686
#ifndef NO_NETBEUI
687 688
  if ( stricmp ( argv[0], tp0 ) == 0 )
    WrCMOS(CMOS_NB_TYPE, RdCMOS(CMOS_NB_TYPE) & ~NBTYPE_IP_BIT );
689 690 691
  else
#endif
  if ( stricmp ( argv[0], tp1 ) == 0 )
692 693 694 695 696 697 698 699
    WrCMOS(CMOS_NB_TYPE, RdCMOS(CMOS_NB_TYPE) | NBTYPE_IP_BIT );
  else
    printf("'%s' isn't a supported option\n", argv[0] );

  return NULL;
}

/* --------------------- */
700
static _kernel_oserror *Cmd_LMNameServer( const char *args )
701 702 703 704 705 706
{
  /* This is a *Configure/Status command handler - it's therefore unusual */
  char *argv[1];
  struct in_addr address;
  int result;

707
  if ( args == arg_CONFIGURE_SYNTAX ) /* Show 'Configure' options */
708 709 710 711
  {
    printf("LMNameServer xx.xx.xx.xx\n");
    return NULL;
  }
712
  else if ( args == arg_STATUS ) /* Show FS status */
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
  {
    int b0, b1, b2, b3;

    b0 = RdCMOS(NBNSIPCMOS0);
    b1 = RdCMOS(NBNSIPCMOS1);
    b2 = RdCMOS(NBNSIPCMOS2);
    b3 = RdCMOS(NBNSIPCMOS3);

    if ( ((b0 != 0) && (b0 != 127) && (b0 <= 223))  &&  ((b3 != 0) && (b3 != 255)) )
    {
      printf("LMNameServer %d.%d.%d.%d\n", b0,b1,b2,b3);
    }
    else
    {
      printf("LMNameServer <unset>\n");
    }
    return NULL;
  }
  else if (GetArgs(args,argv,1) == 1)
  {
    result = inet_aton(argv[0],&address);
    if (result)
    {
Stewart Brodie's avatar
Stewart Brodie committed
736
      unsigned long b0, b1, b2, b3;
737

738 739 740 741
      b0 = (unsigned long)(address.s_addr & 0x000000ff);
      b1 = (unsigned long)(address.s_addr & 0x0000ff00) >>  8;
      b2 = (unsigned long)(address.s_addr & 0x00ff0000) >> 16;
      b3 = (unsigned long)(address.s_addr & 0xff000000) >> 24;
742 743 744

      if ( ((b0 != 127) && (b0 <= 223))  &&  ((b3 != 255)) )
      {
Stewart Brodie's avatar
Stewart Brodie committed
745 746 747 748
        WrCMOS(NBNSIPCMOS0,(int) b0);
        WrCMOS(NBNSIPCMOS1,(int) b1);
        WrCMOS(NBNSIPCMOS2,(int) b2);
        WrCMOS(NBNSIPCMOS3,(int) b3);
749 750 751 752 753 754 755 756 757
        return NULL;
      }
    }
  }
  return Xlt_Error(EBADPARAM);
}

/* --------------------- */

Stewart Brodie's avatar
Stewart Brodie committed
758
typedef _kernel_oserror * (*CommandFnPtr) ( const char *args );
759

760 761
#define CmdEntry(X) [CMD_##X] = Cmd_##X
static CommandFnPtr Cmd_Dispatch[] =
762
{
763 764 765 766 767 768 769 770 771 772 773 774 775 776
  CmdEntry(LanMan),
  CmdEntry(LMConnect),
  CmdEntry(LMDisconnect),
  CmdEntry(LMLogon),
  CmdEntry(LMInfo),
  CmdEntry(LMNameMode),
  CmdEntry(LMLogoff),
  CmdEntry(LMServer),
  CmdEntry(LMPrinters),
  CmdEntry(FS),
  CmdEntry(LMTransport),
  CmdEntry(LMNameServer),
  CmdEntry(Free),
  CmdEntry(ListFS)
777 778
};

779
#define MAX_CMDS (sizeof Cmd_Dispatch / sizeof Cmd_Dispatch[0])
Stewart Brodie's avatar
Stewart Brodie committed
780

781 782
/* --------------------- */

Stewart Brodie's avatar
Stewart Brodie committed
783
_kernel_oserror *LM_Command ( const char *args, int argc, int cmd_no, void *pw )
784 785 786 787 788 789 790 791 792 793 794 795
{
  (void) pw;
  (void) argc;

  if ( cmd_no < 0 || cmd_no >= MAX_CMDS )
    return Xlt_Error(ENOTPRESENT);

  return Cmd_Dispatch[cmd_no](args);
}

/* Service call handler --------------------------------- */
static void LM_check_driver_status(_kernel_swi_regs *r);
Stewart Brodie's avatar
Stewart Brodie committed
796
static void LM_check_internet_status(_kernel_swi_regs *r);
797
static void LM_check_protocol_status(_kernel_swi_regs *r);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

void LM_Service ( int service_number, _kernel_swi_regs *r, void *pw )
{
  (void) pw;
  /* Any changes to list of service calls?  Must mirror in cmhg inputfile too! */

  switch(service_number)
  {
    case Service_FSRedeclare:
      LM_Declare();
      break;

    case Service_OmniAction:
      Omni_ServiceCall (r);
      break;

    case Service_ResourceFSStarting:
      OmniS_ResFSStarting(r->r[2], r->r[3]);
      break;

    case Service_DCIDriverStatus:
819
      LM_check_driver_status(r);
Stewart Brodie's avatar
Stewart Brodie committed
820 821
      break;

822 823 824 825
    case Service_DCIProtocolStatus:
      LM_check_protocol_status(r);
      break;

Stewart Brodie's avatar
Stewart Brodie committed
826 827 828 829
    case Service_InternetStatus:
      if (NB_InitedTransport == NB_NBIP_Setup) {
          LM_check_internet_status(r);
      }
830 831 832 833 834 835 836 837 838 839 840 841
      break;

    default:
      break;
  }
}

/* SWI handler ------------------------------------------ */

_kernel_oserror *LM_Swi( int swi_ofs, _kernel_swi_regs *r, void *pw )
{
  (void) pw;  /* Not used */
Stewart Brodie's avatar
Stewart Brodie committed
842
  switch ( swi_ofs )
843
  {
Stewart Brodie's avatar
Stewart Brodie committed
844
    case LanMan_OmniOp - LanMan_00:
845 846
      return OmniOp_SWI(r);

Stewart Brodie's avatar
Stewart Brodie committed
847
    case LanMan_LogonOp - LanMan_00:
848 849
      return Lgn_LogonOp_SWI(r);

Stewart Brodie's avatar
Stewart Brodie committed
850
    case LanMan_FreeOp - LanMan_00:
851 852
      return Omni_FreeOp_SWI(r);

Stewart Brodie's avatar
Stewart Brodie committed
853
    case LanMan_NameOp - LanMan_00:
854 855 856 857
      return Xlt_Error(RPC_NameOp(r->r[0], /* reason code */
                                  (char *)(r->r[1]), /* name in */
                                  (char *)(r->r[2])  /* buffer out */
                                   ));
Stewart Brodie's avatar
Stewart Brodie committed
858
    case LanMan_Transact - LanMan_00:
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
      return Xlt_Error(RPC_Transact((char *)(r->r[0]), /* server */
                                    (char *)(r->r[1]), /* share name */
                                    (void *)(r->r[2]) /* params */
                                     ));

    default:
      return Xlt_Error(ENOTPRESENT);
  }

  return NULL;
}

/* Initialisation code ---------------------------------- */

static char *copy_space ( char *dst, char *src, int maxlen )
{
  while ( *src > ' ' )
  {
    if ( --maxlen > 0 )
      *dst++ = *src;
    src++;
  }

  *dst = 0;
  return src;
}

/* ------------------------------- */

Stewart Brodie's avatar
Stewart Brodie committed
888
static err_t ProcessCmdLine ( const char *_line )
889 890
{
  char cli[256];
Stewart Brodie's avatar
Stewart Brodie committed
891 892
  char *end;
  char *line;
893 894 895 896 897 898

  /* This code relies on NB_xxx_Setup() only setting variables, not
     allocating anything. Multiple NB_xxx_Setup()'s may be called if
     the user is being perverse */

  /* NAS 20/3/97 - RO doesn't pass a 0-terminated string, but a ctrl-terminated one */
Stewart Brodie's avatar
Stewart Brodie committed
899
  strncpy(cli, _line, 255);
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
  line = end = cli;
  while (*end >= ' ') end++;
  *end = '\0';

  while ((line = strchr(line, '-')) != NULL)
  {
    line++;
    switch ( toupper(*line) )
    {
      case 'D':    /* Specify driver name ************ */
         line = copy_space ( LM_Vars.drivername, line+1, NAME_LIMIT );
         break;

      case 'H':
      case '?':
Ben Avison's avatar
Ben Avison committed
915
         printf( "SMB client for RISCOS, version " Module_FullVersion
916 917 918 919 920
                 "\nWritten by Ian Harvey 1994-1996\nUse:\n"
                 "\t-dea0\tto use 'ea0' for network driver (etc)\n"
                 "\t-n\tto disable network browsing\n"
                 "\t-mMYNAME\tto set machine name to MYNAME\n"
                 "\t-i\tto use NetBIOS-over-IP transport\n"
921
#ifndef NO_NETBEUI
922
                 "\t-t\tto use NetBEUI transport\n"
923
#endif
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
                 "\t-v\tto show progress information on startup\n"
                 "\t-?\tto display this help\n" );
         return ENOTINSTALLED;

      case 'V':
         LM_Vars.verbose = true;
         break;

      case 'N':
         Lgn_Logoff();
         break;

      case 'M':
         line = copy_space( LM_Vars.machinename, line+1, NAME_LIMIT );
         strcpyn_upper( LM_Vars.machinename,LM_Vars.machinename,NAME_LIMIT );
         break;

      case 'I':
Stewart Brodie's avatar
Stewart Brodie committed
942
         LanMan_InitTransport(NB_NBIP_Setup);
943 944
         break;

945
#ifndef NO_NETBEUI
946
      case 'T':
Stewart Brodie's avatar
Stewart Brodie committed
947
         LanMan_InitTransport(NB_NetBEUI_Setup);
948
         break;
949
#endif
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

      default:
         printf("\nCommand line switch '-%c' not recognised\n", line[0] );
         printf("Use '-?' to show available options\n");
         return ECMDLINE;

    }

  }

  return OK;
}


/* ------------------------------- */

void LM_Boot(void)
{
Stewart Brodie's avatar
Stewart Brodie committed
968
  int tries=1;
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  int tmp;
  err_t res;
  char *serv_name;
  char fs_name [ FSERVER_LEN+1 ];

  if (LM_Vars.initialised < LMInitState_FullyInited) {
  if (LM_Vars.verbose) {
          printf("LanManFS: LM_Boot called, but cannot comply: %s\n", LM_Status());
  }
    LM_Vars.initialised = LMInitState_Boot;
    return;
  }

  GetFSName(fs_name);

  if ( strlen(fs_name) == 0 )
  {
Stewart Brodie's avatar
Stewart Brodie committed
986 987 988
    if (LM_Vars.verbose) {
      printf("Auto-boot: server name has not been set...\n");
    }
989 990 991 992 993
    return;
  }

  /* Ready to go... */

Stewart Brodie's avatar
Stewart Brodie committed
994 995 996 997
  for (;;) {
    serv_name = RPC_GetDomainController(fs_name);
    if ( serv_name == NULL )
      serv_name = fs_name;
998

Stewart Brodie's avatar
Stewart Brodie committed
999 1000
    if ( LM_Vars.verbose )
      printf("  Server name '%s'\n", serv_name );
1001

Stewart Brodie's avatar
Stewart Brodie committed
1002
    /* Wash'n'go... */
1003

Stewart Brodie's avatar
Stewart Brodie committed
1004 1005 1006 1007 1008
    res =  Omni_MountServer ( serv_name,  /* Server */
            "ARMBOOT", "", /* User name & password */
            "BOOT",        /* Mountname */
            "ARMBOOT",     /* Drive name */
            &tmp );          /* MountID_out */
1009

Stewart Brodie's avatar
Stewart Brodie committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    if ( res != OK )
    {
      printf("  Error during boot: %s\n", Xlt_Error(res)->errmess);
      /* tmp will be zero unless the share was successfully created */
      if (tmp == 0)
      {
        char var_name[sizeof("Inet$ServerName12")];
        sprintf(var_name, "Inet$ServerName%d", ++tries);
        if (!_kernel_getenv(var_name, fs_name, FSERVER_LEN))
          continue; /* Try next server */
      }
    }
    break;
1023 1024 1025
  }
}

Stewart Brodie's avatar
Stewart Brodie committed
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
/* ------------------------------- */


/* SNB 980224.
 *
 *
 *  This function has been split into two as it is inappropriate for some
 *  parts of this code to be executed during a service call.  The initialised
 *  member of LM_Vars has been promoted to 'int' type so that it can hold any
 *  of the values in the anonymous enum at the top of this file.  This is used
 *  to control the execution of the LM_init_phase_2 function.
 *
 *
 */

Stewart Brodie's avatar
Stewart Brodie committed
1042
_kernel_oserror *LM_Initialise(const char *cmd_tail, int pod_base, void *pw)
1043 1044 1045 1046
{
  _kernel_oserror *err;
  _kernel_swi_regs R;

Stewart Brodie's avatar
Stewart Brodie committed
1047 1048 1049 1050
  debug_initialise("LanManFS", "", "");
  debug_atexit();
  debug_set_options(0,0,0);
  debug_output_device(DEBUGIT_OUTPUT);
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
  LM_Vars.initialised = LMInitState_Uninitialised;

  (void)pod_base;
  if (pw != NULL)
  {
    LM_pw = (int)pw;
  }

  SetDefaultVars();

  R.r[0] = 129;
  R.r[1] = 0;
  R.r[2] = 255;
  _kernel_swi(OS_Byte, &R, &R);

  if (R.r[1] < 0xA2)
    return Xlt_Error(ERISCOSVER);

Stewart Brodie's avatar
Stewart Brodie committed
1069
if (cmd_tail != NULL)
1070 1071 1072 1073 1074 1075 1076
  {
    err = Xlt_Error(ProcessCmdLine(cmd_tail));
    if (err != NULL)
      return err;
  }

  err = LM_Declare();
1077

1078 1079 1080 1081
  if (err != NULL)
      return err;

  LM_Vars.initialised = LMInitState_PreInit;
1082

1083
  (void) LM_init_phase_2();
1084 1085 1086

#ifndef NO_NETBEUI
  /* Cheap way to set a callback ;-) Send service call announcing ourselves */
1087
  if (NB_InitedTransport == NB_NetBEUI_Setup) {
1088 1089 1090 1091 1092
    callevery_handler(&R, pw);
    callbackflag = 2;
  }
#endif

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
  return NULL;
}

static _kernel_oserror *LM_init_phase_2(void)
{
  /* Initialise various modules */

  enum { MAXEXITS = 4 };
  pfnShutdown shutdowns[MAXEXITS];
  int ctr = 0;
  int want_boot;
  int oldstate = LM_Vars.initialised;
  _kernel_oserror *err;


  want_boot = (LM_Vars.initialised == LMInitState_Boot);

  if (LM_Vars.verbose)
    printf("Initialising...\n");

  if (!FS_Init() || !SMB_Init()) goto initfailed;
  shutdowns[ctr++] = (pfnShutdown) SMB_Shutdown;
  if (!RPC_Init() || !Prn_Init()) goto initfailed;
  shutdowns[ctr++] = (pfnShutdown) Prn_Shutdown;
  if (!Stat_Init()) goto initfailed;

  err = Xlt_Error(Buf_Init());
  if (err != NULL) goto abort;

  shutdowns[ctr++] = (pfnShutdown) Buf_Shutdown;

  /* Try to start various modules */

  if (LM_Vars.verbose)
    printf("Starting network transport...\n");

  err = Xlt_Error( NB_Startup() );
  if (err != NULL)
  {
    debug2(    "Error - NB_Startup() returned %d (%s)\n",err->errnum,err->errmess);
    goto abort;
  }

#if 0
  if (LM_Vars.verbose)
    printf( "Starting filing system...\n");

  err = LM_Declare();
  if (err != NULL) {
          if (LM_Vars.verbose) printf("LanManFS: %s\n", err->errmess);
          NB_Shutdown();
          goto abort;
  }
#endif

Stewart Brodie's avatar
Stewart Brodie committed
1148
  startcallbacks();
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

  /* We have now managed to fully initialise everything that we need - whee! */
  LM_Vars.initialised = LMInitState_FullyInited;
  ctr=0; /* Forget destructors */

  Omni_StartUp();  /* Try to contact OmniFiler */

  OmniS_ResourceInit();

  _kernel_oscli("IconSprites Resources:$.ThirdParty.OmniClient.LanMan.Sprites");

  /*LM_StartupBoot();*/
  if (want_boot) {
Ben Avison's avatar
Ben Avison committed
1162
          if (LM_Vars.verbose) printf("Booting...\n");
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
          LM_Boot();
  }

  if ( LM_Vars.verbose )
    printf("All done...\n");

  return NULL;

initfailed:
  err = Xlt_Error(EINITFAILED);
#ifdef DEBUG
  if (LM_Vars.verbose) {
          printf("LanManFS: %s (%s)\n", err->errmess, LM_Status());
  }
#endif
  while (--ctr >= 0) (shutdowns[ctr])();
  return err;

abort:
  LM_Vars.initialised = oldstate;
  while (--ctr >= 0) (shutdowns[ctr])();
#ifdef DEBUG
  if (LM_Vars.verbose) {
          printf("LanManFS: %s\n", LM_Status());
  }
#endif
  return NULL;

/* abort used to use this, but that assumes deterministic module startup,   *
 * which can't be assumed under DCI4, so now it just sits around waiting    *
 * for an appropriate set of service calls to wake it up.  RCE 980212       *

abort:
  LM_Finalise();
  return err;
 */
}

/* SNB 980224 added this function to verify the actual contents of the
 * Service_DCIDriverStatus service call
 */
static void LM_check_driver_status(_kernel_swi_regs *r)
{
        char if_name[16];
Stewart Brodie's avatar
Stewart Brodie committed
1207
        Dib *dib = (Dib *) (r->r[0]);
1208 1209 1210

        sprintf(if_name, "%-.8s%d", dib->dib_name, dib->dib_unit);

Stewart Brodie's avatar
Stewart Brodie committed
1211 1212
#ifdef TRACE
        printf ("Service_DCIDriverStatus called (interface %s%d is %sing)\n"
1213 1214 1215 1216 1217
        	"  Driver supports DCI version %d.%02d\n",
        	dib->dib_name, dib->dib_unit, r->r[2] ? "dy" : "start",
		r->r[3] / 100, r->r[3] % 100
                );

Stewart Brodie's avatar
Stewart Brodie committed
1218
        printf ("We are looking for driver `%s' - got `%s'\n", LM_Vars.drivername,
1219
        	if_name);
1220

Stewart Brodie's avatar
Stewart Brodie committed
1221
#endif
1222 1223 1224 1225 1226 1227
        if (strcmp(LM_Vars.drivername, DEFAULT_ETHER_TYPE) == 0) {
                if (if_name[0] != 'l') {
                	(void) strncpy(LM_Vars.drivername, if_name, NAME_LIMIT);
                }
        }

1228
#ifndef NO_NETBEUI
1229 1230 1231 1232
        if (NB_InitedTransport != NB_NetBEUI_Setup) {
                return;
        }

1233 1234 1235 1236 1237 1238

        if (stricmp(LM_Vars.drivername, if_name) != 0) {
                /* printf("Not interested\n"); */
                return;
        }
        else {
Stewart Brodie's avatar
Stewart Brodie committed
1239
                debug0("Ding!  This was our interface\n");
1240
		if (r->r[2] == 0) {
1241
		        /*if (LM_Vars.initialised ==  LMInitState_FullyInited)*/ {
Stewart Brodie's avatar
Stewart Brodie committed
1242
                        	LM_GracefulClosedown();
1243 1244 1245 1246
		        }
	                LM_init_phase_2();
		}
		else if (r->r[2] == 1) {
Stewart Brodie's avatar
Stewart Brodie committed
1247
		        LM_GracefulClosedown();
1248 1249
		}
        }
1250 1251 1252 1253 1254 1255 1256 1257 1258
#endif
}

static void LM_check_protocol_status(_kernel_swi_regs *r)
{
  /* A protocol module came or went (could be us in NetBEUI mode!)
   * Inform our transport layer if we find it was Internet leaving.
   */
  const char *proto = (char *) r->r[4];
1259

1260 1261 1262
  if (r->r[2] == protocolstatus_TERMINATING && strcmp(proto, "Internet") == 0) {
    NB_InternetGone();
  }
1263 1264 1265
  else if (r->r[2] == protocolstatus_STARTING && strcmp(proto, "Internet") == 0) {
    NB_InternetInit();
  }
1266 1267
}

1268

Stewart Brodie's avatar
Stewart Brodie committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
#ifdef TRACE

static void DumpBuffer(void *ptr, int len)
{
        static char db[512];
        char *p = db;

        const char *membuf = ptr;
        int i,j;
	db[0] = '\0';
        for (i=0; i<((len+31)&~31); ++i) {
                if (!(i & 31)) {
                        p += sprintf (p, "  ");
                        if (i) for (j = i - 32; j != i; ++j) {
                                p+=sprintf(p, "%c", (membuf[j]>=32 && membuf[j] != 0x7f) ?
                                membuf[j] : '.');
                        }
                        printf("$%s\n", db);
                        p=db+sprintf(db,"%04x: ", i);
                }
                if (i>=len) {
                        p+=sprintf (p,"  ");
                        if (3==(i & 3)) p+=sprintf (p," ");
                }
                else {
                        p+=sprintf (p,"%02x", membuf[i]);
                        if (3==(i & 3)) p+=sprintf (p," ");
                }
        }
        if (i) for ( p+=sprintf (p,"  "), j = i - 32; j != i; ++j) p+=sprintf (p,"%c",
            j>=len ? ' ' : (membuf[j]>=32 && membuf[j] != 0x7f) ?
            membuf[j] : '.');
        printf("$%s\n", db);
}
#endif

/* SNB 981029 added this function to verify the contents of the Service_InternetStatus
 * service call to determine whether we were waiting for the interface to go up so
 * that we could boot
 */
static void LM_check_internet_status(_kernel_swi_regs *r)
{
        char *cp = (char *) r->r[3];

	if (r->r[0] == InternetStatus_AddressChanged) {
	  if (LM_Vars.initialised != LMInitState_PreInit &&
	      LM_Vars.initialised != LMInitState_Boot) {
            return;
	  }
	  /* An interface address was set - almost certainly ours! */
	}
#ifdef TRACE
	else if (r->r[0] == InternetStatus_DynamicBootReply) {
	  DumpBuffer((void *)r->r[4], r->r[5]);
	}
	else if (r->r[0] == InternetStatus_DynamicBootStart) {
	  DumpBuffer((void *)r->r[4], r->r[5]);
	}
#endif
        else {
          /* Were we actually waiting for the driver to appear */
    	  if (LM_Vars.initialised != LMInitState_PreInit &&
              LM_Vars.initialised != LMInitState_Boot) return;
     	  /* Was it the right reason code? */
	  if (r->r[0] != InternetStatus_InterfaceUpDown) return;
	  /* Was it the right interface? */
	  if (stricmp(LM_Vars.drivername, cp) != 0) return;
	  /* was the interface coming up? */
	  if (r->r[2] != 1) return;
        }

1340
	/*if (LM_Vars.initialised == LMInitState_FullyInited)*/ {
Stewart Brodie's avatar
Stewart Brodie committed
1341 1342 1343 1344 1345 1346
	        /* Should never happen nowadays ?? */
	        LM_GracefulClosedown();
	}
	LM_init_phase_2();
}

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

char *LM_Status(void)
{
        switch (LM_Vars.initialised) {
        case LMInitState_Uninitialised: return "Dormant";
        case LMInitState_PreInit: return "Waiting for driver";
        case LMInitState_FullyInited: return "Active";
        case LMInitState_Boot: return "Waiting to boot";
        default: return "Dead";
        }
}
Stewart Brodie's avatar
Stewart Brodie committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423

#ifdef TRACE
/* Debugging routine.  If str is NULL, then the log is printed to 'f' (or stderr
 * if f was NULL).  If f is NULL, the message is added to the log but not displayed
 * on the screen.  If neither f nor str was NULL, the screen characteristcs are set
 * to useful things (text window is reset, default colours restored and text origin
 * restored to 0,0, and the message is printed to the screen and copied to tne log.
 * A monotonic coumnter is incremented for each debug message logged.  Timestamps
 * are stored in the log buffer too.
 */
#include <stdarg.h>
int module_printf(const char *str, ...)
{
	static int callcount = 0;
	static char pbuffer[65536];
	static int pbufferptr = 0;
	va_list ap;
	int inblock[2];
	FILE *f;

	f=stderr;

	if (f != NULL || str == NULL)  {
		inblock[1] = -1;
		inblock[0] = 132;
		_swix(OS_ReadVduVariables, _INR(0,1), inblock, inblock);
		_swix(OS_WriteC, _IN(0), 6);
		if (inblock[0] > 20) {
			int i;
			for (i=0; i<0; ++i) (void) _kernel_osbyte(19,0,0);
			_swix(OS_WriteN, _INR(0,1), "\x1a\x0c", 2);
		}
		_swix(OS_WriteC, _IN(0), 20);
	}

	if (!str) {
		if (pbufferptr > 0) fprintf(f?f:stderr, "%s\n", pbuffer);
		return 0;
	}

	++callcount;
	if (*str != '$' && f != NULL) {
		va_start(ap, str);
		vfprintf(f, str, ap);
		va_end(ap);
	}
	va_start(ap, str);
	_swix(OS_ReadMonotonicTime, _OUT(0), inblock);
	if (*str == '$') ++str; else
	pbufferptr += sprintf(pbuffer + pbufferptr, "(%8d) (%4d) ", inblock[0], callcount);
	pbufferptr += vsprintf(pbuffer + pbufferptr, str, ap);
	if (pbufferptr > 60000) pbufferptr = 0;
	return 0;
}
#endif


/* ========== Callback code ========== */

/* Callback management.  Some servers tend to idle-out connections.  This code stops
 * that happening.
 */

_kernel_oserror *callback_handler(_kernel_swi_regs *r, void *pw)
{
	(void) r;
1424 1425 1426 1427 1428 1429 1430
	switch (callbackflag) {
	  case 2:
                /* Re-entrancy on this code is OK - announce we are starting */
                callbackflag = 0;
                lanman_announce_lanmanfs(protocolstatus_STARTING, pw);
                break;
	  case 1:
Stewart Brodie's avatar
Stewart Brodie committed
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	        debug0("Anti IdleOut Callback entered\n");
	        SMB_AntiIdle();
		callbackflag = 0;
	}
	return 0;
}

static void clearcallback(void)
{
	if (callbackflag != 0) {
		(void) _swix(OS_RemoveCallBack, _INR(0,1), callback_entry, LM_pw);
		callbackflag = 0;
	}
}

_kernel_oserror *callevery_handler(_kernel_swi_regs *r, void *pw)
{
        (void) r;
	if (callbackflag == 0) {
		if (_swix(OS_AddCallBack, _INR(0,1), callback_entry, pw) == NULL) {
			callbackflag = 1;
			debug0(Module_Title ": (setcallback) just set a callback\n");
		}
	}
	return 0;
}

static void startcallbacks(void)
{
        /* Remember that as the SMB_AntiIdle routine rotates through all the shares,
         * each share will only be every pinged MAX_SHARES * 100 * 45 seconds.
         */
1463
        (void) _swix(OS_CallEvery, _INR(0,2), (100*45) - 1, callevery_entry, LM_pw);
Stewart Brodie's avatar
Stewart Brodie committed
1464 1465 1466 1467 1468 1469 1470
}

static void stopcallbacks(void)
{
        (void) _swix(OS_RemoveTickerEvent, _INR(0,1), callevery_entry, LM_pw);
        clearcallback();
}