Annotation of OpenXM/doc/oxlib/test1-tcp-rev.c, Revision 1.2
1.2 ! fujimoto 1: /* $OpenXM: OpenXM/doc/oxlib/test1-tcp-rev.c,v 1.1 2005/11/12 02:38:01 takayama Exp $
1.1 takayama 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>
1.2 ! fujimoto 8: #include <errno.h>
1.1 takayama 9: #include <sys/types.h>
10: #include <sys/socket.h>
11: #include <sys/time.h>
12: #include <netinet/in.h>
13: #include <netdb.h>
14:
15: #define OX_COMMAND 513
16: #define OX_DATA 514
17: #define SM_executeFunction 269
18: #define SM_popString 263
19: #define CMO_STRING 4
20:
21: static int Serial = 0;
22:
23: main() {
24: int fdControl = -1;
25: int fdStream = -1;
26: int portControl;
27: int portStream;
28: int m;
29: char com[1024];
30: char *s;
31: int controlByteOrder=0;
32: int engineByteOrder=0;
33: char *passControl = "1111";
34: char *passData = "2222";
35: int v = 1;
36:
37: fdControl = socketOpen("localhost",0,&portControl);
38: fdStream = socketOpen("localhost",0,&portStream);
39:
40: /* Starting the ox_asir server */
41: sprintf(com,"../../bin/ox -ox ../../bin/ox_asir -reverse -control %d -data %d -passControl 1111 -passData 2222 &",portControl,portStream);
42: fprintf(stderr,"system: %s\n",com);
43: system(com);
44:
45:
46: fdControl = socketAcceptLocal(fdControl);
47: fdStream = socketAcceptLocal(fdStream);
48:
49: fprintf(stderr,"\nControl fd %d : Connected.\n",fdControl);
50: fprintf(stderr,"\nStream fd %d : Connected.\n",fdStream);
51:
52: if (fdStream == -1 || fdControl == -1) {
53: fprintf(stderr,"\nOpen error in oxCreateClient2.\n");
54: fprintf(stderr,"fdStream=%d, fdControl=%d\n",fdStream,fdControl);
1.2 ! fujimoto 55: return(0);
1.1 takayama 56: }
57:
58: /* Authentication by password. */
59: m = strlen(passControl)+strlen(passData);
60: if (m > 0) {
61: s = (char *)malloc(sizeof(char)*(m+1));
62: m = strlen(passControl); s[0] = 0;
63: read(fdControl,s,m+1); s[m] = '\0';
64: if (strcmp(s,passControl) != 0) {
65: fprintf(stderr,"s=%s, passControl=%s\n",s,passControl);
66: fprintf(stderr,"password authentication failed for control channel.\n");
67: close(fdControl);
1.2 ! fujimoto 68: return(0);
1.1 takayama 69: }
70: m = strlen(passData); s[0] = 0;
71: read(fdStream,s,m+1); s[m] = '\0';
72: if (strcmp(s,passData) != 0) {
73: fprintf(stderr,"s=%s, passData=%s\n",s,passData);
74: fprintf(stderr,"password authentication failed for data channel.\n");
75: close(fdStream);
1.2 ! fujimoto 76: return(0);
1.1 takayama 77: }
78: }
79:
80: controlByteOrder = oxSetByteOrder(fdControl);
81: if (v) fprintf(stderr,"Byte order for control process is %s.\n",
82: (controlByteOrder == 0? "network byte order":
83: (controlByteOrder == 1? "little indican":
84: "big indian")));
85: engineByteOrder = oxSetByteOrder(fdStream);
86: if (v) fprintf(stderr,"Byte order for engine process is %s.\n",
87: (engineByteOrder == 0? "network byte order":
88: (engineByteOrder == 1? "little indican":
89: "big indian")));
90:
91:
92: hoge(fdStream);
93: fprintf(stderr,"Type in ctrl-C"); fflush(NULL);
94: sleep(1000);
95: }
96:
97: ox_push_cmo(int dataPort,char *cmo,int length) {
98: extern int Serial;
99: char oxtag[4];
100: char oxserial[4];
101: *((int *)oxtag) = (int) htonl(OX_DATA);
102: *((int *)oxserial) = (int) htonl(Serial++);
103: write(dataPort,oxtag,4);
104: write(dataPort,oxserial,4);
105: write(dataPort,cmo,length);
106: fflush(NULL);
107: }
108:
109: ox_push_cmd(int dataPort, int cmd) {
110: extern int Serial;
111: char oxtag[4];
112: char oxserial[4];
113: char oxcmd[4];
114: *((int *)oxtag) = (int) htonl(OX_COMMAND);
115: *((int *)oxserial) = (int) htonl(Serial++);
116: *((int *)oxcmd) = (int) htonl(cmd);
117: write(dataPort,oxtag,4);
118: write(dataPort,oxserial,4);
119: write(dataPort,oxcmd,4);
120: fflush(NULL);
121: }
122:
123: ox_pop_int32(int dataPort) {
124: char s[4];
125: read(dataPort,s,4);
126: return((int) ntohl( *((int *)s)));
127: }
128:
129: ox_pop_string(int dataPort,char *buf,int limit) {
130: int oxtag, serial, cmotag, length ;
131:
132: ox_push_cmd(dataPort,SM_popString);
133: oxtag = ox_pop_int32(dataPort); /* It must be CMO_DATA */
134: serial = ox_pop_int32(dataPort); /* Read the serial number */
135: cmotag = ox_pop_int32(dataPort); /* Read the CMO tag */
136: if (cmotag != CMO_STRING) {
137: fprintf(stderr,"The tag should be CMO_STRING.\n");
138: exit(1);
139: }
140: length = ox_pop_int32(dataPort);
141: if (length > limit-1) {
142: fprintf(stderr,"Too long string to read.\n");
143: exit(1);
144: }
145: read(dataPort,buf,length);
146: buf[length]=0;
147: }
148:
149: #define SIZE 1024
150: hoge(int dataPort) {
151: char s[SIZE];
152: /* (CMO_ZZ,12); */
153: unsigned char cmo0[]=
154: {00, 00, 00, 0x14,
155: 00, 00, 00, 01, 00, 00, 00, 0xc};
156:
157: /* (CMO_ZZ,8) */
158: unsigned char cmo1[] =
159: {00, 00, 00, 0x14,
160: 00, 00, 00, 01, 00, 00, 00, 8};
161:
162: /* (CMO_INT32,2); */
163: unsigned char cmo2[] =
164: { 00, 00, 00, 02, 00, 00, 00, 02};
165:
166: /* (CMO_STRING,"igcd") */
167: unsigned char cmo3[] =
168: {00, 00, 00, 04, 00, 00, 00, 04,
169: 0x69,0x67,0x63,0x64 };
170:
171:
172: ox_push_cmo(dataPort,cmo0,12);
173: ox_push_cmo(dataPort,cmo1,12);
174: ox_push_cmo(dataPort,cmo2,8);
175: ox_push_cmo(dataPort,cmo3,12);
176:
177: ox_push_cmd(dataPort,SM_executeFunction);
178: ox_pop_string(dataPort,s,SIZE);
179:
180: printf("gcd of 12 and 8, in the string format, is \n");
181: printf("%s",s);
182: printf("\n");
183: }
184:
185:
186: #define SET_TCPIOERROR { if (TcpioError == NULL) TcpioError = stdout; }
187: FILE *TcpioError = NULL;
188: int Quiet = 0;
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>