[BACK]Return to test1-tcp-rev.c CVS log [TXT][DIR] Up to [local] / OpenXM / doc / oxlib

Annotation of OpenXM/doc/oxlib/test1-tcp-rev.c, Revision 1.1

1.1     ! takayama    1: /* $OpenXM$
        !             2: /* A sample code to explain how to use ox_asir by TCP/IP and
        !             3:    OpenXM control protocol. ox_asir is called by the -revese option.
        !             4:    It computes the gcd of 12 and 8 by calling ox_asir server.
        !             5:    */
        !             6:
        !             7: #include <stdio.h>
        !             8: #include <sys/types.h>
        !             9: #include <sys/socket.h>
        !            10: #include <sys/time.h>
        !            11: #include <netinet/in.h>
        !            12: #include <netdb.h>
        !            13:
        !            14: #define OX_COMMAND          513
        !            15: #define OX_DATA             514
        !            16: #define SM_executeFunction  269
        !            17: #define SM_popString        263
        !            18: #define CMO_STRING            4
        !            19:
        !            20: static int Serial = 0;
        !            21:
        !            22: main() {
        !            23:   int fdControl = -1;
        !            24:   int fdStream = -1;
        !            25:   int portControl;
        !            26:   int portStream;
        !            27:   int m;
        !            28:   char com[1024];
        !            29:   char *s;
        !            30:   int controlByteOrder=0;
        !            31:   int engineByteOrder=0;
        !            32:   char *passControl = "1111";
        !            33:   char *passData = "2222";
        !            34:   int v = 1;
        !            35:
        !            36:   fdControl = socketOpen("localhost",0,&portControl);
        !            37:   fdStream = socketOpen("localhost",0,&portStream);
        !            38:
        !            39:   /* Starting the ox_asir server */
        !            40:   sprintf(com,"../../bin/ox -ox ../../bin/ox_asir -reverse -control %d -data %d -passControl 1111 -passData 2222 &",portControl,portStream);
        !            41:   fprintf(stderr,"system: %s\n",com);
        !            42:   system(com);
        !            43:
        !            44:
        !            45:   fdControl = socketAcceptLocal(fdControl);
        !            46:   fdStream  = socketAcceptLocal(fdStream);
        !            47:
        !            48:   fprintf(stderr,"\nControl fd %d : Connected.\n",fdControl);
        !            49:   fprintf(stderr,"\nStream fd %d : Connected.\n",fdStream);
        !            50:
        !            51:   if (fdStream == -1 || fdControl == -1) {
        !            52:     fprintf(stderr,"\nOpen error in oxCreateClient2.\n");
        !            53:     fprintf(stderr,"fdStream=%d, fdControl=%d\n",fdStream,fdControl);
        !            54:     return(NULL);
        !            55:   }
        !            56:
        !            57:   /* Authentication by password. */
        !            58:   m = strlen(passControl)+strlen(passData);
        !            59:   if (m > 0) {
        !            60:     s = (char *)malloc(sizeof(char)*(m+1));
        !            61:     m = strlen(passControl); s[0] = 0;
        !            62:     read(fdControl,s,m+1); s[m] = '\0';
        !            63:     if (strcmp(s,passControl) != 0) {
        !            64:       fprintf(stderr,"s=%s, passControl=%s\n",s,passControl);
        !            65:       fprintf(stderr,"password authentication failed for control channel.\n");
        !            66:       close(fdControl);
        !            67:       return(NULL);
        !            68:     }
        !            69:     m = strlen(passData); s[0] = 0;
        !            70:     read(fdStream,s,m+1); s[m] = '\0';
        !            71:     if (strcmp(s,passData) != 0) {
        !            72:       fprintf(stderr,"s=%s, passData=%s\n",s,passData);
        !            73:       fprintf(stderr,"password authentication failed for data channel.\n");
        !            74:       close(fdStream);
        !            75:       return(NULL);
        !            76:     }
        !            77:   }
        !            78:
        !            79:   controlByteOrder = oxSetByteOrder(fdControl);
        !            80:   if (v) fprintf(stderr,"Byte order for control process is %s.\n",
        !            81:                  (controlByteOrder == 0? "network byte order":
        !            82:                   (controlByteOrder == 1? "little indican":
        !            83:                    "big indian")));
        !            84:   engineByteOrder = oxSetByteOrder(fdStream);
        !            85:   if (v) fprintf(stderr,"Byte order for engine process is %s.\n",
        !            86:                  (engineByteOrder == 0? "network byte order":
        !            87:                   (engineByteOrder == 1? "little indican":
        !            88:                    "big indian")));
        !            89:
        !            90:
        !            91:   hoge(fdStream);
        !            92:   fprintf(stderr,"Type in ctrl-C"); fflush(NULL);
        !            93:   sleep(1000);
        !            94: }
        !            95:
        !            96: ox_push_cmo(int dataPort,char *cmo,int length) {
        !            97:   extern int Serial;
        !            98:   char oxtag[4];
        !            99:   char oxserial[4];
        !           100:   *((int *)oxtag) = (int) htonl(OX_DATA);
        !           101:   *((int *)oxserial) = (int) htonl(Serial++);
        !           102:   write(dataPort,oxtag,4);
        !           103:   write(dataPort,oxserial,4);
        !           104:   write(dataPort,cmo,length);
        !           105:   fflush(NULL);
        !           106: }
        !           107:
        !           108: ox_push_cmd(int dataPort, int cmd) {
        !           109:   extern int Serial;
        !           110:   char oxtag[4];
        !           111:   char oxserial[4];
        !           112:   char oxcmd[4];
        !           113:   *((int *)oxtag) = (int) htonl(OX_COMMAND);
        !           114:   *((int *)oxserial) = (int) htonl(Serial++);
        !           115:   *((int *)oxcmd) = (int) htonl(cmd);
        !           116:   write(dataPort,oxtag,4);
        !           117:   write(dataPort,oxserial,4);
        !           118:   write(dataPort,oxcmd,4);
        !           119:   fflush(NULL);
        !           120: }
        !           121:
        !           122: ox_pop_int32(int dataPort) {
        !           123:   char s[4];
        !           124:   read(dataPort,s,4);
        !           125:   return((int) ntohl( *((int *)s)));
        !           126: }
        !           127:
        !           128: ox_pop_string(int dataPort,char *buf,int limit) {
        !           129:   int oxtag, serial, cmotag, length ;
        !           130:
        !           131:   ox_push_cmd(dataPort,SM_popString);
        !           132:   oxtag = ox_pop_int32(dataPort);  /* It must be CMO_DATA */
        !           133:   serial = ox_pop_int32(dataPort); /* Read the serial number */
        !           134:   cmotag = ox_pop_int32(dataPort); /* Read the CMO tag */
        !           135:   if (cmotag != CMO_STRING) {
        !           136:        fprintf(stderr,"The tag should be CMO_STRING.\n");
        !           137:        exit(1);
        !           138:   }
        !           139:   length = ox_pop_int32(dataPort);
        !           140:   if (length > limit-1) {
        !           141:        fprintf(stderr,"Too long string to read.\n");
        !           142:        exit(1);
        !           143:   }
        !           144:   read(dataPort,buf,length);
        !           145:   buf[length]=0;
        !           146: }
        !           147:
        !           148: #define SIZE 1024
        !           149: hoge(int dataPort) {
        !           150:   char s[SIZE];
        !           151:   /*  (CMO_ZZ,12); */
        !           152:   unsigned char cmo0[]=
        !           153:   {00, 00, 00, 0x14,
        !           154:    00, 00, 00, 01, 00, 00, 00, 0xc};
        !           155:
        !           156:   /* (CMO_ZZ,8) */
        !           157:   unsigned char cmo1[] =
        !           158:   {00, 00, 00, 0x14,
        !           159:    00, 00, 00, 01, 00, 00, 00, 8};
        !           160:
        !           161:   /* (CMO_INT32,2); */
        !           162:   unsigned char cmo2[] =
        !           163:   { 00, 00, 00, 02, 00, 00, 00, 02};
        !           164:
        !           165:   /* (CMO_STRING,"igcd") */
        !           166:   unsigned char cmo3[] =
        !           167:   {00, 00, 00, 04, 00, 00, 00, 04,
        !           168:    0x69,0x67,0x63,0x64 };
        !           169:
        !           170:
        !           171:   ox_push_cmo(dataPort,cmo0,12);
        !           172:   ox_push_cmo(dataPort,cmo1,12);
        !           173:   ox_push_cmo(dataPort,cmo2,8);
        !           174:   ox_push_cmo(dataPort,cmo3,12);
        !           175:
        !           176:   ox_push_cmd(dataPort,SM_executeFunction);
        !           177:   ox_pop_string(dataPort,s,SIZE);
        !           178:
        !           179:   printf("gcd of 12 and 8, in the string format, is \n");
        !           180:   printf("%s",s);
        !           181:   printf("\n");
        !           182: }
        !           183:
        !           184:
        !           185: #define SET_TCPIOERROR  { if (TcpioError == NULL) TcpioError = stdout; }
        !           186: FILE *TcpioError = NULL;
        !           187: int Quiet = 0;
        !           188: extern int errno;
        !           189: errorMsg1s(char *s) { fprintf(stderr,"%s\n",s); }
        !           190: socketAcceptLocal(int snum) {
        !           191:   int s, news;
        !           192:   struct sockaddr peer;
        !           193:   int len;
        !           194:   int i;
        !           195:
        !           196:   SET_TCPIOERROR;
        !           197:   s = snum;
        !           198:   if (!Quiet) {fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);}
        !           199:   len = sizeof(struct sockaddr);
        !           200:   if ((news = accept(s,&peer,&len)) < 0) {
        !           201:     errorMsg1s("Error in accept. Retrying");
        !           202:     /* Code added for strange behavior on cygwin. */
        !           203:    if ((news = accept(s,&peer,&len)) < 0) {
        !           204:       errorMsg1s("Error in accept. Retry failed.");
        !           205:       return (-1);
        !           206:    }
        !           207:   }
        !           208:
        !           209:   len = sizeof(struct sockaddr);
        !           210:   getpeername(news,&peer,&len);
        !           211:   if (!Quiet) printf("len= %d\n",len);
        !           212:   for (i=0; i<len; i++) {
        !           213:     if (!Quiet) printf(" %x ",peer.sa_data[i]);
        !           214:   }
        !           215:   if (!Quiet) printf("\n");
        !           216:   if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 &&
        !           217:       peer.sa_data[4] == 0    && peer.sa_data[5] == 1) {
        !           218:     if (!Quiet) fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n");
        !           219:   }else{
        !           220:     errorMsg1s("Authentication: The connection is not from the localhost.");
        !           221:     close(s);
        !           222:     fprintf(stderr,"The connection is refused.");
        !           223:     return(-1);
        !           224:   }
        !           225:
        !           226:   if (!Quiet) {fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);}
        !           227:   if (close(s) < 0) {
        !           228:     errorMsg1s("Error in closing the old socket.");
        !           229:     return(-1);
        !           230:   }
        !           231:   return(news);
        !           232: }
        !           233:
        !           234: socketOpen(char *serverName,int portNumber,int *portNumberp) {
        !           235:   static struct hostent *myhost;
        !           236:   static struct sockaddr_in me;
        !           237:   static int s_waiting;
        !           238:   static int on;
        !           239:   int tt;
        !           240:
        !           241:   SET_TCPIOERROR;
        !           242:   if (!Quiet) fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n",
        !           243:           serverName,portNumber);
        !           244:   if ((myhost = gethostbyname(serverName)) == NULL) {
        !           245:     errorMsg1s("Bad server name.");
        !           246:     return(-1);
        !           247:   }
        !           248:   bzero((char *)&me,sizeof(me));
        !           249:   me.sin_family = AF_INET;
        !           250:   me.sin_port = htons(portNumber);
        !           251:   bcopy(myhost->h_addr,
        !           252:         &me.sin_addr,myhost->h_length);
        !           253:
        !           254:   if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
        !           255:     errorMsg1s("Socket allocation is failed.");
        !           256:     return(-1);
        !           257:   }
        !           258:
        !           259:   on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
        !           260:   /* important */
        !           261:   if ((tt = bind(s_waiting,(struct sockaddr *) &me,sizeof(me))) == -1) {
        !           262:     fprintf(TcpioError,"Bind error. Error no is %d. See /usr/include/sys/errno.h. (asm/errno.h)\n",errno);
        !           263:     errorMsg1s("cannot bind");
        !           264:     return(-1);
        !           265:   }
        !           266:   /* printf("bind returns %d\n",tt); */
        !           267:   tt = sizeof(me);
        !           268:   if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
        !           269:     fprintf(TcpioError,"getsockname error. Error no is %d. See /usr/include/sys/errno.h (asm/errno.h).\n",errno);
        !           270:     errorMsg1s("cannot getsockname");
        !           271:     return(-1);
        !           272:   }
        !           273:
        !           274:   if (listen(s_waiting,16) < 0) {
        !           275:     errorMsg1s("Listen failed");
        !           276:     return(-1);
        !           277:   }
        !           278:   if (!Quiet) fprintf(TcpioError,"Done the initialization. port =%d\n",ntohs(me.sin_port));
        !           279:   *portNumberp = ntohs(me.sin_port);
        !           280:   return(s_waiting);
        !           281: }
        !           282:
        !           283: int oxSetByteOrder(int fd) {
        !           284:   char data[1];
        !           285:   int peertype;
        !           286:   read(fd,data,1);
        !           287:   peertype = (unsigned char) data[0];
        !           288:
        !           289:   /* We support only Network byte order */
        !           290:   data[0] = 0;
        !           291:   write(fd,data,1);
        !           292:
        !           293:   return(0);
        !           294: }
        !           295:

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