u_pdreceive.c 7.25 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
/* Copyright (c) 2000 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in the Pd distribution.  */

5
/* the "pdreceive" command. This is a standalone program that receives messages
Miller Puckette's avatar
Miller Puckette committed
6
7
8
from Pd via the netsend/netreceive ("FUDI") protocol, and copies them to
standard output. */

9
10
11
12
13
/* May 2008 : fixed a buffer overflow problem; pdreceive sometimes 
    repeated infinitely its buffer during high speed transfer. 
    Moonix::Antoine Rousseau
*/

Miller Puckette's avatar
Miller Puckette committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef MSW
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#define SOCKET_ERROR -1
#endif

typedef struct _fdpoll
{
    int fdp_fd;
34
35
36
37
    char *fdp_outbuf;/*output message buffer*/ 
    int fdp_outlen;     /*length of output message*/
    int fdp_discard;/*buffer overflow: output message is incomplete, discard it*/
    int fdp_gotsemi;/*last char from input was a semicolon*/
Miller Puckette's avatar
Miller Puckette committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
} t_fdpoll;

static int nfdpoll;
static t_fdpoll *fdpoll;
static int maxfd;
static int sockfd;
static int protocol;

static void sockerror(char *s);
static void x_closesocket(int fd);
static void dopoll(void);
#define BUFSIZE 4096

int main(int argc, char **argv)
{
    int portno;
54
    struct sockaddr_in server = {0};
Miller Puckette's avatar
Miller Puckette committed
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
#ifdef MSW
    short version = MAKEWORD(2, 0);
    WSADATA nobby;
#endif
    if (argc < 2 || sscanf(argv[1], "%d", &portno) < 1 || portno <= 0)
        goto usage;
    if (argc >= 3)
    {
        if (!strcmp(argv[2], "tcp"))
            protocol = SOCK_STREAM;
        else if (!strcmp(argv[2], "udp"))
            protocol = SOCK_DGRAM;
        else goto usage;
    }
    else protocol = SOCK_STREAM;
#ifdef MSW
    if (WSAStartup(version, &nobby)) sockerror("WSAstartup");
#endif
    sockfd = socket(AF_INET, protocol, 0);
    if (sockfd < 0)
    {
        sockerror("socket()");
        exit(1);
    }
    maxfd = sockfd + 1;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;

#ifdef IRIX
        /* this seems to work only in IRIX but is unnecessary in
        Linux.  Not sure what MSW needs in place of this. */
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0)
        fprintf(stderr, "setsockopt failed\n");
#endif

        /* assign client port number */
    server.sin_port = htons((unsigned short)portno);

        /* name the socket */
    if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        sockerror("bind");
        x_closesocket(sockfd);
        return (0);
    }
    if (protocol == SOCK_STREAM)
    {
        if (listen(sockfd, 5) < 0)
        {
            sockerror("listen");
            x_closesocket(sockfd);
            exit(1);
        }
    }
        /* now loop forever selecting on sockets */
    while (1)
        dopoll();

usage:
    fprintf(stderr, "usage: pdreceive <portnumber> [udp|tcp]\n");
    fprintf(stderr, "(default is tcp)\n");
    exit(1);
}

static void addport(int fd)
{
    t_fdpoll *fp;
    fdpoll = (t_fdpoll *)realloc(fdpoll,
        (nfdpoll+1) * sizeof(t_fdpoll));
    fp = fdpoll + nfdpoll;
    fp->fdp_fd = fd;
    nfdpoll++;
    if (fd >= maxfd) maxfd = fd + 1;
128
129
    fp->fdp_outlen = fp->fdp_discard = fp->fdp_gotsemi = 0;
    if (!(fp->fdp_outbuf = malloc(BUFSIZE)))
Miller Puckette's avatar
Miller Puckette committed
130
131
132
133
134
135
136
137
138
    {
        fprintf(stderr, "out of memory");
        exit(1);
    }
    printf("number_connected %d;\n", nfdpoll);
}

static void rmport(t_fdpoll *x)
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
139
    int i;
Miller Puckette's avatar
Miller Puckette committed
140
141
142
143
144
145
    t_fdpoll *fp;
    for (i = nfdpoll, fp = fdpoll; i--; fp++)
    {
        if (fp == x)
        {
            x_closesocket(fp->fdp_fd);
146
            free(fp->fdp_outbuf);
Miller Puckette's avatar
Miller Puckette committed
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
            while (i--)
            {
                fp[0] = fp[1];
                fp++;
            }
            fdpoll = (t_fdpoll *)realloc(fdpoll,
                (nfdpoll-1) * sizeof(t_fdpoll));
            nfdpoll--;
            printf("number_connected %d;\n", nfdpoll);
            return;
        }
    }
    fprintf(stderr, "warning: item removed from poll list but not found");
}

static void doconnect(void)
{
    int fd = accept(sockfd, 0, 0);
    if (fd < 0)
        perror("accept");
    else addport(fd);
}

static void udpread(void)
{
    char buf[BUFSIZE];
    int ret = recv(sockfd, buf, BUFSIZE, 0);
    if (ret < 0)
    {
        sockerror("recv (udp)");
        x_closesocket(sockfd);
        exit(1);
    }
    else if (ret > 0)
    {
#ifdef MSW
        int j;
        for (j = 0; j < ret; j++)
            putchar(buf[j]);
#else
        if (write(1, buf, ret) < ret)
        {
            perror("write");
            exit(1);
        }
#endif
    }
}

196
static int tcpmakeoutput(t_fdpoll *x, char *inbuf, int len)
Miller Puckette's avatar
Miller Puckette committed
197
{
198
199
200
201
202
    int i;
    int outlen = x->fdp_outlen;
    char *outbuf = x->fdp_outbuf;
    
    for (i = 0 ; i < len ; i++)
Miller Puckette's avatar
Miller Puckette committed
203
    {
204
205
206
207
208
209
210
211
212
213
214
        char c = inbuf[i];
        
        if((c != '\n') || (!x->fdp_gotsemi))
            outbuf[outlen++] = c;
        x->fdp_gotsemi = 0; 
        if (outlen >= (BUFSIZE-1)) /*output buffer overflow; reserve 1 for '\n' */
        {
            fprintf(stderr, "pdreceive: message too long; discarding\n");
            outlen = 0;
            x->fdp_discard = 1;
        }  
Miller Puckette's avatar
Miller Puckette committed
215
216
217
            /* search for a semicolon.   */
        if (c == ';')
        {
218
219
            outbuf[outlen++] = '\n';
            if (!x->fdp_discard)
Miller Puckette's avatar
Miller Puckette committed
220
            {
221
222
223
#ifdef MSW
                {
                    int j;
Miller Puckette's avatar
Miller Puckette committed
224
225
                    for (j = 0; j < outlen; j++)
                        putchar(outbuf[j]);
226
                }
Miller Puckette's avatar
Miller Puckette committed
227
#else
228
                write(1, outbuf, outlen);
Miller Puckette's avatar
Miller Puckette committed
229
#endif
230
231
232
233
234
235
236
237
238
            } /* if (!x->fdp_discard) */

            outlen = 0;
            x->fdp_discard = 0;
            x->fdp_gotsemi = 1;
        } /* if (c == ';') */
    } /* for */

    x->fdp_outlen = outlen;
Miller Puckette's avatar
Miller Puckette committed
239
240
241
242
243
    return (0);
}

static void tcpread(t_fdpoll *x)
{
244
245
    int  ret;
    char inbuf[BUFSIZE];
Miller Puckette's avatar
Miller Puckette committed
246

247
248
    ret = recv(x->fdp_fd, inbuf, BUFSIZE, 0);
    if (ret < 0)
Miller Puckette's avatar
Miller Puckette committed
249
    {
250
251
        sockerror("recv (tcp)");
        rmport(x);
Miller Puckette's avatar
Miller Puckette committed
252
    }
253
254
255
    else if (ret == 0)
        rmport(x);
    else tcpmakeoutput(x, inbuf, ret);
Miller Puckette's avatar
Miller Puckette committed
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
}

static void dopoll(void)
{
    int i;
    t_fdpoll *fp;
    fd_set readset, writeset, exceptset;
    FD_ZERO(&writeset);
    FD_ZERO(&readset);
    FD_ZERO(&exceptset);

    FD_SET(sockfd, &readset);
    if (protocol == SOCK_STREAM)
    {
        for (fp = fdpoll, i = nfdpoll; i--; fp++)
            FD_SET(fp->fdp_fd, &readset);
    }
    if (select(maxfd+1, &readset, &writeset, &exceptset, 0) < 0)
    {
        perror("select");
        exit(1);
    }
    if (protocol == SOCK_STREAM)
    {
        for (i = 0; i < nfdpoll; i++)
            if (FD_ISSET(fdpoll[i].fdp_fd, &readset))
                tcpread(&fdpoll[i]);
        if (FD_ISSET(sockfd, &readset))
            doconnect();
    }
    else
    {
        if (FD_ISSET(sockfd, &readset))
            udpread();
    }
}


static void sockerror(char *s)
{
#ifdef MSW
    int err = WSAGetLastError();
    if (err == 10054) return;
    else if (err == 10044)
    {
        fprintf(stderr,
            "Warning: you might not have TCP/IP \"networking\" turned on\n");
    }
#else
    int err = errno;
#endif
    fprintf(stderr, "%s: %s (%d)\n", s, strerror(err), err);
}

static void x_closesocket(int fd)
{
#ifdef MSW
    closesocket(fd);
#else
    close(fd);
#endif
}