summaryrefslogtreecommitdiff
path: root/audio.c
blob: 7eabc10a00bffa3e6a26d2dd11003a9dd82a8e7f (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * Copyright 1996 Network Computing Devices
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of NCD. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  NCD. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * NCD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NCD.
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Keith Packard, Network Computing Devices
 */

#include "scope.h"
#include "nas.h"

#include <sys/uio.h>            /* for struct iovec, used by socket.h */
#include <sys/socket.h>         /* for AF_INET, SOCK_STREAM, ... */
#include <sys/ioctl.h>          /* for FIONCLEX, FIONBIO, ... */
#if !defined(FIOCLEX) && defined(HAVE_SYS_FILIO_H)
#include <sys/filio.h>
#endif
#include <fcntl.h>
#include <netinet/in.h>         /* struct sockaddr_in */
#include <netinet/tcp.h>
#include <netdb.h>              /* struct servent * and struct hostent * */
#include <errno.h>              /* for EINTR, EADDRINUSE, ... */

static void
ReportFromAudioClient(FD fd, unsigned char *buf, long n)
{
    if (NasVerbose) {
        if (ScopeEnabled) {
            PrintTime();
            fprintf(stdout, "NAS Client%s --> %4ld %s\n",
                    ClientName(fd), n, (n == 1 ? "byte" : "bytes"));
        }
    }
    ProcessBuffer(fd, buf, n);
}

static void
ReportFromAudioServer(FD fd, const unsigned char *buf, long n)
{
    if (NasVerbose) {
        if (ScopeEnabled) {
            PrintTime();
            fprintf(stdout, "\t\t\t\t\t%4ld %s <-- NAS Server%s\n",
                    n, (n == 1 ? "byte" : "bytes"), ClientName(fd));
        }
    }
    ProcessBuffer(fd, buf, n);
}

static long StartAudioSetUpMessage(FD fd, const unsigned char *buf, long n);
static long FinishAudioSetUpMessage(FD fd, const unsigned char *buf, long n);
static long StartAudioRequest(FD fd, const unsigned char *buf, long n);
static long FinishAudioRequest(FD fd, const unsigned char *buf, long n);
static long StartAudioSetUpReply(FD fd, const unsigned char *buf, long n);
static long FinishAudioSetUpReply(FD fd, const unsigned char *buf, long n);
static long FinishAudioReply(FD fd, const unsigned char *buf, long n);
static long AudioServerPacket(FD fd, const unsigned char *buf, long n);

static void
StartAudioClientConnection(FD fd)
{
    enterprocedure("StartAudioClientConnection");
    /* when a new connection is started, we have no saved bytes */
    CS[fd].SavedBytes = NULL;
    CS[fd].SizeofSavedBytes = 0;
    CS[fd].NumberofSavedBytes = 0;
    CS[fd].NumberofBytesProcessed = 0;

    /* when a new connection is started, we have no reply Queue */
    FlushReplyQ(fd);

    /* each new connection gets a request sequence number */
    CS[fd].SequenceNumber = 0;

    /* we need 12 bytes to start a SetUp message */
    CS[fd].ByteProcessing = StartAudioSetUpMessage;
    CS[fd].NumberofBytesNeeded = 12;
}

static void
StopAudioClientConnection(FD fd)
{
    enterprocedure("StopAudioClientConnection");
    /* when a new connection is stopped, discard the old buffer */

    if (CS[fd].SizeofSavedBytes > 0)
        free(CS[fd].SavedBytes);
}

static long
StartAudioSetUpMessage(FD fd, const unsigned char *buf, long n)
{
    short namelength;
    short datalength;

    enterprocedure("StartSetUpMessage");
    /*
       we need the first 12 bytes to be able to determine if, and how many,
       additional bytes we need for name and data authorization.  However, we
       can't process the first 12 bytes until we get all of them, so
       return zero bytes used, and increase the number of bytes needed
     */

    CS[fd].littleEndian = (buf[0] == 'l');
    CS[ServerHalf(fd)].littleEndian = CS[fd].littleEndian;
    littleEndian = CS[fd].littleEndian;
    namelength = IShort(&buf[6]);
    datalength = IShort(&buf[8]);
    CS[fd].ByteProcessing = FinishAudioSetUpMessage;
    CS[fd].NumberofBytesNeeded = n
        + pad((long) namelength) + pad((long) datalength);
    debug(8, (stderr, "need %ld bytes to finish startup\n",
              CS[fd].NumberofBytesNeeded - n));
    return (0);
}

static long
FinishAudioSetUpMessage(FD fd, const unsigned char *buf, long n)
{
    enterprocedure("FinishSetUpMessage");
    if (ScopeEnabled)
        PrintAudioSetUpMessage(buf);

    /* after a set-up message, we expect a string of requests */
    CS[fd].ByteProcessing = StartAudioRequest;
    CS[fd].NumberofBytesNeeded = 4;
    return (n);
}

static long
StartAudioRequest(FD fd, const unsigned char *buf, long n)
{
    unsigned short requestlength;

    enterprocedure("StartRequest");

    /* bytes 0,1 are ignored now; bytes 2,3 tell us the request length */
    requestlength = IShort(&buf[2]);
    CS[fd].ByteProcessing = FinishAudioRequest;
    CS[fd].NumberofBytesNeeded = 4 * requestlength;
    debug(8, (stderr, "need %ld more bytes to finish request\n",
              CS[fd].NumberofBytesNeeded - n));
    return (0);
}

static long
FinishAudioRequest(FD fd, const unsigned char *buf, long n)
{
    enterprocedure("FinishRequest");
    CS[fd].ByteProcessing = StartAudioRequest;
    CS[fd].NumberofBytesNeeded = 4;
    if (ScopeEnabled)
        DecodeAudioRequest(fd, buf, n);
    return (n);
}

static void
StartAudioServerConnection(FD fd)
{
    enterprocedure("StartAudioServerConnection");
    /* when a new connection is started, we have no saved bytes */
    CS[fd].SavedBytes = NULL;
    CS[fd].SizeofSavedBytes = 0;
    CS[fd].NumberofSavedBytes = 0;
    CS[fd].NumberofBytesProcessed = 0;

    /* when a new connection is started, we have no reply Queue */
    FlushReplyQ(fd);

    /* we need 8 bytes to start a SetUp reply */
    CS[fd].ByteProcessing = StartAudioSetUpReply;
    CS[fd].NumberofBytesNeeded = 8;
}

static void
StopAudioServerConnection(FD fd)
{
    enterprocedure("StopAudioServerConnection");
    /* when a new connection is stopped, discard the old buffer */

    if (CS[fd].SizeofSavedBytes > 0)
        free(CS[fd].SavedBytes);
}

static long
StartAudioSetUpReply(FD fd, const unsigned char *buf, long n)
{
    short replylength;

    enterprocedure("StartSetUpReply");
    replylength = IShort(&buf[6]);
    CS[fd].ByteProcessing = FinishAudioSetUpReply;
    CS[fd].NumberofBytesNeeded = n + 4 * replylength;
    debug(8, (stderr, "need %ld bytes to finish startup reply\n",
              CS[fd].NumberofBytesNeeded - n));
    return (0);
}

static long
FinishAudioSetUpReply(FD fd, const unsigned char *buf, long n)
{
    enterprocedure("FinishSetUpReply");
    if (ScopeEnabled)
        PrintAudioSetUpReply(buf);
    CS[fd].ByteProcessing = AudioServerPacket;
    CS[fd].NumberofBytesNeeded = 32;
    return (n);
}

/* ************************************************************ */

static long
AudioErrorPacket(FD fd, const unsigned char *buf, long n)
{
    CS[fd].ByteProcessing = AudioServerPacket;
    CS[fd].NumberofBytesNeeded = 32;
    DecodeAudioError(fd, buf, n);
    return (n);
}

static long
AudioEventPacket(FD fd, const unsigned char *buf, long n)
{
    CS[fd].ByteProcessing = AudioServerPacket;
    CS[fd].NumberofBytesNeeded = 32;
    if (ScopeEnabled)
        DecodeAudioEvent(fd, buf, n);
    return (n);
}

static long
AudioReplyPacket(FD fd, const unsigned char *buf, long n)
{
    long replylength;

    replylength = ILong(&buf[4]);

    /*
       Replies may need more bytes, so we compute how many more
       bytes are needed and ask for them, not using any of the bytes
       we were given (return(0) to say that no bytes were used).
       If the replylength is zero (we don't need any more bytes), the
       number of bytes needed will be the same as what we have, and
       so the top-level loop will call the next routine immediately
       with the same buffer of bytes that we were given.
     */

    CS[fd].ByteProcessing = FinishAudioReply;
    CS[fd].NumberofBytesNeeded = n + 4 * replylength;
    debug(8, (stderr, "need %ld bytes to finish reply\n", (4 * replylength)));
    return (0);
}

static long
FinishAudioReply(FD fd, const unsigned char *buf, long n)
{
    CS[fd].ByteProcessing = AudioServerPacket;
    CS[fd].NumberofBytesNeeded = 32;
    enterprocedure("FinishAudioReply");
    if (ScopeEnabled)
        DecodeAudioReply(fd, buf, n);
    return (n);
}

static long
AudioServerPacket(FD fd, const unsigned char *buf, long n)
{
    short PacketType;

    enterprocedure("ServerPacket");

    PacketType = IByte(&buf[0]);
    if (PacketType == 0)
        return (AudioErrorPacket(fd, buf, n));
    if (PacketType == 1)
        return (AudioReplyPacket(fd, buf, n));
    return (AudioEventPacket(fd, buf, n));
}

static void
CloseAudioConnection(FD fd)
{
    debug(4, (stderr, "close %d and %d\n", fd, FDPair(fd)));
    StopAudioClientConnection(ServerHalf(fd));
    StopAudioServerConnection(ClientHalf(fd));

    (void) close(fd);
    NotUsingFD(fd);
    (void) close(FDPair(fd));
    NotUsingFD(FDPair(fd));
}

/* when we get data from a client, we read it in, copy it to the
   server for this client, then dump it to the client. Note, we don't
   have to have a server, if there isn't one. */

static void
DataFromAudioClient(FD fd)
{
    long n;
    FD ServerFD;

    Verbose = NasVerbose;
    enterprocedure("DataFromAudioClient");
    ServerFD = FDPair(fd);
    if (ServerFD < 0) {
        ServerFD = ConnectToServer(false);
        if (ServerFD < 0) {
            CloseAudioConnection(fd);
            return;
        }
        SetUpPair(fd, ServerFD);
    }

    n = read(fd, FDinfo[ServerFD].buffer, BUFFER_SIZE);
    debug(4, (stderr, "read %ld bytes from Client%s\n", n, ClientName(fd)));
    if (n < 0) {
        PrintTime();
        perror("Client --> read error:");
        CloseAudioConnection(fd);
        return;
    }
    if (n == 0) {
        PrintTime();
        if (NasVerbose >= 0)
            fprintf(stdout, "Client%s --> EOF\n", ClientName(fd));
        CloseAudioConnection(fd);
        return;
    }

    FDinfo[ServerFD].bufcount = n;
    FDinfo[ServerFD].bufstart = 0;

    FlushFD(ServerFD);
    /* also report the bytes to standard out */
    ReportFromAudioClient(fd, FDinfo[ServerFD].buffer, n);
}

/* ********************************************** */
/*                                                */
/* ********************************************** */

/* similar situation for the server, but note that if there is no client,
   we close the connection down -- don't need a server with no client. */

static void
DataFromAudioServer(FD fd)
{
    long n;
    FD ClientFD;

    Verbose = NasVerbose;
    ClientFD = FDPair(fd);
    if (ClientFD < 0) {
        CloseAudioConnection(fd);
        return;
    }

    enterprocedure("DataFromAudioServer");
    n = read(fd, (char *) FDinfo[ClientFD].buffer, BUFFER_SIZE);
    debug(4, (stderr, "read %ld bytes from Server%s\n", n, ClientName(fd)));
    if (n < 0) {
        PrintTime();
        perror("read error <- Server");
        CloseAudioConnection(fd);
        return;
    }
    if (n == 0) {
        PrintTime();
        if (NasVerbose >= 0)
            fprintf(stdout, "EOF <-- Server%s\n", ClientName(fd));
        CloseAudioConnection(fd);
        return;
    }

    FDinfo[ClientFD].bufcount = n;
    FDinfo[ClientFD].bufstart = 0;
    FlushFD(ClientFD);

    /* also report the bytes to standard out */
    ReportFromAudioServer(fd, FDinfo[ClientFD].buffer, n);
}

static FD
ConnectToAudioClient(FD ConnectionSocket)
{
    FD ClientFD;

    ClientFD = AcceptConnection(ConnectionSocket);
    UsingFD(ClientFD, DataFromAudioClient, FlushFD, NULL);
    StartAudioClientConnection(ClientFD);
    return (ClientFD);
}

static FD
ConnectToAudioServer(Boolean report)
{
    FD ServerFD;
    XtransConnInfo trans_conn = NULL;   /* transport connection object */

    if (!AudioServerHostName[0])
        strcpy(AudioServerHostName, ServerHostName);
    ServerFD =
        MakeConnection(AudioServerHostName, GetServerport() + 2000, report,
                       &trans_conn);
    if (ServerFD >= 0) {
        UsingFD(ServerFD, DataFromAudioServer, FlushFD, trans_conn);
        StartAudioServerConnection(ServerFD);
    }
    return (ServerFD);
}

void
NewAudio(FD fd)
{
    FD ServerFD = -1;
    FD ClientFD = -1;

    ClientFD = ConnectToAudioClient(fd);
    ServerFD = ConnectToAudioServer(true);
    SetUpPair(ClientFD, ServerFD);
}

void
InitializeAudio(void)
{
    InitializeAudioDecode();
}