1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* IvyDaemon, an Ivy gateway for short-lived agents
*
* Copyright (C) 1999
* Centre d'Études de la Navigation Aérienne
*
* Main file for the client ivyecho
*
* Author(s): Stephane Chatty <chatty@cenatoulouse.dgac.fr>
* from code by Patrick Amar <pa@lri.fr>
*
* $Id$
*
* Please refer to file version.h for the
* copyright notice regarding this software
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "ivyd.h"
const char* const servicename = "ivyd";
const char* const port_env_variable = "INIVYD_PORT";
const int default_port = DEFAULT_INIVYD_PORT;
int debug = 0;
void
Usage (const char* progname) {
fprintf (stderr, "Usage: %s [-b bus (ignored)][-s serverhost] text\n", progname);
exit (1);
}
/* Get in touch with super-server and retrieve server's port number */
int
GetPort (long addr, int key)
{
struct servent *serv;
u_short serv_port;
struct sockaddr_in sin;
struct connmsg msg;
int sock;
int len;
/* get service port number */
serv = getservbyname (servicename, 0);
if (! serv) {
char *pp = getenv (port_env_variable);
serv_port = htons (pp ? atoi (pp) : default_port);
if (serv_port <= 0)
exit (0);
} else
serv_port = serv -> s_port;
/* create and bind socket */
sock = socket (AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
perror ("Can't create socket: ");
return 0;
}
memset (&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if (-1 == bind (sock, &sin, sizeof sin)) {
perror ("Can't bind socket: ");
goto failed;
}
sin.sin_port = serv_port;
sin.sin_addr.s_addr = addr;
/* build request */
#if 0
msg.msg_key = getuid ();
#else
msg.msg_key = key;
#endif
msg.msg_port = 0;
msg.msg_type = MSG_CLIENT;
/* send it */
if (-1 == sendto (sock, (char *)&msg, sizeof msg, 0, (struct sockaddr *)&sin, sizeof sin)) {
perror ("Can't send on socket: ");
goto failed;
}
/* wait for reply */
len = sizeof sin;
if (-1 == recvfrom (sock, (char *)&msg, sizeof msg, 0, (struct sockaddr *)&sin, &len)) {
perror ("Can't receive on socket: ");
goto failed;
}
/* OK, we're done, return reply */
(void) close (sock);
return (int) msg.msg_port;
failed:
(void) close (sock);
return 0;
}
int
ServerOpen (const char *host, int key)
{
int sock;
static struct sockaddr_in addr;
struct hostent *hp;
/* determine host name and address */
if (!host)
host = getenv ("IVYD");
if (!host)
host = "localhost";
hp = gethostbyname (host);
if (hp == 0) {
fprintf (stderr, "Unknown host %s\n", host);
return 0;
}
/* create socket */
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror ("Can't create socket: ");
return 0;
}
/* build address from host addr, asking port number to super-server */
memcpy (&addr.sin_addr, hp->h_addr, hp->h_length);
addr.sin_family = AF_INET;
addr.sin_port = GetPort (addr.sin_addr.s_addr, key);
if (! addr.sin_port)
return 0;
/* connect socket on address */
if (connect (sock, &addr, sizeof (addr)) < 0) {
perror ("Can't connect socket: ");
return 0;
}
return sock;
}
int
SendMsg (int sock, const char* s)
{
request req;
/* build request */
strcpy (req.buffer, s);
/* send request */
/* we should handle return value more finely */
if (write (sock, &req, sizeof (request)) != sizeof (request)) {
fprintf (stderr, "Unable to write %d bytes: ", sizeof (request));
perror ("");
}
#if 0
answer ans;
register char *p = (char *) &ans;
register int n, l;
/* retrieve answer (useless) */
for (n = sizeof (answer); n > 0; p += l) {
n -= (l = read (sock, p, n));
if (l <= 0) {
perror ("Error retrieving answer from server: ");
return 0;
}
}
return ans.ok;
#endif
return 1;
}
void
HandleArgs (int sock, int index, int argc, char** argv)
{
char buf [BUFSIZE];
int space = 0;
int nbchars = 0;
/* slice arguments into buffer */
buf [0] = '\0';
while (index < argc) {
const char* p = argv[index++];
int l = strlen (p);
/* while we have pending chars for this arg... */
while (l + space > 0) {
/* first try and fit space if needed */
if (space) {
if (nbchars + space <= BUFSIZE - 1) {
strcat (buf, " ");
nbchars += space;
space = 0;
} else {
goto bufferfull;
}
}
/* then look if we can fit all the chars we have */
if (nbchars + l <= BUFSIZE - 1) {
/* if yes, go ahead */
strcat (buf, p);
nbchars += l;
/* we'll need to insert space if there are other args */
space = 1;
/* but let's make sure we don't keep looping */
l = -1;
/* if buffer is full, handle it */
if (nbchars == BUFSIZE - 1)
goto bufferfull;
/* otherwise, we're done anyway */
else
continue;
/* if not enough room, first fit what we can */
} else {
int fitting = (BUFSIZE - 1) - nbchars;
if (fitting > 0) {
strncat (buf, p, fitting);
nbchars += fitting;
l -= fitting;
p += fitting;
}
}
bufferfull: /* now we have a full buffer */
/* send it */
if (debug)
printf ("Sending [%s]\n", buf);
SendMsg (sock, buf);
/* and prepare for next round */
nbchars = 0;
buf [0] = '\0';
}
}
if (nbchars > 0) {
if (debug)
printf ("Sending [%s]\n", buf);
SendMsg (sock, buf);
}
}
int
ExtractPort (const char* bus)
{
int res = 2010;
const char* p;
if (bus && (p = index (bus, ':'))) {
res = atoi (p+1);
}
return res;
}
int
main (int argc, char** argv)
{
char busbuf [128];
const char* bus = 0;
int busport;
char serverhostbuf [128];
const char* serverhost = 0;
const struct option options [] = {
{"bus", required_argument, 0, 'b'},
{"debug", no_argument, 0, 'd'},
{"server", required_argument, 0, 's'},
{0, 0, 0, 0}
};
char c;
int servfd;
#if 0
if (strlen (argv0) >= 4 && !strcmp (argv0+strlen (argv0)-4, "wait"))
wflag = 1;
#endif
/* handle options */
while ((c = getopt_long (argc, argv, "+b:s:d", options, 0)) != EOF) {
switch (c) {
case 'b':
strcpy (busbuf, optarg) ;
bus = busbuf;
break;
case 's':
strcpy (serverhostbuf, optarg) ;
serverhost = serverhostbuf;
break;
case 'd':
debug = 1;
break;
}
}
busport = ExtractPort (bus);
/* Se connecter au serveur. */
servfd = ServerOpen (serverhost, busport);
if (!servfd) {
fprintf (stderr, "No server.\n");
return 0;
}
HandleArgs (servfd, optind, argc, argv);
return 1;
}
|