home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / NETBIOS.C < prev    next >
Text File  |  1995-04-20  |  24KB  |  480 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   netbios.c                                                               */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Netbios management functions.                                            */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   07/15/94 Created.                                                       */
  12. /*                                                                           */
  13. /*****************************************************************************/
  14.  
  15. #include "all.h"
  16.  
  17. /*****************************************************************************/
  18. /* NetBiosInit()                                                             */
  19. /*                                                                           */
  20. /* Description:                                                              */
  21. /*                                                                           */
  22. /*   Set up netbios session with a remote probe.                             */
  23. /*                                                                           */
  24. /* Parameters:                                                               */
  25. /*                                                                           */
  26. /*   pConnection    -> to the structure defining the remote connection.      */
  27. /*   pLsnHandle     where to stuff the session handle for the caller.        */
  28. /*   pNB_MoreRcInfo ->holder of additional netbios info.                     */
  29. /*                                                                           */
  30. /* Return:                                                                   */
  31. /*                                                                           */
  32. /*   rc            a netbios return code.                                    */
  33. /*                                                                           */
  34. /* Assumptions:                                                              */
  35. /*                                                                           */
  36. /*   The following session names are agreed upon by the dbg and the esp:     */
  37. /*                                                                           */
  38. /*    esp?          -> name on the esp side of the connection.               */
  39. /*    sd3?          -> name of the dbg side of the connection.               */
  40. /*                                                                           */
  41. /*   where ? is a 13 character name extracted from the /n invocation         */
  42. /*           option.                                                         */
  43. /*                                                                           */
  44. /*****************************************************************************/
  45. static  unsigned ( * _Far16 _Pascal NetBios)(char *) = NULL;
  46.  
  47. APIRET  NetBiosInit( CONNECTION *pConnection,
  48.                         LHANDLE *pLsnHandle,
  49.                             int *pNB_MoreRcInfo )
  50. {
  51.  APIRET  rc;
  52.  BYTE    sess;
  53.  BYTE    ncbs;
  54.  BYTE    names;
  55.  BYTE    RequestOrRelease;
  56.  char    LsnDbgName[MAX_LSN_NAME];         /* max length allowed by netbios. */
  57.  char    LsnEspName[MAX_LSN_NAME];
  58.  int     DbgOrEsp;
  59.  char    buf[CCHMAXPATH];
  60.  ULONG   mte = 0;
  61.  
  62.  /****************************************************************************/
  63.  /* - Load the Netbios entry point.                                          */
  64.  /* - We will assume that if we can load it that we will be able to get      */
  65.  /*   the procedure address.                                                 */
  66.  /****************************************************************************/
  67.  if( NetBios == NULL )
  68.  {
  69.   rc = DosLoadModule( buf, sizeof(buf), "ACSNETB", &mte );
  70.   if(rc)
  71.   {
  72.    *pNB_MoreRcInfo = CANT_LOAD_NETB_DLL;
  73.    return(rc);
  74.   }
  75.  
  76.   DosQueryProcAddr(mte, 0, "NETBIOS",(PFN*)&NetBios);
  77.  }
  78.  
  79.  DbgOrEsp = pConnection->DbgOrEsp;
  80.  /****************************************************************************/
  81.  /* - grab some resources from the netbios pool.                             */
  82.  /****************************************************************************/
  83.  if( IsParent() == TRUE )
  84.  {
  85.   sess  = 2;
  86.   ncbs  = 3;
  87.   names = 1;
  88.  }
  89.  else
  90.  {
  91.   sess  = 1;
  92.   ncbs  = 2;
  93.   names = 1; /* <-- need a 1 here but I don't know why...*/
  94.              /* we will not be adding a name to the adapter table. */
  95.  }
  96.  
  97.  RequestOrRelease = REQUEST;
  98.  
  99.  rc = NetBios_Reset( sess, ncbs, names, RequestOrRelease, pNB_MoreRcInfo );
  100.  if( rc ) return(rc);
  101.  
  102.  /****************************************************************************/
  103.  /* - now build the logical session names for the dbg/esp ends of the        */
  104.  /*   connection. This connection assumes that the dbg and esp ends          */
  105.  /*   of the connection have the same logical session name as defined        */
  106.  /*   by the user. We're using an agreed upon convention.                    */
  107.  /****************************************************************************/
  108.  BuildLsnName(LsnDbgName,LSN_DBG,pConnection);
  109.  BuildLsnName(LsnEspName,LSN_ESP,pConnection);
  110.  
  111.  /****************************************************************************/
  112.  /* - tell the name to the adapter if first instance of the connection.      */
  113.  /****************************************************************************/
  114.  if( IsParent() == TRUE )
  115.  {
  116.   if( DbgOrEsp == _DBG )
  117.    rc = NetBios_AddName( LsnDbgName );
  118.   else
  119.    rc = NetBios_AddName( LsnEspName );
  120.  
  121.   if(rc) return(rc);
  122.  }
  123.  
  124.  /****************************************************************************/
  125.  /* - now, make/receive the call to/from the remote session.                 */
  126.  /****************************************************************************/
  127.  rc = NetBios_Call_Answer( DbgOrEsp,LsnDbgName,LsnEspName,pLsnHandle);
  128.  return(rc);
  129. }
  130.  
  131. /*****************************************************************************/
  132. /* NetBios_Reset()                                                           */
  133. /*                                                                           */
  134. /* Description:                                                              */
  135. /*                                                                           */
  136. /*   Requests/Releases resources from the netbios pool.                      */
  137. /*                                                                           */
  138. /* Parameters:                                                               */
  139. /*                                                                           */
  140. /*   sess                  number of sessions.                               */
  141. /*   ncbs                  number of network control blocks(NCBs).           */
  142. /*   names                 number of unique name identifiers.                */
  143. /*   RequestOrRelease      0 = request 1 = release.                          */
  144. /*   pInadequateResources  ->holder for which resource is inadequate.        */
  145. /*                                                                           */
  146. /* Return:                                                                   */
  147. /*                                                                           */
  148. /*   rc  = NCB.Reset.ncb_retcode. This is the return code from the           */
  149. /*         netbios call.                                                     */
  150. /*                                                                           */
  151. /* Assumptions:                                                              */
  152. /*                                                                           */
  153. /*  This is currently hard coded for the primary adapter.                    */
  154. /*                                                                           */
  155. /*****************************************************************************/
  156. APIRET NetBios_Reset( BYTE  sess,
  157.                       BYTE  ncbs,
  158.                       BYTE  names,
  159.                       BYTE  RequestOrRelease,
  160.                       int  *pInadequateResources)
  161. {
  162.  NCB_RESET NCB_Reset;
  163.  
  164.  memset(&NCB_Reset, 0, sizeof(NCB_Reset) );
  165.  
  166.  NCB_Reset.ncb_command  = NB_RESET_WAIT;
  167.  NCB_Reset.ncb_lana_num = PRIMARY_ADAPTER;
  168.  NCB_Reset.ncb_lsn      = RequestOrRelease;
  169.  NCB_Reset.req_sessions = sess;
  170.  NCB_Reset.req_commands = ncbs;
  171.  NCB_Reset.req_names    = names;
  172.  
  173.  (* NetBios)( (char *)&NCB_Reset );
  174.  
  175.  *pInadequateResources = 0;
  176.  
  177.  if( NCB_Reset.ncb_retcode == NB_INADEQUATE_RESOURCES )
  178.  {
  179.   if( NCB_Reset.act_sessions != sess )
  180.   {
  181.    *pInadequateResources = INADEQUATE_SESSIONS;
  182.   }
  183.   else if( NCB_Reset.act_commands != ncbs )
  184.   {
  185.    *pInadequateResources = INADEQUATE_COMMANDS;
  186.   }
  187.   else if( NCB_Reset.act_names != names )
  188.   {
  189.    *pInadequateResources = INADEQUATE_NAMES;
  190.   }
  191.  }
  192.  return(NCB_Reset.ncb_retcode);
  193. }
  194.  
  195. /*****************************************************************************/
  196. /* NetBios_AddName()                                                         */
  197. /*                                                                           */
  198. /* Description:                                                              */
  199. /*                                                                           */
  200. /*   Add a unique logical name that will identify this adapter to the        */
  201. /*   the network.                                                            */
  202. /*                                                                           */
  203. /* Parameters:                                                               */
  204. /*                                                                           */
  205. /*   pLocalName    -> to the unique name identifying the adapter.            */
  206. /*                                                                           */
  207. /* Return:                                                                   */
  208. /*                                                                           */
  209. /*   rc  = ncb.ncb_retcode. This is the return code from the                 */
  210. /*         netbios call.                                                     */
  211. /*                                                                           */
  212. /* Assumptions:                                                              */
  213. /*                                                                           */
  214. /*****************************************************************************/
  215. APIRET NetBios_AddName( char *pLocalName )
  216. {
  217.  NCB       ncb;
  218.  
  219.  memset(&ncb, 0, sizeof(ncb) );
  220.  
  221.  ncb.ncb_command  = NB_ADD_NAME_WAIT;
  222.  ncb.ncb_lana_num = PRIMARY_ADAPTER;
  223.  
  224.  strncpy(ncb.ncb_name,pLocalName,16);
  225.  
  226.  (* NetBios)( (char *)&ncb );
  227.  
  228.  return(ncb.ncb_retcode);
  229. }
  230.  
  231. /*****************************************************************************/
  232. /* NetBios_Call_Answer()                                                     */
  233. /*                                                                           */
  234. /* Description:                                                              */
  235. /*                                                                           */
  236. /*   Make a call to get a netbios connection to an esp. Return the           */
  237. /*   logical session number back to the caller.                              */
  238. /*                                                                           */
  239. /* Parameters:                                                               */
  240. /*                                                                           */
  241. /*   DbgOrEsp      whether we are connecting from esp or dbg.                */
  242. /*   pDbgName      -> to the unique name identifying the Dbg session.        */
  243. /*   pEspName      -> to the unique name identifying the Esp session.        */
  244. /*   pLsn          -> to the unique session number returned by netbios.      */
  245. /*                                                                           */
  246. /* Return:                                                                   */
  247. /*                                                                           */
  248. /*   rc  = NCB.Reset.ncb_retcode. This is the return code from the           */
  249. /*         netbios call.                                                     */
  250. /*                                                                           */
  251. /*   ncb.ncb_lsn = logical session number that we get back from netbios.     */
  252. /*                                                                           */
  253. /* Assumptions:                                                              */
  254. /*                                                                           */
  255. /*****************************************************************************/
  256. APIRET NetBios_Call_Answer(int      DbgOrEsp,
  257.                            char    *pDbgName,
  258.                            char    *pEspName,
  259.                            LHANDLE *pLsn)
  260. {
  261.  NCB    ncb;
  262.  
  263.  memset(&ncb, 0, sizeof(ncb) );
  264.  
  265.  ncb.ncb_lana_num = PRIMARY_ADAPTER;
  266.  ncb.ncb_rto      = 0;                  /* infinite receive timeout.         */
  267.  ncb.ncb_sto      = 0;                  /* infinite send timeout.            */
  268.  
  269.  /****************************************************************************/
  270.  /* - make/listen for the call                                               */
  271.  /* - give the handle back to the caller.                                    */
  272.  /****************************************************************************/
  273.  ncb.ncb_command  = NB_CALL_WAIT;
  274.  strncpy(ncb.ncb_name ,pDbgName, 16);
  275.  strncpy(ncb.ncb_callname, pEspName, 16);
  276.  if( DbgOrEsp == _ESP )
  277.  {
  278.   ncb.ncb_command  = NB_LISTEN_WAIT;
  279.   strncpy(ncb.ncb_name, pEspName, 16);
  280.   strncpy(ncb.ncb_callname, pDbgName, 16);
  281.  }
  282.  
  283.  /****************************************************************************/
  284.  /* - try to make the connection a number of times and then give up.         */
  285.  /****************************************************************************/
  286.  {
  287.   int retries = 5;
  288.  
  289.   ncb.ncb_retcode = NB_SES_OPEN_REJECTED;
  290.   while( (ncb.ncb_retcode == NB_SES_OPEN_REJECTED) && (retries > 0) )
  291.   {
  292.    ( * NetBios)( (char *)&ncb );
  293.    retries--;
  294.   }
  295.  }
  296.  
  297.  *pLsn = ncb.ncb_lsn;
  298.  
  299.  return(ncb.ncb_retcode);
  300. }
  301. /*****************************************************************************/
  302. /* NetBiosOriginateSession()                                                 */
  303. /*                                                                           */
  304. /* Description:                                                              */
  305. /*                                                                           */
  306. /*   This function is only used for parallel connections to set up           */
  307. /*   connection between the esp and dbg queues. The resources have           */
  308. /*   already been allocated by parent dbg/esp.                               */
  309. /*                                                                           */
  310. /* Parameters:                                                               */
  311. /*                                                                           */
  312. /*   pConnection    -> to the structure defining the remote connection.      */
  313. /*   pLsnHandle     where to stuff the session handle for the caller.        */
  314. /*                                                                           */
  315. /* Return:                                                                   */
  316. /*                                                                           */
  317. /*   rc            a netbios return code.                                    */
  318. /*                                                                           */
  319. /* Assumptions:                                                              */
  320. /*                                                                           */
  321. /*   The following session names are agreed upon by the dbg and the esp:     */
  322. /*                                                                           */
  323. /*    esp?          -> name on the esp side of the connection.               */
  324. /*    sd3?          -> name of the dbg side of the connection.               */
  325. /*                                                                           */
  326. /*   where ? is a 13 character name extracted from the /n invocation         */
  327. /*           option.                                                         */
  328. /*                                                                           */
  329. /*****************************************************************************/
  330. APIRET  NetBiosOriginateSession( CONNECTION *pConnection , LHANDLE *pLsnHandle)
  331. {
  332.  APIRET  rc;
  333.  char    LsnDbgName[MAX_LSN_NAME];         /* max length allowed by netbios. */
  334.  char    LsnEspName[MAX_LSN_NAME];
  335.  int     DbgOrEsp;
  336.  
  337.  DbgOrEsp = pConnection->DbgOrEsp;
  338.  
  339.  /****************************************************************************/
  340.  /* - now build the logical session names for the dbg/esp ends of the        */
  341.  /*   connection.  This connection assumes that the dbg and esp ends of      */
  342.  /*   the connection have the same logical session name as defined by the    */
  343.  /*   user.  We're using an agreed upon convention.                          */
  344.  /****************************************************************************/
  345.  BuildLsnName(LsnDbgName,LSN_DBG,pConnection);
  346.  BuildLsnName(LsnEspName,LSN_ESP,pConnection);
  347.  
  348.  /****************************************************************************/
  349.  /* - now, make/receive the call to/from the remote session.                 */
  350.  /****************************************************************************/
  351.  rc = NetBios_Call_Answer( DbgOrEsp,LsnDbgName,LsnEspName,pLsnHandle);
  352.  return(rc);
  353. }
  354.  
  355. /*****************************************************************************/
  356. /* - a utility routine to build the logical session names.                   */
  357. /*****************************************************************************/
  358. void BuildLsnName( char *LsnName, char *header, CONNECTION *pConnection )
  359. {
  360.  char *cp;
  361.  int   len;
  362.  
  363.  /****************************************************************************/
  364.  /* - clear the caller's buffer.                                             */
  365.  /* - put in the header.                                                     */
  366.  /* - concatenate the user specified logical session name.                   */
  367.  /****************************************************************************/
  368.  memset( LsnName,' ',MAX_LSN_NAME );
  369.  strcpy(LsnName, header);
  370.  
  371.  cp  = LsnName + strlen(LsnName);
  372.  len = MAX_LSN_NAME - strlen(LsnName);
  373.  if( strlen(pConnection->pLsnName) > len )
  374.   memcpy(cp, pConnection->pLsnName, len );
  375.  else
  376.   memcpy(cp, pConnection->pLsnName, strlen(pConnection->pLsnName) );
  377.  return;
  378. }
  379.  
  380. /*****************************************************************************/
  381. /* NetBios_Send()                                                            */
  382. /*                                                                           */
  383. /* Description:                                                              */
  384. /*                                                                           */
  385. /*   Send a netbios message buffer.                                          */
  386. /*                                                                           */
  387. /* Parameters:                                                               */
  388. /*                                                                           */
  389. /*   pBuf       -> our transmit buffer.                                      */
  390. /*   Len        number of bytes to transmit.                                 */
  391. /*   LsnHandle  handle for the connection.                                   */
  392. /*                                                                           */
  393. /* Return:                                                                   */
  394. /*                                                                           */
  395. /* Assumptions:                                                              */
  396. /*                                                                           */
  397. /*****************************************************************************/
  398. void NetBios_Send( LHANDLE LsnHandle, PVOID pBuf, ULONG Len)
  399. {
  400.  NCB ncb;
  401.  
  402.  memset(&ncb, 0, sizeof(ncb) );
  403.  
  404.  ncb.ncb_command        = NB_SEND_WAIT;
  405.  ncb.ncb_lana_num       = PRIMARY_ADAPTER;
  406.  ncb.ncb_lsn            = LsnHandle;
  407.  ncb.ncb_buffer_address = pBuf;
  408.  ncb.ncb_length         = (USHORT)Len;
  409.  
  410.  ( * NetBios)( (char *)&ncb );
  411.  
  412.  if( ncb.ncb_retcode != 0 )
  413.  {
  414.   if( ncb.ncb_retcode != NB_SESSION_CLOSED )
  415.   {
  416.    if( IsVerbose()){printf("\nNetbios send error rc=%d",ncb.ncb_retcode);fflush(0);}
  417.   }
  418.  }
  419. }
  420.  
  421. /*****************************************************************************/
  422. /* NetBios_Recv()                                                            */
  423. /*                                                                           */
  424. /* Description:                                                              */
  425. /*                                                                           */
  426. /*   Receive a netbios message buffer.                                       */
  427. /*                                                                           */
  428. /* Parameters:                                                               */
  429. /*                                                                           */
  430. /*   pBuf       -> our transmit buffer.                                      */
  431. /*   Len        number of bytes to transmit.                                 */
  432. /*   LsnHandle  handle for the connection.                                   */
  433. /*                                                                           */
  434. /* Return:                                                                   */
  435. /*                                                                           */
  436. /* Assumptions:                                                              */
  437. /*                                                                           */
  438. /*****************************************************************************/
  439. void NetBios_Recv( LHANDLE LsnHandle, PVOID pBuf, ULONG Len)
  440. {
  441.  NCB ncb;
  442.  
  443.  memset(&ncb, 0, sizeof(ncb) );
  444.  
  445.  ncb.ncb_command        = NB_RECEIVE_WAIT;
  446.  ncb.ncb_lana_num       = PRIMARY_ADAPTER;
  447.  ncb.ncb_lsn            = LsnHandle;
  448.  ncb.ncb_buffer_address = pBuf;
  449.  ncb.ncb_length         = (USHORT)Len;
  450.  
  451.  (* NetBios)( (char *)&ncb );
  452.  
  453.  if( ncb.ncb_retcode != 0 )
  454.  {
  455.   if( ncb.ncb_retcode != NB_SESSION_CLOSED )
  456.   {
  457.    if( IsVerbose() ){printf("\nNetbios receive error rc=%d",ncb.ncb_retcode);fflush(0);}
  458.   }
  459.  }
  460. }
  461.  
  462. void NetBiosClose( LHANDLE LsnHandle )
  463. {
  464.  NCB    ncb;
  465.  
  466.  /****************************************************************************/
  467.  /* - We might get a return code of NB_SESSION_CLOSED from this call if      */
  468.  /*   the other end of the connection has already hung up. This is ok.       */
  469.  /****************************************************************************/
  470.  memset(&ncb, 0, sizeof(ncb) );
  471.  
  472.  ncb.ncb_command        = NB_HANG_UP_WAIT;
  473.  ncb.ncb_lana_num       = PRIMARY_ADAPTER;
  474.  ncb.ncb_lsn            = LsnHandle;
  475.  
  476.  (* NetBios)( (char *)&ncb );
  477.  
  478.  if( IsVerbose() ) {printf("\nNetbios close rc=%d",ncb.ncb_retcode);fflush(0);}
  479. }
  480.