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

1.7     ! takayama    1: /*  $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.6 2002/02/24 10:27:20 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>
                      9: /* -lnsl -lsocket /usr/ucblib/libucb.a */
                     10: #include "ox_kan.h"
                     11: /*
                     12:    MyEnv_oxmisc is used in oxmisc.c.
                     13:    It cannot be put in oxmisc.c for the case of sun.
                     14: */
                     15: #if defined(sun)
                     16: int MyEnv_oxmisc[2000];
                     17: #else
1.6       takayama   18: #if defined(__CYGWIN__)
                     19: sigjmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */
                     20: #else
1.1       maekawa    21: jmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */
1.6       takayama   22: #endif
1.1       maekawa    23: #endif
                     24:
                     25: #define READBUFSIZE 10000
1.7     ! takayama   26: #define MAX_LISTEN_QUEUE 3
1.1       maekawa    27: static void errorMsg1s(char *s) {
                     28:   fprintf(stderr,"%s\n",s);
                     29: }
                     30:
1.3       takayama   31: #define SET_TCPIOERROR  { if (TcpioError == NULL) TcpioError = stdout; }
                     32: FILE *TcpioError = NULL;
1.1       maekawa    33: int OpenedSocket = 0;
                     34: extern int Quiet;
                     35:
                     36: socketOpen(char *serverName,int portNumber) {
                     37:   static struct hostent *myhost;
                     38:   static struct sockaddr_in me;
                     39:   static int s_waiting;
                     40:   static int on;
                     41:   extern int errno;
                     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) {
                     94:     errorMsg1s("Error in accept.");
                     95:     return(-1);
                     96:   }
                     97:   fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
                     98:   if (close(s) < 0) {
                     99:     errorMsg1s("Error in closing the old socket.");
                    100:     return(-1);
                    101:   }
                    102:   return(news);
                    103: }
                    104:
                    105: socketAcceptLocal(int snum) {
                    106:   int s, news;
                    107:   struct sockaddr peer;
                    108:   int len;
                    109:   int i;
1.3       takayama  110:
                    111:   SET_TCPIOERROR;
1.1       maekawa   112:   s = snum;
                    113:   fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);
                    114:   len = sizeof(struct sockaddr);
                    115:   if ((news = accept(s,&peer,&len)) < 0) {
                    116:     errorMsg1s("Error in accept.");
                    117:     return(-1);
                    118:   }
                    119:
                    120:   len = sizeof(struct sockaddr);
                    121:   getpeername(news,&peer,&len);
                    122:   printf("len= %d\n",len);
                    123:   for (i=0; i<len; i++) {
                    124:     printf(" %x ",peer.sa_data[i]);
                    125:   }
                    126:   printf("\n");
                    127:   if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
                    128:       peer.sa_data[4] == 0    && peer.sa_data[5] == 1) {
1.4       takayama  129:     fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
1.1       maekawa   130:   }else{
1.4       takayama  131:     errorMsg1s("Authentication: The connection is not from the localhost.");
1.1       maekawa   132:     close(s);
                    133:     fprintf(stderr,"The connection is refused.");
                    134:     return(-1);
                    135:   }
                    136:
                    137:   fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
                    138:   if (close(s) < 0) {
                    139:     errorMsg1s("Error in closing the old socket.");
                    140:     return(-1);
                    141:   }
                    142:   return(news);
                    143: }
                    144:
                    145: int oxSocketSelect0(int fd,int t) {
                    146:   fd_set readfds;
                    147:   struct timeval timeout;
                    148:   int debug = 0;
                    149:   extern int errno;
1.3       takayama  150:   SET_TCPIOERROR;
1.1       maekawa   151:   FD_ZERO(&readfds);
                    152:   FD_SET(fd,&readfds);
                    153:   timeout.tv_sec = 0;
                    154:   timeout.tv_usec = (long) t;
                    155:   if (t >= 0) {
                    156:     if (debug) {printf("select t>=0 for fd = %d\n",fd); fflush(NULL);}
                    157:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    158:       /* It must be fd+1 !,  Not fd. */
                    159:       fprintf(TcpioError,"select (non-block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    160:       errorMsg1s("oxSocketSelect0() : select failed.");
                    161:       return(0);
                    162:     }
                    163:     if(debug){printf("Return from select t>=0 for fd = %d\n",fd);fflush(NULL);}
                    164:   }else{ /* block */
                    165:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    166:       fprintf(TcpioError,"select (block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    167:       errorMsg1s("socketSelect0() : select failed.");
                    168:       return(0);
                    169:     }
                    170:   }
                    171:   if (FD_ISSET(fd,&readfds)) return(1);
                    172:   else return(0);
                    173: }
                    174:
                    175: oxSocketMultiSelect(int sid[],int size,int t,int result[])
                    176: {
                    177:   int i,fd,p;
                    178:   fd_set readfds;
                    179:   struct timeval timeout;
                    180:   int isdata = 0;
                    181:   extern errno;
                    182:
1.3       takayama  183:   SET_TCPIOERROR;
1.1       maekawa   184:   FD_ZERO(&readfds);
                    185:   timeout.tv_sec = 0;
                    186:   timeout.tv_usec = (long)t;
                    187:
                    188:   fd = 0;
                    189:
                    190:   for (i=0; i<size; i++) {
                    191:     if (sid[i] >= 0) {
                    192:       p = sid[i];
                    193:       if (p > fd) fd = p;
                    194:       FD_SET(p,&readfds);
                    195:       /* printf("p = %d, fd=%d",p,fd); */
                    196:     }
                    197:   }
                    198:
                    199:   /* printf("MultiSelect..\n"); fflush(NULL); */
                    200:   if (t >= 0) {
                    201:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    202:       /* It must be fd+1 !,  Not fd. */
                    203:       fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    204:       errorMsg1s("oxSocketMultiSelect() : select failed.");
                    205:       return(0);
                    206:     }
                    207:   }else{ /* block */
                    208:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    209:       fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    210:       errorMsg1s("oxSocketMultiSelect() : (block) select failed.");
                    211:       return(0);
                    212:     }
                    213:   }
                    214:   /* printf("Done. MultiSelect.\n"); fflush(NULL); */
                    215:   for (i=0; i<size; i++) {
                    216:     p = sid[i];
                    217:     if ((sid[i] >=0) && FD_ISSET(p,&readfds)) {
                    218:       result[i] = 1; isdata = 1;
                    219:     }else{
                    220:       result[i] = 0;
                    221:     }
                    222:   }
                    223:   return(isdata);
                    224: }
                    225:
                    226:
                    227: socketConnect(char *serverName,int portNumber) {
                    228:   struct hostent *servhost;
                    229:   struct sockaddr_in server;
                    230:   int socketid;
                    231:   int on;
                    232:
1.3       takayama  233:   SET_TCPIOERROR;
1.1       maekawa   234:   if ((servhost = gethostbyname(serverName)) == NULL) {
                    235:     errorMsg1s("bad server name.\n");
                    236:     return(-1);
                    237:   }
                    238:   bzero((char *)&server,sizeof(server));
                    239:   server.sin_family = AF_INET;
                    240:   server.sin_port = htons(portNumber);
                    241:   bcopy(servhost->h_addr,
1.5       takayama  242:         (char *)&server.sin_addr,servhost->h_length);
1.1       maekawa   243:
                    244:   if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
                    245:     errorMsg1s("socket allocation is failed.\n");
                    246:     return(-1);
                    247:   }
                    248:   /* on=1; setsockopt(socketid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));  */
                    249:   if (!Quiet) {
                    250:     fprintf(TcpioError,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
                    251:   }
                    252:   if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
                    253:     errorMsg1s("cannot connect");
                    254:     return(-1);
                    255:   }
                    256:   if (!Quiet) fprintf(TcpioError,"connected.\n");
                    257:   return(socketid);
                    258: }
                    259:
                    260: socketConnectWithPass(char *servername,int port,char *pass)
                    261: {
                    262:   int fd;
                    263:   int m;
1.3       takayama  264:   SET_TCPIOERROR;
1.1       maekawa   265:   fd = socketConnect(servername,port);
                    266:   if (fd >= 0) {
1.2       takayama  267:     m = write(fd,pass,strlen(pass)+1);
                    268:     if (m != strlen(pass)+1) {
1.1       maekawa   269:       fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd);
                    270:       return(-1);
                    271:     }
                    272:     return(fd);
                    273:   }else{
                    274:     return(-1);
                    275:   }
                    276: }
                    277:

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