Annotation of OpenXM/src/kxx/oxmain.c, Revision 1.3
1.3 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.2 1999/10/30 02:24:27 takayama Exp $ */
1.1 maekawa 2: /* nullserver01 */
3: #include <stdio.h>
4: #include <sys/types.h>
5: #include <sys/socket.h>
6: #include <sys/time.h>
7: #include <netinet/in.h>
8: #include <netdb.h>
9: #include <signal.h>
10: #include <setjmp.h>
11: /* -lnsl -lsocket /usr/ucblib/libucb.a */
12: #include "ox_kan.h"
13: #include "serversm.h"
14:
1.2 takayama 15: #define SERVERNAME "/usr/local/OpenXM/bin/ox_sm1"
1.1 maekawa 16:
17: int OxCritical = 0;
18: int OxInterruptFlag = 0;
19:
20: int SerialCurrentControl;
21:
22: int MyServerPid;
23: char ServerName[1024];
24: int PacketMonitor = 0;
25: int Quiet = 0;
26:
27: int LocalMode = 1;
28: int NotifyPortnumber = 0;
29:
30:
31: main(int argc, char *argv[]) {
32: int fd;
33: int size;
34: char sname[1024];
35: int tmp[1];
36: char *buf;
37: int i;
38: int fdControl = -1; int portControl = 1200;
39: int fdStream = -1; int portStream = 1300;
40: int reverse = 0;
41: extern int OpenedSocket;
42: char portfile[1024];
43: char *pass;
44:
45: strcpy(sname,"localhost");
46: strcpy(ServerName,SERVERNAME);
47: i = 1;
1.2 takayama 48: if (argc == 1) {
49: oxmainUsage();
50: exit();
51: }
1.1 maekawa 52: while (i<argc) {
53: if (strcmp(argv[i],"-host") == 0) {
54: i++;
55: if (i<argc) strcpy(sname,argv[i]);
56: }else if (strcmp(argv[i],"-data")==0) {
57: i++;
58: if (i<argc) sscanf(argv[i],"%d",&portStream);
59: }else if (strcmp(argv[i],"-control")==0) {
60: i++;
61: if (i<argc) sscanf(argv[i],"%d",&portControl);
62: }else if (strcmp(argv[i],"-ox") == 0) {
63: i++;
64: if (i<argc) strcpy(ServerName,argv[i]);
65: }else if (strcmp(argv[i],"-monitor") == 0) {
66: PacketMonitor = 1;
67: }else if (strcmp(argv[i],"-insecure") == 0) {
68: LocalMode = 0;
69: }else if (strcmp(argv[i],"-reverse") == 0) {
70: reverse = 1;
71: }else if (strcmp(argv[i],"-portfile") == 0) {
72: i++;
73: if (i<argc) {
74: sscanf(argv[i],"%s",portfile);
75: portControl = 0;
76: portStream = 0;
77: NotifyPortnumber = 1;
78: }
79: }else if (strcmp(argv[i],"-pass") == 0) {
80: i++;
81: if (i<argc) {
82: pass = argv[i];
83: }
84: }else {
85: fprintf(stderr,"Unknown option %s.\n",argv[i]);
86: oxmainUsage(); exit(10);
87: }
88: i++;
89: }
90:
91: if (reverse) {
92: /* The order is very important. */
93: fdControl = socketConnectWithPass(sname,portControl,pass);
94: fdStream = socketConnectWithPass(sname,portStream,pass);
95:
96: fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream);
97: if (portControl != -1) {
98: MyServerPid = fork();
99: if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
100: else childServerMain(fdControl,fdStream);
101: }else{
102: childServerMain(fdControl,fdStream);
103: }
104: /* This line will be never executed. */
105: }
106:
107: /* non-reverse case. */
108: fprintf(stderr,"Hostname is %s \n",sname);
109: fprintf(stderr,"Port for data (-data) = %d \n",portStream);
110: fprintf(stderr,"Port for control message (-control) = %d \n",portControl);
111: fflush(NULL);
112:
113:
114: if (LocalMode) {
115: if (portControl != -1) {
116: fdControl = socketOpen(sname,portControl);
117: portControl = OpenedSocket;
118: if (NotifyPortnumber) {
119: oxWritePortFile(0,portControl,portfile);
120: }
121: fdControl = socketAcceptLocal(fdControl);
122: fprintf(stderr,"\n control port %d : Connected.\n",portControl);
123: }
124: if (portStream != -1) {
125: fdStream = socketOpen(sname,portStream);
126: portStream = OpenedSocket;
127: if (NotifyPortnumber) {
128: oxWritePortFile(1,portStream,portfile);
129: }
130: fdStream = socketAcceptLocal(fdStream);
131: fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
132: }
133: }else{
134: if (portControl != -1) {
135: fdControl = socketOpen(sname,portControl);
136: portControl = OpenedSocket;
137: if (NotifyPortnumber) {
138: oxWritePortFile(0,portControl,portfile);
139: }
140: fdControl = socketAccept(fdControl);
141: fprintf(stderr,"\n control port %d : Connected.\n",portControl);
142: }
143: if (portStream != -1) {
144: fdStream = socketOpen(sname,portStream);
145: portStream = OpenedSocket;
146: if (NotifyPortnumber) {
147: oxWritePortFile(1,portStream,portfile);
148: }
149: fdStream = socketAccept(fdStream);
150: fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
151: }
152: }
153:
154:
155:
156: if (portControl != -1) {
157: MyServerPid = fork();
158: if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
159: else childServerMain(fdControl,fdStream);
160: }else{
161: childServerMain(fdControl,fdStream);
162: }
163:
164: }
165:
166: oxmainUsage() {
167: fprintf(stderr,"Usage: \n");
168: fprintf(stderr," ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\n");
169: fprintf(stderr," [-insecure -portfile fname -reverse -pass xxxyyyzzz]");
170: fprintf(stderr,"\n");
1.2 takayama 171: fprintf(stderr,"-reverse: ox server connects to the client.\n");
172: fprintf(stderr," The client must give a one time password to ox server to connect to the client with -pass option.\n");
173: fprintf(stderr," The one time password can be seen by ps command, so you must not use this one time password system on an untrustful host.\n");
174: fprintf(stderr," The one time password should be sent by a safe communication line like ssh and the ox server should be started by ssh. Do not use rsh\n");
175: fprintf(stderr," If -reverse is not given, the client connect to the ox server\n");
176: fprintf(stderr," See OpenXM/src/SSkan/Doc/ox.sm1, /sm1connectr\n");
177: fprintf(stderr,"-insecure : \n");
178: fprintf(stderr," If you access to the server from a localhost, you do not need one time password. However, if you access outside of the localhost, a one time password is required. To turn off this restriction, -insecure option is used.\n");
179: fprintf(stderr,"Example 1:\n");
1.3 ! takayama 180: fprintf(stderr,"(Start the ox server): dc1%% ox -ox ~/OpenXM/bin/ox_sm1 -host dc1.math.kobe-u.ac.jp -insecure -control 1200 -data 1300\n");
1.2 takayama 181: fprintf(stderr,"(client): sm1\n ");
182: fprintf(stderr," (ox.sm1) run ; \n");
1.1 maekawa 183: fprintf(stderr," sm1>[(dc1.math.kobe-u.ac.jp) 1300 1200] oxconnect /ox.ccc set\n");
1.2 takayama 184: fprintf(stderr,"Example 2:\n");
185: fprintf(stderr,"(Start the ox server): dc1%% ox -ox ~/OpenXM/bin/ox_sm1\n");
186: fprintf(stderr,"(client): dc1%% sm1\n ");
187: fprintf(stderr," (ox.sm1) run ; \n");
188: fprintf(stderr," sm1>[(localhost) 1300 1200] oxconnect /ox.ccc set\n");
1.1 maekawa 189: fprintf(stderr,"\n");
190: }
191:
192: parentServerMain(int fdControl, int fdStream) {
193: int id;
194: int mtag;
195: int size;
196: int n;
197: int r;
198: int message = 1;
199: int controlByteOrder;
200:
201: extern void myServerExit();
202:
203: controlByteOrder = oxTellMyByteOrder(fdControl);
204: /* Set the network byte order. */
205: fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
206:
207:
208: signal(SIGINT,myServerExit);
209: while(1) {
210: mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);
211: /* get the message_tag */
212: /* message_body */
213: id = oxfdGetInt32(fdControl); /* get the function_id */
214: if (message) {printf("\n[control] control function_id is %d\n",id);}
215: switch( id ) {
216: case SM_control_kill:
217: if (message) printf("[control] control_kill\n");
218: oxSendResultOfControl(fdControl);
219: sleep(2);
220: myServerExit();
221: break;
222: case SM_control_reset_connection:
223: if (message) printf("[control] control_reset_connection.\n");
224: if (message) printf("Sending the SIGUSR1 signal to %d: ",MyServerPid);
225: r=kill(MyServerPid,SIGUSR1);
226: if (message) printf("Result = %d\n",r);
227: fflush(NULL);
228: oxSendResultOfControlInt32(fdControl,0);
229: break;
230: default:
231: fprintf(stderr,"[control] Unknown control message.\n");
232: fprintf(stderr,"Shutdown the server.");
233: myServerExit();
234: break;
235: }
236: }
237: }
238:
239: void myServerExit() {
240: printf("Sending the kill signal to the child.\n");
241: kill(MyServerPid,SIGKILL);
242: exit();
243: }
244:
245: childServerMain(int fdControl, int fdStream) {
246: int i;
247: close(fdControl); /* close(0); dup(fdStream); */
248: dup2(fdStream,3);
249: dup2(fdStream,4);
250: /*close(0);
251: #include <sys/param.h>
252: for (i=5; i<NOFILE; i++) close(i);
253: */
254: if (PacketMonitor) {
255: if (execl(ServerName,ServerName,"-monitor",NULL)) {
256: fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);
257: }
258: }else {
259: if (execl(ServerName,ServerName,NULL)) {
260: fprintf(stderr,"%s cannot be executed.\n",ServerName);
261: }
262: }
263: }
264:
265:
266: /* These are dummy. It is defined in stackmachine.c */
267: unlockCtrlCForOx() { ; }
268: restoreLockCtrlCForOx() { ; }
269:
270:
271:
272:
273:
274:
275:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>