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


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