lib/clib/vxwserv.c
Go to the documentation of this file.
1 /* vxwserv.c
2 /* spawned from server
3 /* This program is supposed to run on any vxworks machines, not Lisp.
4 */
5 
6 #include "vxWorks.h"
7 #include "stdioLib.h"
8 #include "socket.h"
9 #include "in.h"
10 #include "sigLib.h"
11 #include "taskLib.h"
12 
13 #include "vxw_proto.h"
14 
15 #define SERVER_NUM 2200
16 #define ARGUMENT_BUFFER_SIZE 16384
17 
18 extern char *sysSymTbl;
19 
20 static int error_code;
21 
22 /****************************************************************/
23 
25 int x;
26 { printf("VxWs protocol error %d\n",x);
27  error_code=x; }
28 
29 static getint(type, s)
30 enum argument_type type;
31 register FILE *s;
32 { register int ival=0;
33  switch(type) {
34  A_LONG: ival=(getc(s)<<24)+(getc(s)<<16);
35  A_SHORT: ival+=(getc(s)<<8);
36  A_CHAR: return(ival+getc(s));}
37  }
38 
39 static double getfloat(type, s)
40 enum argument_type type;
41 register FILE *s;
42 { double dval=0.0;
43  number_union nu;
44  switch(type) {
45  case A_FLOAT: nu.i.ival1=getw(s); return(nu.f.fval1);
46  case A_DOUBLE: nu.i.ival1=getw(s); nu.i.ival2=getw(s); return(nu.dval);
47  }}
48 
49 static getlength(s)
50 FILE *s;
51 { int len;
52  len=getc(s);
53  if (len & 0x80) {
54  if (len & 0x40) /*32bit length*/
55  return(((len & 0x3f)<<24) + (getc(s)<<16) + (getc(s)<<8) + getc(s));
56  else return(((len & 0x7f)<<8) + getc(s)); }
57  else return(len);}
58 
59 static int getvector(s,vmem,index,bufsize)
60 register FILE *s;
61 register unsigned char *vmem;
62 register int index;
63 int bufsize;
64 { register int i,length;
65  length=getlength(s);
66  if (index+length >= bufsize-100) {
67  protocol_error(3); /* vector buffer overflow*/
68  for (i=0; i<length; i++) getc(s); /*skip vector elements*/
69  return(index);}
70  for (i=0; i<length; i++) vmem[index++]=getc(s);
71  i=4-(length % 4);
72  while (i-- >0) vmem[index++] =0; /*align to long word boundary */
73  return(index);}
74 
76 char *s;
77 { int stat, entry, entry_type;
78  if (symFindByName(sysSymTbl, s, &entry, &entry_type) == OK) return(entry);
79  else return(ERROR);}
80 
81 static int getsymbol(s)
82 register FILE *s;
83 { char symstr[256];
84  register int i,len;
85  int stat, entry, entry_type;
86 
87  len=getlength(s);
88  for (i=0; i<len; i++) symstr[i+1]=getc(s);
89  symstr[len+1]=0;
90  stat=symFindByName(sysSymTbl, &symstr[1], &entry, &entry_type);
91  if (stat==ERROR) {
92  symstr[0]='_';
93  stat=symFindByName(sysSymTbl, symstr, &entry, &entry_type);
94  if (stat==ERROR) return(ERROR);}
95  return(entry);}
96 
97 int call_vxw(fd)
98 int fd;
99 { FILE *s, *ws;
100  int bufsize, running=1, callee, tid, result_type;
101  int nextfree, i, j, len; /*argument counter*/
102  int (*ifunc)();
103  double (*ffunc)();
104  double f;
105  enum argument_type type;
106  long cargv[32];
107  number_union nu;
108  unsigned char *ip, *vmem;
109 
110  if ((s=fdopen(fd,"r"))==NULL) { printf("cannot fdopen read\n"); exit(2);}
111  if ((ws=fdopen(fd,"w"))==NULL) { printf("cannot fdopen write\n"); exit(2);}
112 
113  tid=taskIdSelf();
114  taskVarAdd(tid, &error_code);
115  bufsize=getw(s);
116  vmem = malloc(bufsize);
117  if (malloc==NULL) { protocol_error(7); return(7);}
118 
119  printf("VxWserv v1.0 started with %d byte buffer\n", bufsize);
120 
121  while (running) {
122  nextfree=i=j=0; error_code=0;
123  type=getc(s);
124  if (type==EOF) { protocol_error(5); break;}
125  if (type==A_SYMBOL) {
126  callee=getsymbol(s);
127  if (callee==ERROR) protocol_error(1); }
128  else if (type==A_LONG) callee=getw(s);
129  else protocol_error(2);
130 
131 /* printf("entry=0x%x\n", callee); */
132 
133  type=getc(s);
134  while (type!=A_END) {
135  if (j>=32) protocol_error(5);
136  switch(type) {
137  case A_CHAR: cargv[i++]=getc(s); break;
138  case A_SHORT: cargv[i++]=(getc(s)<<8) + getc(s); break;
139  case A_LONG: cargv[i++]=getw(s); break;
140  case A_FLOAT: len=getw(s); nu.i.ival1=len;
141  nu.dval=nu.f.fval1;
142  cargv[i++]=nu.i.ival1; cargv[i++]=nu.i.ival2;
143  break;
144  case A_SYMBOL: cargv[i++]=getsymbol(s); break;
145  case A_VECTOR: cargv[i++]= &vmem[nextfree];
146  nextfree=getvector(s,vmem,nextfree,bufsize);
147  break;
148  default: protocol_error(4); break; }
149  j++;
150  type=getc(s); /*next arg type*/
151  if (type==EOF) protocol_error(6);
152  } /*end of reading arguments*/
153 
154  result_type=getc(s);
155 
156  /* protocol analysis finished, return the status*/
157  putc(error_code, ws);
158  if (error_code==5) break; /*communication broke down*/
159 
160  if (error_code!=0) { /*error occurred, abort invokation*/ }
161  else if (result_type==A_FLOAT || result_type==A_DOUBLE) {
162  ffunc=callee;
163  if (j<=8)
164  f=(*ffunc)(cargv[0],cargv[1],cargv[2],cargv[3],
165  cargv[4],cargv[5],cargv[6],cargv[7]);
166  else if (j<=32)
167  f=(*ffunc)(cargv[0],cargv[1],cargv[2],cargv[3],
168  cargv[4],cargv[5],cargv[6],cargv[7],
169  cargv[8],cargv[9],cargv[10],cargv[11],
170  cargv[12],cargv[13],cargv[14],cargv[15],
171  cargv[16],cargv[17],cargv[18],cargv[19],
172  cargv[20],cargv[21],cargv[22],cargv[23],
173  cargv[24],cargv[25],cargv[26],cargv[27],
174  cargv[28],cargv[29],cargv[30],cargv[31]);
175 /* printf("result=%lf\n",f); */
176  nu.f.fval1=f; /*coerce double to float*/
177  putw(nu.i.ival1, ws);}
178  else {
179  ifunc=callee;
180  if (j<8)
181  i=(*ifunc)(cargv[0],cargv[1],cargv[2],cargv[3],
182  cargv[4],cargv[5],cargv[6],cargv[7]);
183  else if (j<=32)
184  i=(*ifunc)(cargv[0],cargv[1],cargv[2],cargv[3],
185  cargv[4],cargv[5],cargv[6],cargv[7],
186  cargv[8],cargv[9],cargv[10],cargv[11],
187  cargv[12],cargv[13],cargv[14],cargv[15],
188  cargv[16],cargv[17],cargv[18],cargv[19],
189  cargv[20],cargv[21],cargv[22],cargv[23],
190  cargv[24],cargv[25],cargv[26],cargv[27],
191  cargv[28],cargv[29],cargv[30],cargv[31]);
192 /* printf("result=%d\n",i); */
193  if (result_type==A_CHAR || result_type==A_SHORT || result_type==A_LONG)
194  putw(i,ws);
195  else if (result_type==A_VECTOR) {
196  len=getlength(s); ip=(unsigned char *) i;
197  for (j=0; j<len; j++) { putc(*ip, ws); ip++;}
198  }
199  }
200  fflush(ws);} /*running*/
201 
202  fclose(s); free(vmem);
203  printf("connection closed\n"); }
204 
205 /* spawn a new server */
206 
207 
208 static int sock; /* socket fd's */
209 
210 int spawn_vxw(fd)
211 int fd;
212 { char taskname[16];
213  int stat,entry,entry_type;
214  static int task_id=1;
215  unsigned char option,priority;
216 
217  read(fd, &priority,1);
218  read(fd, &option, 1);
219  sprintf(taskname,"VxWs_%d",task_id++);
220  stat=taskSpawn(taskname, priority, option, 3000,call_vxw,fd);
221  if (stat==ERROR) { printf("cannot spawn\n"); return(ERROR); }
222  else return(OK);}
223 
224 static void abort(sig)
225 int sig;
226 { printf("vxwserv aborting\n");
227  close(sock);
228  exit(sig);}
229 
231  { int snew;
232  struct sockaddr_in serverAddr; /* server's address */
233  struct sockaddr_in clientAddr; /* client's address */
234  int client_len; /* length of clientAddr */
235  int connection_count=0;
236  unsigned char special;
237  SIGVEC sigabort;
238 
239  sigabort.sv_handler = abort;
240  sigabort.sv_mask = !SIGMASK(SIGINT);
241  sigabort.sv_flags= 0;
242  sigvec(SIGINT, &sigabort, 0);
243 
244  /* Zero out the sock_addr structures.
245  * This MUST be done before the socket calls. */
246 
247  bzero (&serverAddr, sizeof (serverAddr));
248  bzero (&clientAddr, sizeof (clientAddr));
249 
250  sock = socket (AF_INET, SOCK_STREAM, 0);
251  if (sock == ERROR) exit (1);
252  else printf("listening on socket %d\n",sock);
253  /* Set up our internet address, and bind it so the client can connect. */
254 
255  serverAddr.sin_family = AF_INET;
256  serverAddr.sin_port = SERVER_NUM;
257 
258  while (bind (sock, (SOCKADDR *)&serverAddr, sizeof(serverAddr))==ERROR) {
259  printf ("bind failed, errno = %d\n", errnoGet());
260  serverAddr.sin_port++; }
261  printf ("port %d is bound.\n", serverAddr.sin_port);
262  /* Listen, for the client to connect to us. */
263  if (listen (sock, 2) == ERROR) {
264  printf ("listen failed\n");
265  close (sock);
266  exit (1);}
267 
268  /* accept-fork loop */
269  while (1) {
270  client_len = sizeof (clientAddr);
271  snew = accept (sock, (SOCKADDR *)&clientAddr, &client_len);
272  if (snew==ERROR) { printf("accept failed\n"); close(sock); break;}
273  printf("CLIENT accepted: sock=%d port=%d family=%d addr=%lx\n",
274  snew,
275  clientAddr.sin_port, clientAddr.sin_family,
276  clientAddr.sin_addr.s_addr);
277  connection_count++ ;
278  read(snew, &special, 1); /*ignore first byte*/
279  if (spawn_vxw(snew)==ERROR)
280  { printf("spawn failed\n"); } ;
281  }
282  printf ("totally %d connections established\n",connection_count);
283  close (sock); }
f
static int getvector(FILE *s, unsigned char *vmem, int index, int bufsize)
static void abort(int sig)
int spawn_vxw(int fd)
#define SERVER_NUM
int findsymbol(char *s)
void protocol_error(int x)
char * sysSymTbl
static void special(int k, int x, int y)
Definition: dinoshade.c:765
static getint(enum argument_type type, FILE *s)
int call_vxw(int fd)
thread_t tid
Definition: collector.c:50
struct number_union::@4 f
struct number_union::@5 i
short s
Definition: structsize.c:2
vxwserv()
static int sock
char * index(char *sp, char c)
Definition: eustags.c:1631
#define NULL
Definition: transargv.c:8
static int error_code
static int getsymbol(FILE *s)
static getlength(FILE *s)
static double getfloat(enum argument_type type, FILE *s)


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 6 2019 20:00:44