[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.13

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

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