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

1.4.2.3 ! maekawa     1: /*  $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.4.2.2 2000/11/11 04:44:30 maekawa 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
                     18: jmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */
                     19: #endif
                     20:
                     21: #define READBUFSIZE 10000
                     22: static void errorMsg1s(char *s) {
                     23:   fprintf(stderr,"%s\n",s);
                     24: }
                     25:
1.3       takayama   26: #define SET_TCPIOERROR  { if (TcpioError == NULL) TcpioError = stdout; }
                     27: FILE *TcpioError = NULL;
1.1       maekawa    28: int OpenedSocket = 0;
                     29: extern int Quiet;
                     30:
                     31: socketOpen(char *serverName,int portNumber) {
1.4.2.1   maekawa    32:        struct addrinfo hints, *res, *ai;
                     33:        struct sockaddr_storage ss;
                     34:        socklen_t len;
                     35:        char pstr[BUFSIZ], *errstr;
                     36:        int s, p, opt, error;
                     37:
                     38:        SET_TCPIOERROR;
                     39:        fprintf(TcpioError, "Hello from open. serverName is %s and "
                     40:                            "portnumber is %d\n", serverName, portNumber);
                     41:
                     42:        memset(&hints, 0, sizeof(hints));
                     43:        hints.ai_family = PF_UNSPEC;
                     44:        hints.ai_socktype = SOCK_STREAM;
                     45:
                     46:        memset(pstr, 0, sizeof(pstr));
1.4.2.3 ! maekawa    47:        snprintf(pstr, sizeof(pstr), "%d", portNumber);
1.4.2.1   maekawa    48:
                     49:        error = getaddrinfo(serverName, pstr, &hints, &res);
                     50:        if (error) {
                     51:                errorMsg1s("Bad server name.");
                     52:                return (-1);
                     53:        }
                     54:
                     55:        s = -1;
                     56:        p = -1;
                     57:        errstr = NULL;
                     58:
                     59:        for (ai = res ; ai != NULL ; ai = ai->ai_next) {
                     60:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
                     61:                if (s < 0) {
                     62:                        errstr = "socket";
                     63:                        continue;
                     64:                }
                     65:
                     66:                opt = 1;
                     67:                setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
                     68:
                     69:                if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                     70:                        errstr = "bind";
                     71:                        close(s);
                     72:                        s = -1;
                     73:                        continue;
                     74:                }
                     75:
                     76:                len = sizeof(ss);
                     77:                if (getsockname(s, (struct sockaddr *)&ss, &len) < 0) {
                     78:                        errstr = "getsockname";
                     79:                        close(s);
                     80:                        s = -1;
                     81:                        continue;
                     82:                }
                     83:
                     84:                switch (ss.ss_family) {
                     85:                case AF_INET:
                     86:                {
                     87:                        struct sockaddr_in *sin;
                     88:                        sin = (struct sockaddr_in *)&ss;
                     89:                        p = ntohs(sin->sin_port);
                     90:                        break;
                     91:                }
                     92:                case AF_INET6:
                     93:                {
                     94:                        struct sockaddr_in6 *sin6;
                     95:                        sin6 = (struct sockaddr_in6 *)&ss;
                     96:                        p = ntohs(sin6->sin6_port);
                     97:                        break;
                     98:                }
                     99:                default:
                    100:                        p = -1;
                    101:                }
                    102:                if (p < 0) {
                    103:                        errstr = "AF";
                    104:                        close(s);
                    105:                        s = -1;
                    106:                        continue;
                    107:                }
                    108:
                    109:                if (listen(s, 1) < 0) {
                    110:                        errstr = "listen";
                    111:                        close(s);
                    112:                        s = -1;
                    113:                        continue;
                    114:                }
                    115:
                    116:                break;
                    117:        }
                    118:
                    119:        freeaddrinfo(res);
                    120:
                    121:        if (s < 0) {
                    122:                fprintf(TcpioError, "Error: %s", errstr);
                    123:                errorMsg1s(errstr);
                    124:        } else {
                    125:                OpenedSocket = p;
                    126:        }
1.1       maekawa   127:
1.4.2.1   maekawa   128:        return (s);
1.1       maekawa   129: }
                    130:
                    131:
                    132: socketAccept(int snum) {
                    133:   int s, news;
                    134:
1.3       takayama  135:   SET_TCPIOERROR;
1.1       maekawa   136:   s = snum;
                    137:   fprintf(TcpioError,"Trying to accept... "); fflush(TcpioError);
                    138:   if ((news = accept(s,NULL,NULL)) < 0) {
                    139:     errorMsg1s("Error in accept.");
                    140:     return(-1);
                    141:   }
                    142:   fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);
                    143:   if (close(s) < 0) {
                    144:     errorMsg1s("Error in closing the old socket.");
                    145:     return(-1);
                    146:   }
                    147:   return(news);
                    148: }
                    149:
1.4.2.2   maekawa   150: socketAcceptLocal(int s) {
                    151:        struct sockaddr_storage ss;
                    152:        socklen_t len;
                    153:        int ps, accepted, error;
1.3       takayama  154:
1.4.2.2   maekawa   155:        SET_TCPIOERROR;
1.1       maekawa   156:
1.4.2.2   maekawa   157:        fprintf(TcpioError,"Trying to accept from localhost... ");
                    158:        fflush(TcpioError);
1.1       maekawa   159:
1.4.2.2   maekawa   160:        len = sizeof(ss);
                    161:        if ((ps = accept(s, (struct sockaddr *)&ss, &len)) < 0) {
                    162:                errorMsg1s("Error in accept.");
                    163:                close(s);
                    164:                return (-1);
                    165:        }
                    166:
                    167:        len = sizeof(ss);
                    168:        if (getpeername(ps, (struct sockaddr *)&ss, &len) < 0) {
                    169:                close(s);
                    170:                close(ps);
                    171:                errorMsg1s("Error in getpeername.");
                    172:                return (-1);
                    173:        }
                    174:
                    175:        accepted = 0;
                    176:        switch (ss.ss_family) {
                    177:        case AF_INET:
                    178:        {
                    179: #ifndef INADDR_LOOPBACK
                    180: #define        INADDR_LOOPBACK         0x7f000001      /* 127.0.0.1 */
                    181: #endif /* INADDR_LOOPBACK */
                    182:                struct sockaddr_in *sin;
                    183:                sin = (struct sockaddr_in *)&ss;
                    184:                if (sin->sin_addr.s_addr == INADDR_LOOPBACK)
                    185:                        accepted = 1;
                    186:                break;
                    187:        }
                    188:        case AF_INET6:
                    189:        {
                    190:                struct sockaddr_in6 *sin6;
                    191:                sin6 = (struct sockaddr_in6 *)&ss;
1.4.2.3 ! maekawa   192:                if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
1.4.2.2   maekawa   193:                        accepted = 1;
                    194:                break;
                    195:        }
                    196:        default:
                    197:                accepted = 0;
                    198:        }
                    199:
                    200:        if (accepted) {
                    201:                fprintf(stderr, "Authentication: "
                    202:                                "localhost is allowed to be accepted.\n");
                    203:        } else {
                    204:                errorMsg1s("Authentication: "
                    205:                           "The connection is not from the localhost.");
                    206:                close(s);
                    207:                close(ps);
                    208:                fprintf(stderr, "The connection is refused.");
                    209:                return (-1);
                    210:        }
                    211:
                    212:        fprintf(TcpioError, "Accepted.\n");
                    213:        fflush(TcpioError);
                    214:        if (close(s) < 0) {
                    215:                errorMsg1s("Error in closing the old socket.");
                    216:                close(ps);
                    217:                return(-1);
                    218:        }
                    219:
                    220:        return(ps);
1.1       maekawa   221: }
                    222:
                    223: int oxSocketSelect0(int fd,int t) {
                    224:   fd_set readfds;
                    225:   struct timeval timeout;
                    226:   int debug = 0;
                    227:   extern int errno;
1.3       takayama  228:   SET_TCPIOERROR;
1.1       maekawa   229:   FD_ZERO(&readfds);
                    230:   FD_SET(fd,&readfds);
                    231:   timeout.tv_sec = 0;
                    232:   timeout.tv_usec = (long) t;
                    233:   if (t >= 0) {
                    234:     if (debug) {printf("select t>=0 for fd = %d\n",fd); fflush(NULL);}
                    235:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    236:       /* It must be fd+1 !,  Not fd. */
                    237:       fprintf(TcpioError,"select (non-block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    238:       errorMsg1s("oxSocketSelect0() : select failed.");
                    239:       return(0);
                    240:     }
                    241:     if(debug){printf("Return from select t>=0 for fd = %d\n",fd);fflush(NULL);}
                    242:   }else{ /* block */
                    243:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    244:       fprintf(TcpioError,"select (block) error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    245:       errorMsg1s("socketSelect0() : select failed.");
                    246:       return(0);
                    247:     }
                    248:   }
                    249:   if (FD_ISSET(fd,&readfds)) return(1);
                    250:   else return(0);
                    251: }
                    252:
                    253: oxSocketMultiSelect(int sid[],int size,int t,int result[])
                    254: {
                    255:   int i,fd,p;
                    256:   fd_set readfds;
                    257:   struct timeval timeout;
                    258:   int isdata = 0;
                    259:   extern errno;
                    260:
1.3       takayama  261:   SET_TCPIOERROR;
1.1       maekawa   262:   FD_ZERO(&readfds);
                    263:   timeout.tv_sec = 0;
                    264:   timeout.tv_usec = (long)t;
                    265:
                    266:   fd = 0;
                    267:
                    268:   for (i=0; i<size; i++) {
                    269:     if (sid[i] >= 0) {
                    270:       p = sid[i];
                    271:       if (p > fd) fd = p;
                    272:       FD_SET(p,&readfds);
                    273:       /* printf("p = %d, fd=%d",p,fd); */
                    274:     }
                    275:   }
                    276:
                    277:   /* printf("MultiSelect..\n"); fflush(NULL); */
                    278:   if (t >= 0) {
                    279:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    280:       /* It must be fd+1 !,  Not fd. */
                    281:       fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    282:       errorMsg1s("oxSocketMultiSelect() : select failed.");
                    283:       return(0);
                    284:     }
                    285:   }else{ /* block */
                    286:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    287:       fprintf(TcpioError,"Select error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
                    288:       errorMsg1s("oxSocketMultiSelect() : (block) select failed.");
                    289:       return(0);
                    290:     }
                    291:   }
                    292:   /* printf("Done. MultiSelect.\n"); fflush(NULL); */
                    293:   for (i=0; i<size; i++) {
                    294:     p = sid[i];
                    295:     if ((sid[i] >=0) && FD_ISSET(p,&readfds)) {
                    296:       result[i] = 1; isdata = 1;
                    297:     }else{
                    298:       result[i] = 0;
                    299:     }
                    300:   }
                    301:   return(isdata);
                    302: }
                    303:
                    304:
                    305: socketConnect(char *serverName,int portNumber) {
1.4.2.3 ! maekawa   306:        struct addrinfo hints, *res, *ai;
        !           307:        char pstr[BUFSIZ], *errstr;
        !           308:        int s, error;
1.1       maekawa   309:
1.4.2.3 ! maekawa   310:        SET_TCPIOERROR;
1.1       maekawa   311:
1.4.2.3 ! maekawa   312:        memset(&hints, 0, sizeof(hints));
        !           313:        hints.ai_family = PF_UNSPEC;
        !           314:        hints.ai_socktype = SOCK_STREAM;
        !           315:
        !           316:        memset(pstr, 0, sizeof(pstr));
        !           317:        snprintf(pstr, sizeof(pstr), "%d", portNumber);
        !           318:
        !           319:        error = getaddrinfo(serverName, pstr, &hints, &res);
        !           320:        if (error) {
        !           321:                errorMsg1s("bad server name.\n");
        !           322:                return (-1);
        !           323:        }
        !           324:
        !           325:        s = -1;
        !           326:        errstr = NULL;
        !           327:
        !           328:        for (ai = res ; ai != NULL ; ai = ai->ai_next) {
        !           329:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
        !           330:                if (s < 0) {
        !           331:                        errstr = "socket";
        !           332:                        continue;
        !           333:                }
        !           334:
        !           335:                if (!Quiet) {
        !           336:                        fprintf(TcpioError, "Trying to connect %s, port %d\n",
        !           337:                                            serverName, portNumber);
        !           338:                }
        !           339:
        !           340:                if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
        !           341:                        errstr = "connect";
        !           342:                        close(s);
        !           343:                        s = -1;
        !           344:                        continue;
        !           345:                }
        !           346:        }
        !           347:
        !           348:        freeaddrinfo(res);
        !           349:
        !           350:        if (s < 0) {
        !           351:                fprintf(stderr, "Error %s\n", errstr);
        !           352:                return (-1);
        !           353:        }
        !           354:
        !           355:        if (!Quiet)
        !           356:                fprintf(TcpioError,"connected.\n");
        !           357:
        !           358:        return (s);
1.1       maekawa   359: }
                    360:
                    361: socketConnectWithPass(char *servername,int port,char *pass)
                    362: {
                    363:   int fd;
                    364:   int m;
1.3       takayama  365:   SET_TCPIOERROR;
1.1       maekawa   366:   fd = socketConnect(servername,port);
                    367:   if (fd >= 0) {
1.2       takayama  368:     m = write(fd,pass,strlen(pass)+1);
                    369:     if (m != strlen(pass)+1) {
1.1       maekawa   370:       fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd);
                    371:       return(-1);
                    372:     }
                    373:     return(fd);
                    374:   }else{
                    375:     return(-1);
                    376:   }
                    377: }
                    378:

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