[BACK]Return to mytcpio.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/mytcpio.c, Revision 1.16

1.16    ! ohara       1: /*  $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.15 2004/11/23 05:28:19 takayama Exp $ */
1.1       maekawa     2: #include <stdio.h>
1.16    ! ohara       3: #include <string.h>
1.1       maekawa     4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <sys/time.h>
                      7: #include <netinet/in.h>
                      8: #include <netdb.h>
                      9: #include <setjmp.h>
1.13      takayama   10: #include <errno.h>
1.1       maekawa    11: /* -lnsl -lsocket /usr/ucblib/libucb.a */
                     12: #include "ox_kan.h"
                     13: /*
                     14:    MyEnv_oxmisc is used in oxmisc.c.
                     15:    It cannot be put in oxmisc.c for the case of sun.
                     16: */
                     17: #if defined(sun)
                     18: int MyEnv_oxmisc[2000];
                     19: #else
1.6       takayama   20: #if defined(__CYGWIN__)
                     21: sigjmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */
                     22: #else
1.1       maekawa    23: jmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */
1.6       takayama   24: #endif
1.1       maekawa    25: #endif
                     26:
                     27: #define READBUFSIZE 10000
1.7       takayama   28: #define MAX_LISTEN_QUEUE 3
1.1       maekawa    29: static void errorMsg1s(char *s) {
                     30:   fprintf(stderr,"%s\n",s);
                     31: }
                     32:
1.3       takayama   33: #define SET_TCPIOERROR  { if (TcpioError == NULL) TcpioError = stdout; }
                     34: FILE *TcpioError = NULL;
1.1       maekawa    35: int OpenedSocket = 0;
                     36: extern int Quiet;
                     37:
                     38: socketOpen(char *serverName,int portNumber) {
                     39:   static struct hostent *myhost;
                     40:   static struct sockaddr_in me;
                     41:   static int s_waiting;
                     42:   static int on;
                     43:   int tt;
                     44:
1.3       takayama   45:   SET_TCPIOERROR;
1.14      takayama   46:   if (!Quiet) fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n",
1.5       takayama   47:           serverName,portNumber);
1.1       maekawa    48:   if ((myhost = gethostbyname(serverName)) == NULL) {
                     49:     errorMsg1s("Bad server name.");
                     50:     return(-1);
                     51:   }
                     52:   bzero((char *)&me,sizeof(me));
                     53:   me.sin_family = AF_INET;
                     54:   me.sin_port = htons(portNumber);
                     55:   bcopy(myhost->h_addr,
1.5       takayama   56:         &me.sin_addr,myhost->h_length);
1.1       maekawa    57:
                     58:   if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
                     59:     errorMsg1s("Socket allocation is failed.");
                     60:     return(-1);
                     61:   }
                     62:
                     63:   on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
                     64:   /* important */
                     65:   if ((tt = bind(s_waiting,(struct sockaddr *) &me,sizeof(me))) == -1) {
                     66:     fprintf(TcpioError,"Bind error. Error no is %d. See /usr/include/sys/errno.h. (asm/errno.h)\n",errno);
                     67:     errorMsg1s("cannot bind");
                     68:     return(-1);
                     69:   }
                     70:   /* printf("bind returns %d\n",tt); */
                     71:   tt = sizeof(me);
                     72:   if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
                     73:     fprintf(TcpioError,"getsockname error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                     74:     errorMsg1s("cannot getsockname");
                     75:     return(-1);
                     76:   }
                     77:
1.7       takayama   78:   if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) {
1.1       maekawa    79:     errorMsg1s("Listen failed");
                     80:     return(-1);
                     81:   }
1.14      takayama   82:   if (!Quiet) fprintf(TcpioError,"Done the initialization. port =%d\n",ntohs(me.sin_port));
1.1       maekawa    83:   OpenedSocket = ntohs(me.sin_port);
                     84:   return(s_waiting);
                     85: }
                     86:
                     87:
                     88: socketAccept(int snum) {
                     89:   int s, news;
                     90:
1.3       takayama   91:   SET_TCPIOERROR;
1.1       maekawa    92:   s = snum;
1.15      takayama   93:   if (!Quiet) {fprintf(TcpioError,"Trying to accept... "); fflush(TcpioError);}
1.1       maekawa    94:   if ((news = accept(s,NULL,NULL)) < 0) {
1.12      takayama   95:     errorMsg1s("Error in accept. Retrying (socketAccept).");
                     96:     /* Code added for strange behavior on cygwin. */
                     97:     if ((news = accept(s,NULL,NULL)) < 0) {
                     98:       errorMsg1s("Error in accept. Retry failed.");
                     99:       return (-1);
                    100:     }
1.1       maekawa   101:   }
1.15      takayama  102:   if (!Quiet) {fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);}
1.1       maekawa   103:   if (close(s) < 0) {
                    104:     errorMsg1s("Error in closing the old socket.");
                    105:     return(-1);
                    106:   }
                    107:   return(news);
                    108: }
                    109:
                    110: socketAcceptLocal(int snum) {
                    111:   int s, news;
                    112:   struct sockaddr peer;
                    113:   int len;
                    114:   int i;
1.3       takayama  115:
                    116:   SET_TCPIOERROR;
1.1       maekawa   117:   s = snum;
1.15      takayama  118:   if (!Quiet) {fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);}
1.1       maekawa   119:   len = sizeof(struct sockaddr);
                    120:   if ((news = accept(s,&peer,&len)) < 0) {
1.10      takayama  121:     errorMsg1s("Error in accept. Retrying");
1.11      takayama  122:     /* Code added for strange behavior on cygwin. */
                    123:    if ((news = accept(s,&peer,&len)) < 0) {
1.12      takayama  124:       errorMsg1s("Error in accept. Retry failed.");
1.11      takayama  125:       return (-1);
                    126:    }
1.1       maekawa   127:   }
                    128:
                    129:   len = sizeof(struct sockaddr);
                    130:   getpeername(news,&peer,&len);
1.14      takayama  131:   if (!Quiet) printf("len= %d\n",len);
1.1       maekawa   132:   for (i=0; i<len; i++) {
1.14      takayama  133:     if (!Quiet) printf(" %x ",peer.sa_data[i]);
1.1       maekawa   134:   }
1.15      takayama  135:   if (!Quiet) printf("\n");
1.1       maekawa   136:   if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
                    137:       peer.sa_data[4] == 0    && peer.sa_data[5] == 1) {
1.14      takayama  138:     if (!Quiet) fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
1.1       maekawa   139:   }else{
1.4       takayama  140:     errorMsg1s("Authentication: The connection is not from the localhost.");
1.1       maekawa   141:     close(s);
                    142:     fprintf(stderr,"The connection is refused.");
                    143:     return(-1);
                    144:   }
                    145:
1.15      takayama  146:   if (!Quiet) {fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);}
1.1       maekawa   147:   if (close(s) < 0) {
                    148:     errorMsg1s("Error in closing the old socket.");
                    149:     return(-1);
                    150:   }
1.8       takayama  151:   return(news);
                    152: }
                    153:
                    154: /* It does not close the socket for listening. */
                    155: socketAcceptLocal2(int snum) {
                    156:   int s, news;
                    157:   struct sockaddr peer;
                    158:   int len;
                    159:   int i;
                    160:
                    161:   SET_TCPIOERROR;
                    162:   s = snum;
1.15      takayama  163:   if (!Quiet) {fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);}
1.8       takayama  164:   len = sizeof(struct sockaddr);
                    165:   if ((news = accept(s,&peer,&len)) < 0) {
1.12      takayama  166:     errorMsg1s("Error in accept. Retrying (socketAcceptLocal2).");
                    167:     /* Code added for strange behavior on cygwin. */
                    168:     if ((news = accept(s,&peer,&len)) < 0) {
                    169:       errorMsg1s("Error in accept. Retry failed.");
                    170:       return (-1);
                    171:     }
1.8       takayama  172:   }
                    173:
                    174:   len = sizeof(struct sockaddr);
                    175:   getpeername(news,&peer,&len);
1.14      takayama  176:   if (!Quiet) printf("len= %d\n",len);
1.8       takayama  177:   for (i=0; i<len; i++) {
1.14      takayama  178:     if (!Quiet) printf(" %x ",peer.sa_data[i]);
1.8       takayama  179:   }
1.15      takayama  180:   if (!Quiet) printf("\n");
1.8       takayama  181:   if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
                    182:       peer.sa_data[4] == 0    && peer.sa_data[5] == 1) {
1.14      takayama  183:     if (!Quiet) fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
1.8       takayama  184:   }else{
                    185:     errorMsg1s("Authentication: The connection is not from the localhost.");
                    186:     fprintf(stderr,"The connection is refused.");
                    187:     return(-1);
                    188:   }
                    189:
1.15      takayama  190:   if (!Quiet) {fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);}
1.1       maekawa   191:   return(news);
                    192: }
                    193:
                    194: int oxSocketSelect0(int fd,int t) {
                    195:   fd_set readfds;
                    196:   struct timeval timeout;
                    197:   int debug = 0;
1.3       takayama  198:   SET_TCPIOERROR;
1.1       maekawa   199:   FD_ZERO(&readfds);
                    200:   FD_SET(fd,&readfds);
                    201:   timeout.tv_sec = 0;
                    202:   timeout.tv_usec = (long) t;
                    203:   if (t >= 0) {
                    204:     if (debug) {printf("select t>=0 for fd = %d\n",fd); fflush(NULL);}
                    205:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    206:       /* It must be fd+1 !,  Not fd. */
                    207:       fprintf(TcpioError,"select (non-block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    208:       errorMsg1s("oxSocketSelect0() : select failed.");
                    209:       return(0);
                    210:     }
                    211:     if(debug){printf("Return from select t>=0 for fd = %d\n",fd);fflush(NULL);}
                    212:   }else{ /* block */
                    213:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    214:       fprintf(TcpioError,"select (block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    215:       errorMsg1s("socketSelect0() : select failed.");
                    216:       return(0);
                    217:     }
                    218:   }
                    219:   if (FD_ISSET(fd,&readfds)) return(1);
                    220:   else return(0);
                    221: }
                    222:
                    223: oxSocketMultiSelect(int sid[],int size,int t,int result[])
                    224: {
                    225:   int i,fd,p;
                    226:   fd_set readfds;
                    227:   struct timeval timeout;
                    228:   int isdata = 0;
                    229:
1.3       takayama  230:   SET_TCPIOERROR;
1.1       maekawa   231:   FD_ZERO(&readfds);
                    232:   timeout.tv_sec = 0;
                    233:   timeout.tv_usec = (long)t;
                    234:
                    235:   fd = 0;
                    236:
                    237:   for (i=0; i<size; i++) {
                    238:     if (sid[i] >= 0) {
                    239:       p = sid[i];
                    240:       if (p > fd) fd = p;
                    241:       FD_SET(p,&readfds);
                    242:       /* printf("p = %d, fd=%d",p,fd); */
                    243:     }
                    244:   }
                    245:
                    246:   /* printf("MultiSelect..\n"); fflush(NULL); */
                    247:   if (t >= 0) {
                    248:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    249:       /* It must be fd+1 !,  Not fd. */
                    250:       fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    251:       errorMsg1s("oxSocketMultiSelect() : select failed.");
                    252:       return(0);
                    253:     }
                    254:   }else{ /* block */
                    255:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    256:       fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    257:       errorMsg1s("oxSocketMultiSelect() : (block) select failed.");
                    258:       return(0);
                    259:     }
                    260:   }
                    261:   /* printf("Done. MultiSelect.\n"); fflush(NULL); */
                    262:   for (i=0; i<size; i++) {
                    263:     p = sid[i];
                    264:     if ((sid[i] >=0) && FD_ISSET(p,&readfds)) {
                    265:       result[i] = 1; isdata = 1;
                    266:     }else{
                    267:       result[i] = 0;
                    268:     }
                    269:   }
                    270:   return(isdata);
                    271: }
                    272:
                    273:
                    274: socketConnect(char *serverName,int portNumber) {
                    275:   struct hostent *servhost;
                    276:   struct sockaddr_in server;
                    277:   int socketid;
                    278:   int on;
                    279:
1.3       takayama  280:   SET_TCPIOERROR;
1.1       maekawa   281:   if ((servhost = gethostbyname(serverName)) == NULL) {
                    282:     errorMsg1s("bad server name.\n");
                    283:     return(-1);
                    284:   }
                    285:   bzero((char *)&server,sizeof(server));
                    286:   server.sin_family = AF_INET;
                    287:   server.sin_port = htons(portNumber);
                    288:   bcopy(servhost->h_addr,
1.5       takayama  289:         (char *)&server.sin_addr,servhost->h_length);
1.1       maekawa   290:
                    291:   if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
                    292:     errorMsg1s("socket allocation is failed.\n");
                    293:     return(-1);
                    294:   }
                    295:   /* on=1; setsockopt(socketid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));  */
                    296:   if (!Quiet) {
                    297:     fprintf(TcpioError,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
                    298:   }
                    299:   if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
                    300:     errorMsg1s("cannot connect");
                    301:     return(-1);
                    302:   }
                    303:   if (!Quiet) fprintf(TcpioError,"connected.\n");
                    304:   return(socketid);
                    305: }
                    306:
                    307: socketConnectWithPass(char *servername,int port,char *pass)
                    308: {
                    309:   int fd;
                    310:   int m;
1.3       takayama  311:   SET_TCPIOERROR;
1.1       maekawa   312:   fd = socketConnect(servername,port);
1.9       takayama  313:   if ((pass == NULL) && (fd >= 0)) return fd;
                    314:   if ((pass == NULL) && (fd < 0)) return -1;
1.1       maekawa   315:   if (fd >= 0) {
1.2       takayama  316:     m = write(fd,pass,strlen(pass)+1);
                    317:     if (m != strlen(pass)+1) {
1.1       maekawa   318:       fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd);
                    319:       return(-1);
                    320:     }
                    321:     return(fd);
                    322:   }else{
                    323:     return(-1);
                    324:   }
                    325: }
                    326:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>