home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d145 / dnet.lha / Dnet / unix / lib / dnetlib.c
C/C++ Source or Header  |  1988-05-26  |  4KB  |  251 lines

  1.  
  2. /*
  3.  *  DNETLIB.C
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  Library Interface for DNET.
  8.  */
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <fcntl.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #ifdef O_CREAT
  17. #include <sys/file.h>
  18. #endif
  19.  
  20. extern char *getenv();
  21.  
  22. typedef unsigned short uword;
  23. typedef unsigned long ulong;
  24. typedef unsigned char ubyte;
  25. typedef struct sockaddr SOCKADDR;
  26.  
  27. typedef struct {
  28.     int s;
  29.     uword port;
  30. } CHANN;
  31.  
  32. #define NAMELEN sizeof(".PORT.XXXXX")
  33. #define NAMEPAT "%s.PORT.%ld"
  34.  
  35. CHANN *
  36. DListen(port)
  37. uword port;
  38. {
  39.     CHANN *chan;
  40.     int s;
  41.     SOCKADDR *sa = (SOCKADDR *)malloc(sizeof(SOCKADDR)+256);
  42.     char *dirstr = getenv("DNETDIR") ? getenv("DNETDIR") : "";
  43.  
  44.     sprintf(sa->sa_data, NAMEPAT, dirstr, port);
  45.     sa->sa_family = AF_UNIX;
  46.     unlink(sa->sa_data);
  47.  
  48.     s = socket(PF_UNIX, SOCK_STREAM, 0);
  49.     fcntl(s, F_SETOWN, getpid());
  50.     if (bind(s, sa, sizeof(*sa)-sizeof(sa->sa_data)+strlen(sa->sa_data)) < 0) {
  51.     close(s);
  52.     free(sa);
  53.     return(NULL);
  54.     }
  55.     if (listen(s, 5) < 0) {
  56.         close(s);
  57.         unlink(sa->sa_data);
  58.     free(sa);
  59.         return(NULL);
  60.     }
  61.     chan = (CHANN *)malloc(sizeof(CHANN));
  62.     chan->s = s;
  63.     chan->port = port;
  64.     free(sa);
  65.     return(chan);
  66. }
  67.  
  68.  
  69. DUnListen(chan)
  70. CHANN *chan;
  71. {
  72.     char buf[32];
  73.  
  74.     close(chan->s);
  75.     sprintf(buf, NAMEPAT, chan->port);
  76.     unlink(buf);
  77.     free(chan);
  78. }
  79.  
  80. DAccept(chan)
  81. CHANN *chan;
  82. {
  83.     SOCKADDR sa;
  84.     int addrlen = sizeof(sa);
  85.     int fd;
  86.  
  87.     fd = accept(chan->s, &sa, &addrlen);
  88.     return(fd);
  89. }
  90.  
  91. DOpen(host, port, txpri, rxpri)
  92. char *host;
  93. uword port;
  94. char txpri, rxpri;
  95. {
  96.     int s;
  97.     char rc;
  98.     short xb[3];
  99.     SOCKADDR *sa = (SOCKADDR *)malloc(sizeof(SOCKADDR)+256);
  100.     char *dirstr = getenv("DNETDIR") ? getenv("DNETDIR") : "";
  101.  
  102.     if (rxpri < -127)
  103.     rxpri = -127;
  104.     if (rxpri > 126)
  105.     rxpri = 126;
  106.     if (txpri < -127)
  107.     txpri = -127;
  108.     if (txpri > 126)
  109.     txpri = 126;
  110.  
  111.     if (host == NULL)
  112.     host = (getenv("DNETHOST")) ? getenv("DNETHOST"):"";
  113.  
  114.     sa->sa_family = AF_INET;
  115.     sprintf(sa->sa_data, "%s%s%s", dirstr, "DNET.", host);
  116.  
  117.     s = socket(PF_UNIX, SOCK_STREAM, 0);
  118.     fcntl(s, F_SETOWN, getpid());
  119.     if (connect(s, sa, sizeof(*sa)-sizeof(sa->sa_data)+
  120.     strlen(sa->sa_data))<0) {
  121.         close(s);
  122.         free(sa);
  123.         return(-1);
  124.     }
  125.     free(sa);
  126.     xb[0] = port;
  127.     ((char *)&xb[1])[0] = txpri;
  128.     ((char *)&xb[1])[1] = rxpri;
  129.     write(s, xb, 4);
  130.     if (read(s, &rc, 1) == 1 && rc == 0)
  131.         return(s);
  132.     close(s);
  133.     return(-1);
  134. }
  135.  
  136. DEof(fd)
  137. {
  138.     char dummy;
  139.  
  140.     shutdown(fd, 1);
  141.     write(fd, &dummy, 0);
  142. }
  143.  
  144. gwrite(fd, buf, bytes)
  145. char *buf;
  146. {
  147.     int n;
  148.     int orig = bytes;
  149.     extern int errno;
  150.     while (bytes) {
  151.     n = write(fd, buf, bytes);
  152.     if (n > 0) {
  153.         bytes -= n;
  154.         buf += n;
  155.         continue;
  156.     }
  157.     if (n < 0) {
  158.         if (errno == EINTR)
  159.         continue;
  160.         if (errno == EWOULDBLOCK) {
  161.         int wm = 1 << fd;
  162.         int em = 1 << fd;
  163.         if (select(fd+1, NULL, &wm, &em, NULL) < 0)
  164.             continue;
  165.         if (wm)
  166.             continue;
  167.         }
  168.         return(orig - bytes);
  169.     }
  170.     }
  171.     return(orig);
  172. }
  173.  
  174. gread(fd, buf, bytes)
  175. char *buf;
  176. {
  177.     int n;
  178.     int orig = bytes;
  179.     extern int errno;
  180.     while (bytes) {
  181.     n = read(fd, buf, bytes);
  182.     if (n > 0) {
  183.         bytes -= n;
  184.         buf += n;
  185.         break;
  186.     }
  187.     if (n < 0) {
  188.         if (errno == EINTR)
  189.         continue;
  190.         if (errno == EWOULDBLOCK) {
  191.         int rm = 1 << fd;
  192.         int em = 1 << fd;
  193.         if (select(fd+1, &rm, NULL, &em, NULL) < 0)
  194.             continue;
  195.         if (rm)
  196.             continue;
  197.         }
  198.         return(orig - bytes);
  199.     }
  200.     if (n == 0)
  201.         break;
  202.     }
  203.     return(orig - bytes);
  204. }
  205.  
  206. ggread(fd, buf, bytes)
  207. char *buf;
  208. {
  209.     int n;
  210.     int ttl = 0;
  211.     while (bytes) {
  212.     n = gread(fd, buf, bytes);
  213.     if (n > 0) {
  214.         bytes -= n;
  215.         buf += n;
  216.         ttl += n;
  217.         continue;
  218.     }
  219.     return(-1);
  220.     }
  221.     return(ttl);
  222. }
  223.  
  224. /*
  225.  *    Convert to and from 68000 longword format.  Of course, it really
  226.  *    doesn't matter what format you use, just as long as it is defined.
  227.  */
  228.  
  229. ntohl68(n)
  230. ulong n;
  231. {
  232.     return(
  233.     (((ubyte *)&n)[0] << 24)|
  234.     (((ubyte *)&n)[1] << 16)|
  235.     (((ubyte *)&n)[2] << 8)|
  236.     (((ubyte *)&n)[3])
  237.     );
  238. }
  239.  
  240. htonl68(n)
  241. ulong n;
  242. {
  243.     ulong v;
  244.     ((ubyte *)&v)[0] = n >> 24;
  245.     ((ubyte *)&v)[1] = n >> 16;
  246.     ((ubyte *)&v)[2] = n >> 8;
  247.     ((ubyte *)&v)[3] = n;
  248.     return(v);
  249. }
  250.  
  251.