pslsocket.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. % File: PXK:PSLSOCKET.C
  5. % Description: Interface to *ix sockets
  6. % Author:
  7. % Created:
  8. % Modified:
  9. % Mode: Text
  10. % Package:
  11. % Status: Open Source: BSD License
  12. %
  13. % (c) Copyright 1983, Hewlett-Packard Company, see the file
  14. % HP_disclaimer at the root of the PSL file tree
  15. %
  16. % Redistribution and use in source and binary forms, with or without
  17. % modification, are permitted provided that the following conditions are met:
  18. %
  19. % * Redistributions of source code must retain the relevant copyright
  20. % notice, this list of conditions and the following disclaimer.
  21. % * Redistributions in binary form must reproduce the above copyright
  22. % notice, this list of conditions and the following disclaimer in the
  23. % documentation and/or other materials provided with the distribution.
  24. %
  25. % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. % THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. % PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
  29. % CONTRIBUTORS
  30. % BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. % POSSIBILITY OF SUCH DAMAGE.
  37. %
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39. */
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <strings.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <netdb.h>
  46. #include <netinet/in.h>
  47. #include "psl.h"
  48. /* #define PORT_NUMBER 1188 */ /* Port number to listen on.
  49. Must be the same as in client!!!! */
  50. int _(unixsocketopen)(char *name, int number)
  51. { struct hostent *host_info;
  52. struct sockaddr_in mail_addr; /* Address structure */
  53. socklen_t mail_len = sizeof(struct sockaddr_in);
  54. int port_fd, conn_fd;
  55. int mail_fd;
  56. char *getlogin();
  57. if (name == (char *) 0)
  58. { /*
  59. * SERVER: This program is the server side of the connection.
  60. * It waits for clients to come along and requests connections.
  61. */
  62. if ((port_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  63. { perror ("socket");
  64. return(-1);
  65. }
  66. mail_addr.sin_family = AF_INET; /* Setup address structure */
  67. mail_addr.sin_port = number;
  68. mail_addr.sin_addr.s_addr = INADDR_ANY;
  69. /*
  70. * Open up a socket for us to accept connections on and
  71. * bind an address to it which other systems can see.
  72. */
  73. if (bind (port_fd, (struct sockaddr *)&mail_addr, mail_len) != 0)
  74. { perror ("bind");
  75. close (port_fd);
  76. return(-1);
  77. }
  78. /*
  79. * Allow for up to 5 connection requests to be pending at one time.
  80. */
  81. if (listen (port_fd, 5) != 0)
  82. { perror ("listen");
  83. close (port_fd);
  84. return(-1);
  85. }
  86. conn_fd = accept(port_fd, (struct sockaddr *)&mail_addr, &mail_len);
  87. return(conn_fd);
  88. }
  89. else
  90. { if ((host_info = gethostbyname(name)) == NULL) /* Get inet address */
  91. { fprintf (stderr,"ERROR: Unknown host %s.\n",name);
  92. return (-1);
  93. }
  94. /*
  95. * Create a socket and then try to connect it to the server.
  96. */
  97. if ((mail_fd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
  98. { perror ("socket");
  99. return(-1);
  100. }
  101. mail_addr.sin_family = AF_INET;
  102. mail_addr.sin_port = number;
  103. bcopy (host_info->h_addr, (char *) &mail_addr.sin_addr, host_info->h_length);
  104. if (connect (mail_fd, (struct sockaddr *)&mail_addr, sizeof (mail_addr)) != 0)
  105. { perror ("connect");
  106. return(-1);
  107. }
  108. return (mail_fd);
  109. }
  110. }
  111. int _(getsocket)(int mail_fd, char *string, int length)
  112. { int len;
  113. while(1)
  114. { if((len = recv (mail_fd, string, length, 0)) <=0) sleep (1);
  115. else
  116. { string[len] = (char) 0x00;
  117. return(len);
  118. }
  119. }
  120. }
  121. ssize_t _(writesocket)(int mail_fd, char *string, int length)
  122. { return send(mail_fd, string, length, 0);
  123. }
  124. int _(unixclosesocket)(int conn_fd)
  125. { return close(conn_fd);
  126. }
  127. /* end of pslsocket.c */