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 <errno.h>
00043 #include "IMTrans.hh"
00044 #include <stdlib.h>
00045 #include <strings.h>
00046 #include <sys/types.h>
00047 #include <unistd.h>
00048 #include <netdb.h>
00049 #include <netinet/in.h>
00050 #include <sys/socket.h>
00051 #include <stdio.h>
00052 #include <sys/types.h>
00053 #include <sys/stat.h>
00054 #ifdef linux
00055 #include <sys/un.h>
00056 #endif
00057 #include <sys/resource.h>
00058 
00059 IMTransListen::IMTransListen(CompoundString im_address, CompoundString protocol_name, 
00060       int network) {
00061   struct rlimit glp;
00062   char  *err_msg;
00063 
00064   if (getrlimit(RLIMIT_NOFILE, &glp) < 0) {
00065     err_msg = new char[BUFSIZ];
00066     snprintf(err_msg, BUFSIZ -1, "Error in getrlimit \n");
00067     throw err_msg;
00068   }
00069 
00070   cur_maxfd = glp.rlim_cur; /* initial value */
00071   incr_fd = glp.rlim_cur;   /* initial value */
00072   fd_resource_limit = glp.rlim_max; /* initial value */
00073 
00074   if(network) {
00075     IMTransListenINET(im_address, protocol_name);
00076   } else {
00077 #ifdef linux
00078     IMTransListenUNIX(im_address, protocol_name);
00079 #endif /* linux */
00080   }
00081 }
00082 
00083 #ifdef linux
00084 #define UNIX_SOCKET_DIR         "/tmp/.iiim-unix"
00085 
00086 void
00087 IMTransListen::IMTransListenUNIX(CompoundString im_address, CompoundString protocol_name) {
00088   struct sockaddr_un sin; // get a listening socket
00089   char  *err_msg;
00090   int oldUmask;
00091 
00092   listen_socket = socket(AF_UNIX, SOCK_STREAM, 0);
00093   if (listen_socket == -1) {
00094     err_msg = new char[BUFSIZ];
00095     snprintf(err_msg, BUFSIZ -1, "cannot open socket for %s", (char*)protocol_name); 
00096     throw err_msg;
00097   }
00098 
00099   char *p;
00100   char *port_number = "9010";      // default
00101 
00102   if (im_address && (p = (char *)index(im_address, ':'))) {
00103     port_number = ++p;
00104   }
00105 
00106   sin.sun_family = AF_UNIX;
00107   sprintf(sin.sun_path, "%s/%s", UNIX_SOCKET_DIR, port_number);
00108 
00109   oldUmask = umask (0);
00110 
00111   (void)mkdir(UNIX_SOCKET_DIR, 0777);
00112   (void)chmod(UNIX_SOCKET_DIR, 0777);
00113 
00114   const char reuse = 1;
00115   setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,
00116             (const char*)&reuse, sizeof(int));
00117 
00118   (void)unlink(sin.sun_path);
00119 
00120   if (bind(listen_socket, (struct sockaddr*) &sin, sizeof(sin)) != 0) {
00121     shutdown(listen_socket, 2);
00122     err_msg = new char[BUFSIZ];
00123     snprintf(err_msg, BUFSIZ -1,"Another IIIMP Server might be running.");
00124     umask (oldUmask);
00125     throw err_msg;
00126   }
00127 
00128   socklen_t len = sizeof sin;
00129   if (getsockname(listen_socket, (struct sockaddr*) &sin, &len) == -1) {
00130     err_msg = new char[BUFSIZ];
00131     snprintf(err_msg, BUFSIZ -1, "Can't get socket name");
00132     umask (oldUmask);
00133     throw err_msg;
00134   }
00135 
00136   if (listen(listen_socket, 5) < 0) {
00137     shutdown(listen_socket, 2);
00138     err_msg = new char[BUFSIZ];
00139     snprintf(err_msg, BUFSIZ -1, "Can't listen to the socket");
00140     umask (oldUmask);
00141     throw err_msg;
00142   }
00143     umask (oldUmask);
00144     return;
00145 }
00146 #endif /* linux */
00147 
00148 void
00149 IMTransListen::IMTransListenINET(CompoundString im_address, CompoundString protocol_name) {
00150   struct sockaddr_in sin; // get a listening socket
00151   struct servent *sp;
00152   char  *err_msg;
00153   listen_socket = socket(AF_INET, SOCK_STREAM, 0);
00154   if (listen_socket == -1) {
00155     err_msg = new char[BUFSIZ];
00156     snprintf(err_msg, BUFSIZ -1, "cannot open socket for %s", (char*)protocol_name); 
00157     throw err_msg;
00158   }
00159 
00160   char *p;
00161   char *port_number;
00162 
00163   if (im_address && (p = (char *)index(im_address, ':'))) {
00164     port_number = ++p;
00165     sin.sin_port = htons((short)atoi(port_number));
00166   } else if ((sp = getservbyname("IIIMP", "tcp")) != 0) {
00167     sin.sin_port = sp->s_port;
00168   } else {
00169     sin.sin_port = htons((short)9010);    // default
00170   }
00171 
00172   sin.sin_family = AF_INET;
00173   sin.sin_addr.s_addr = htonl(INADDR_ANY);
00174 
00175   const char reuse = 1;
00176   setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,
00177             (const char*)&reuse, sizeof(int));
00178 
00179   if (bind(listen_socket, (struct sockaddr*) &sin, sizeof(sin)) != 0) {
00180     shutdown(listen_socket, 2);
00181     err_msg = new char[BUFSIZ];
00182     snprintf(err_msg, BUFSIZ -1, "Another IIIMP Server might be running.");
00183     throw err_msg;
00184   }
00185 
00186   socklen_t len = sizeof sin;
00187   if (getsockname(listen_socket, (struct sockaddr*) &sin, &len) == -1) {
00188     err_msg = new char[BUFSIZ];
00189     snprintf(err_msg, BUFSIZ -1, "Can't get socket name");
00190     throw err_msg;
00191   }
00192 
00193   if (listen(listen_socket, 5) < 0) {
00194     shutdown(listen_socket, 2);
00195     err_msg = new char[BUFSIZ];
00196     snprintf(err_msg, BUFSIZ -1, "Can't listen to the socket");
00197     throw err_msg;
00198   }
00199     return;
00200 }
00201 
00202 IMTransListen::~IMTransListen() {
00203   close(listen_socket);
00204 }
00205 
00206 IMTransAccept*
00207 IMTransListen::accept() {
00208   int ss;
00209   struct sockaddr_in addr_in;
00210   socklen_t addrlen = sizeof(addr_in);
00211 
00212 LOOP:
00213   if ((ss = ::accept(listen_socket,
00214                    (struct sockaddr*)&addr_in, &addrlen)) != -1) {
00215     if ((check_fd_resource_limit(ss)) != -1) {
00216       IMTransAccept *client = new IMTransAccept(ss);
00217       return client;
00218     }
00219   }
00220   if (errno != EINTR)
00221     throw "Can't accept requests";
00222   else
00223     goto LOOP;       // has been interrupted. accept again
00224 }
00225 
00226 int
00227 IMTransListen::check_fd_resource_limit(int fd) {
00228   char *err_msg;
00229 
00230   if (fd < (cur_maxfd - 1)) 
00231     return fd;
00232 
00233   if (fd >= (cur_maxfd - 1)) {
00234       struct rlimit slp;
00235       if (cur_maxfd == fd_resource_limit) 
00236         return -1;
00237       cur_maxfd += incr_fd;
00238       if (cur_maxfd > fd_resource_limit) 
00239         cur_maxfd = fd_resource_limit;
00240       slp.rlim_cur = cur_maxfd;
00241       slp.rlim_max = fd_resource_limit;
00242       if (setrlimit(RLIMIT_NOFILE, (const struct rlimit*)&slp) < 0) {
00243         err_msg = new char[BUFSIZ];
00244         snprintf(err_msg, BUFSIZ -1, "Error in setrlimit \n");
00245         throw err_msg;
00246       }
00247   }
00248   return fd;
00249 }
00250 
00251 IMTransAccept::~IMTransAccept() {
00252   if (accept_fd) {
00253     FD_CLR(accept_fd, &rmask_fd);
00254     close(accept_fd);
00255     shutdown(accept_fd, 2);
00256     accept_fd = 0;
00257   }
00258 }
00259 
00260 int
00261 IMTransAccept::read(char *buf, int len) {
00262   return ::read(accept_fd, buf, len);
00263 }
00264 
00265 int
00266 IMTransAccept::select_fd(struct timeval timeout) {
00267   fd_set select_mask;
00268   select_mask = rmask_fd;
00269   int nfds = accept_fd + 1;
00270 
00271   while(1) {
00272     if (select(nfds, &select_mask, NULL, NULL, &timeout) == -1) {
00273       if (errno == EINTR)
00274        continue;
00275       else
00276        return Trans_ERROR;
00277     }
00278     if (FD_ISSET(accept_fd, &select_mask)) {
00279       return Trans_ACCEPT;
00280     }
00281     // timeout
00282     return Trans_TIMEOUT;
00283   }
00284 }
00285 
00286 int
00287 IMTransAccept::write(char *buf, int len) {
00288   return ::write(accept_fd, buf, len);
00289 }
00290 
00291 int
00292 IMTransAccept::connection_id() {return accept_fd;}