1 /*
2 Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9     - Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11 
12     - Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the
15       distribution.
16 
17     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
18       names of its contributors may be used to endorse or promote products
19       derived from this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifndef FRICAS_COM_H_INCLUDED
35 #define FRICAS_COM_H_INCLUDED
36 
37 #ifdef __MINGW32__
38 #  include <winsock2.h>
39 #else
40 #  include <sys/types.h>
41 #  include <sys/socket.h>
42 #  include <netinet/in.h>
43 #endif
44 
45 #include "fricas_c_macros.h"
46 
47 /* On Windows, a socket identifier is not a file descriptor.  It is
48    represented by an integer type, but that integer type is not just
49    plain int as in the Unix world.  It is an unsigned integer.
50    Consequently, we abstract over that variation, using the typedef
51    fricas_socket.  */
52 
53 #ifdef __MINGW32__
54 typedef SOCKET fricas_socket;
55 #else
56 typedef int fricas_socket;
57 #endif
58 
59 
60 /* Close a socket communication endpoint.  */
61 extern void fricas_close_socket(fricas_socket);
62 
63 typedef struct {
64   fricas_socket socket;  /* socket number returned by "socket" call */
65   int type;             /* socket type (AF_UNIX or AF_INET) */
66   int purpose;          /* can be SessionManager, GraphicsServer, etc. */
67   int pid;              /* process ID of connected socket */
68   int frame;            /* spad interpreter frame (for interpreter windows) */
69   fricas_socket remote;  /* file descriptor of remote socket */
70   union {
71     struct sockaddr u_addr;
72     struct sockaddr_in i_addr;
73     char pad[32];
74   } addr;
75   char *host_name;      /* name of foreign host if type == AF_INET */
76 } Sock;
77 
78 
79 #define MaxClients      150
80 
81 /* possible socket types (purpose) */
82 
83 #define SessionManager  1
84 #define ViewportServer  2
85 #define MenuServer      3
86 #define SessionIO       4
87 #define BaloonServer    5
88 #define InterpWindow    6
89 #define KillSpad        7
90 #define DebugWindow     8  /* used for nagman */
91 #define Forker          9
92 #define AV              10 /*Simon's algebraic viewer */
93 
94 #define Acknowledge     255
95 
96 /* Timeout value for connection to remote socket */
97 
98 #define Forever 0
99 
100 /* Socket name for local FRICAS server and session manager */
101 
102 #define SpadServer              "/tmp/.d"
103 #define SessionServer           "/tmp/.s"
104 #define SessionIOName           "/tmp/.i"
105 #define MenuServerName          "/tmp/.h"
106 #define ForkServerName          "/tmp/.f"
107 
108 
109 #define MASK_SIZE       (NBBY*sizeof(fd_set))
110 
111 
112 /* table of dedicated socket types */
113 
114 extern Sock *purpose_table[];
115 extern Sock server[];
116 extern Sock clients[];
117 extern fd_set socket_mask;
118 extern fd_set server_mask;
119 
120 /* Commands sent over the FRICAS session manager or menu socket */
121 
122 #define CreateFrame             1
123 #define SwitchFrames            2
124 #define EndOfOutput             3
125 #define CallInterp              4
126 #define EndSession              5
127 #define LispCommand             6
128 #define SpadCommand             7
129 #define SendXEventToHyperTeX    8
130 #define QuietSpadCommand        9
131 #define CloseClient             10
132 #define QueryClients            11
133 #define QuerySpad               12
134 #define NonSmanSession          13
135 #define KillLispSystem          14
136 
137 #define CreateFrameAnswer       50
138 
139 /* Commands from FRICAS menu server to interpreter windows */
140 
141 #define ReceiveInputLine        100
142 #define TestLine                101
143 
144 
145 /* It is idiomatic in the Unix/POSIX world to use the standard
146    read() and write() functions on sockets.  However, in the Windows
147    world, that is invalid.  Consequently, portability suggests that
148    we restrict ourselves to the POSIX standard functions recv() and
149    send().  */
150 
151 static inline int
fricas_write(Sock * s,const char * buf,size_t n)152 fricas_write(Sock* s, const char* buf, size_t n)
153 {
154    return send(s->socket, buf, n, 0);
155 }
156 
157 static inline int
fricas_read(Sock * s,char * buf,size_t n)158 fricas_read(Sock* s, char* buf, size_t n)
159 {
160    return recv(s->socket, buf, n, 0);
161 }
162 
163 #endif
164