home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_05 / 2n05034a < prev    next >
Text File  |  1991-03-27  |  3KB  |  164 lines

  1. /*
  2.  *  File:    NetBIOS.C
  3.  *  Purpose:    Contains network communications
  4.  *        functions
  5.  */
  6.  
  7. #include    "netbios.h"
  8.  
  9.  
  10. /*  External functions  */
  11. void NetBIOS (NCB far *ncb);
  12.  
  13.  
  14. /*
  15.  * NetClearNCB : initialize a network block
  16.  *   Set all fields to binary 0.
  17.  */
  18. void NetClearNCB (NCB *ncb)
  19. {
  20.     /**  compile option -Ox causes memset to  **/    
  21.     /**  be inlined automatically.            **/
  22.     memset (ncb, 0, sizeof (NCB));
  23. }
  24.  
  25. /*
  26.  * NetName : generic add/delete name handler
  27.  *   Prepares for and calls NetBIOS to manage
  28.  *   names
  29.  */
  30. int NetName (BYTE command, char *name)
  31. {
  32.     NCB    ncb;
  33.  
  34.     NetClearNCB (&ncb);
  35.     ncb.command = command;
  36.     strcpy (ncb.name, name);
  37.     NetBIOS (&ncb);
  38.  
  39.     return ncb.status;
  40. }
  41.  
  42. /*
  43.  * NetAddname : Add a network name
  44.  */
  45. int NetAddname (char *name)
  46. {
  47.     return NetName (NB_ADDNAME, name);
  48. }
  49.  
  50. /*
  51.  * NetDelname : Delete a network name
  52.  */
  53. int NetDelname (char *name)
  54. {
  55.     return NetName (NB_DELNAME, name);
  56. }
  57.  
  58. /*
  59.  * NetHangUp : Terminate a session
  60.  */
  61. int NetHangUp (BYTE session)
  62. {
  63.     NCB    ncb;
  64.  
  65.     NetClearNCB (&ncb);
  66.     ncb.command = NB_HANGUP;
  67.     ncb.localSession = session;
  68.     NetBIOS (&ncb);
  69.  
  70.     return (ncb.status);
  71. }
  72.  
  73. /* 
  74.  * NetCall : Establish a session
  75.  *   Attempt to establish a session between names
  76.  *   local and remote.  If established th local
  77.  *   session number is set in *session.
  78.  */
  79. int NetCall (char *local, char *remote, BYTE *session)
  80. {
  81.     NCB    ncb;
  82.  
  83.     NetClearNCB (&ncb);
  84.     ncb.command = NB_CALL;
  85.     strcpy (ncb.name, local);
  86.     strcpy (ncb.callName, remote);
  87.  
  88.     /**  set no receive timeouts  **/
  89.     /**  10 second send timeouts  **/
  90.     ncb.receiveTimeOut = 0;
  91.     ncb.sendTimeOut = 0; 
  92.  
  93.     NetBIOS (&ncb);
  94.     if (!ncb.status)
  95.         *session = ncb.localSession;
  96.  
  97.     return ncb.status;
  98. }
  99.  
  100. /* 
  101.  * NetListen : Wait for a Call
  102.  *   Wait for a session request between names
  103.  *   local and remote.  If established th local
  104.  *   session number is set in *session.
  105.  */
  106. int NetListen (char *local, char *remote, BYTE *session)
  107. {
  108.     NCB    ncb;
  109.  
  110.     NetClearNCB (&ncb);
  111.     ncb.command = NB_LISTEN;
  112.     strcpy (ncb.name, local);
  113.     strcpy (ncb.callName, remote);
  114.  
  115.     /**  set no receive timeouts  **/
  116.     /**  10 second send timeouts  **/
  117.     ncb.receiveTimeOut = 0;
  118.     ncb.sendTimeOut = 20; 
  119.  
  120.     NetBIOS (&ncb);
  121.     if (!ncb.status) {
  122.         *session = ncb.localSession;
  123.         memcpy (remote, ncb.callName, 16);
  124.         remote[16] = '\0';
  125.     }
  126.  
  127.     return ncb.status;
  128. }
  129.  
  130. /*
  131.  * NetTransfer : generic send/receive handler
  132.  *   Trnasfers data over given session
  133.  */
  134. int NetTransfer (BYTE command, FPTR buf, int size,
  135.          BYTE session)
  136. {
  137.     NCB    ncb;
  138.  
  139.     NetClearNCB (&ncb);
  140.     ncb.command = command;
  141.     ncb.localSession = session;
  142.     ncb.buffer = buf;
  143.     ncb.length = size;
  144.     NetBIOS (&ncb);
  145.  
  146.     return ncb.status;
  147. }
  148.  
  149. /*
  150.  * NetSend : Send data over a session
  151.  */
  152. int NetSend (FPTR buf, int size, BYTE session)
  153. {
  154.     return NetTransfer (NB_SEND, buf, size, session);
  155. }
  156.  
  157. /*
  158.  * NetReceive : Receive data over a session
  159.  */
  160. int NetReceive (FPTR buf, int size, BYTE session)
  161. {
  162.     return NetTransfer (NB_RECV, buf, size, session);
  163. }
  164.