Back to index

im-sdk  12.3.91
IMTransP.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <sys/types.h>
00045 #include <string.h>
00046 #include <windows.h>
00047 #include <winsock.h>
00048 
00049 #include "IMTrans.hh"
00050 
00051 static int initialized = 0;
00052 
00053 
00054 
00055 IMTransListen::IMTransListen(CompoundString im_address, CompoundString protocol_name) {
00056   struct sockaddr_in sin; // get a listening socket
00057   struct servent *sp;
00058 
00059   if (!initialized) {
00060          WSADATA Data;
00061          WSAStartup(MAKEWORD(1,1), &Data);
00062          initialized = 1;
00063   }
00064   listen_socket = socket(AF_INET, SOCK_STREAM, 0);
00065   if (listen_socket == -1) {
00066     fprintf(stderr, "cannot open socket for %s\n", (char*)protocol_name);
00067     return;
00068   }
00069 
00070   char *p;
00071   char *port_number;
00072 
00073   if (im_address && (p = (char *)strstr(im_address, ":"))) {
00074     port_number = ++p;
00075     sin.sin_port = htons((short)atoi(port_number));
00076   } else if ((sp = getservbyname("IIIMP", "tcp")) != 0) {
00077     sin.sin_port = sp->s_port;
00078   } else {
00079     sin.sin_port = htons((short)9010);    // default
00080   }
00081 
00082   sin.sin_family = AF_INET;
00083   sin.sin_addr.s_addr = htonl(INADDR_ANY);
00084 
00085   const char reuse = 1;
00086   setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
00087 
00088   if (bind(listen_socket, (struct sockaddr*) &sin, sizeof(sin)) != 0) {
00089     shutdown(listen_socket, 2);
00090     fprintf(stderr, "Another IIIMP Server might be running.\n");
00091     return;
00092   }
00093 
00094   int len = sizeof sin;
00095   if (getsockname(listen_socket, (struct sockaddr*) &sin, &len) == -1) {
00096     fprintf(stderr, "Can't get socket name\n");
00097     return;
00098   }
00099 
00100   if (listen(listen_socket, 5) < 0) {
00101     shutdown(listen_socket, 2);
00102     fprintf(stderr, "Can't listen to the socket\n");
00103     return;
00104   }
00105 }
00106 
00107 IMTransListen::~IMTransListen() {
00108   closesocket(listen_socket);
00109   WSACleanup();
00110 }
00111 
00112 IMTransAccept*
00113 IMTransListen::accept() {
00114   int ss;
00115   struct sockaddr_in addr_in;
00116   int addrlen = sizeof(addr_in);
00117 
00118 LOOP:
00119   if ((ss = ::accept(listen_socket,
00120                    (struct sockaddr*)&addr_in, &addrlen)) != -1) {
00121     IMTransAccept *client = new IMTransAccept(ss);
00122     return client;
00123   } else {
00124     if (WSAGetLastError() != WSAEINTR)
00125        throw "Can't accept requests";
00126     else
00127        goto LOOP;           // has been interrupted. accept again     
00128   }
00129   return (IMTransAccept *)0;
00130 }
00131 
00132 IMTransAccept::~IMTransAccept() {
00133   if (accept_fd) {
00134       FD_CLR(accept_fd, &rmask_fd);
00135       closesocket(accept_fd);
00136       shutdown(accept_fd, 2);
00137       accept_fd = 0;
00138   }    
00139 }
00140 
00141 int
00142 IMTransAccept::read(char *buf, int len) {
00143        int numrecv = recv(accept_fd, buf, len, 0);
00144        if ((numrecv == 0) || (numrecv == SOCKET_ERROR)) {
00145               fprintf(stderr, "Error in reading\n");
00146        }
00147   return numrecv;
00148 }
00149 
00150 int
00151 IMTransAccept::select_fd(struct timeval timeout) {
00152   fd_set select_mask;
00153   select_mask = rmask_fd;
00154   int nfds = accept_fd + 1;
00155 
00156   while(1) {
00157     if (select(nfds, &select_mask, NULL, NULL, &timeout) == SOCKET_ERROR) {
00158       if (WSAGetLastError() == WSAEINTR)
00159         continue;
00160       else
00161         return Trans_ERROR;
00162     }
00163     if (FD_ISSET(accept_fd, &select_mask)) {
00164       return Trans_ACCEPT;
00165     }
00166     // timeout
00167     return Trans_TIMEOUT;
00168   }
00169 }
00170 
00171 int
00172 IMTransAccept::write(char *buf, int len) {
00173        int numsend = send(accept_fd, buf, len, 0);
00174        if (numsend != len)
00175               fprintf(stderr, "Error in sending\n");
00176   return numsend;
00177 }
00178 
00179 int
00180 IMTransAccept::connection_id() {return accept_fd;}