/* Written by Matt Wright, The Center for New Music and Audio Technologies, University of California, Berkeley. Copyright (c) 1998,99,2000,01,02,03,04 The Regents of the University of California (Regents). Permission to use, copy, modify, distribute, and distribute modified versions of this software and its documentation without fee and without a signed licensing agreement, is hereby granted, provided that the above copyright notice, this paragraph and the following two paragraphs appear in all copies, modifications, and distributions. IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. The OSC webpage is http://cnmat.cnmat.berkeley.edu/OpenSoundControl */ /* dumpUDP.c: smallest UDP receiving application by Matt Wright, 9/9/98 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef struct ClientAddressStruct { struct sockaddr_in cl_addr; int clilen; int sockfd; } *ClientAddr; void PrintClientAddr(ClientAddr CA) { unsigned long addr = CA->cl_addr.sin_addr.s_addr; printf("Client address %p:\n", CA); printf(" clilen %d, sockfd %d\n", CA->clilen, CA->sockfd); printf(" sin_family %d, sin_port %d\n", CA->cl_addr.sin_family, CA->cl_addr.sin_port); printf(" address: (%x) %s\n", addr, inet_ntoa(CA->cl_addr.sin_addr)); printf(" sin_zero = \"%c%c%c%c%c%c%c%c\"\n", CA->cl_addr.sin_zero[0], CA->cl_addr.sin_zero[1], CA->cl_addr.sin_zero[2], CA->cl_addr.sin_zero[3], CA->cl_addr.sin_zero[4], CA->cl_addr.sin_zero[5], CA->cl_addr.sin_zero[6], CA->cl_addr.sin_zero[7]); printf("\n"); } static int initudp(int port) { struct sockaddr_in serv_addr; int n, sockfd; if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return sockfd; bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(port); if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("unable to bind\n"); return -1; } fcntl(sockfd, F_SETFL, FNDELAY); return sockfd; } static void closeudp(int sockfd) { close(sockfd); } /* file descriptor poll table */ static int npolldevs =0; typedef struct polldev { int fd; void (*callbackfunction)(int , void *); void *dummy; } polldev; #define TABMAX 8 static polldev polldevs[TABMAX]; /* Register a device (referred to by a file descriptor that the caller should have already successfully obtained from a system call) to be polled as real-time constraints allowed. When a select(2) call indicates activity on the file descriptor, the callback function is called with the file descripter as first argument and the given dummy argument (presumably a pointer to the instance variables associated with the device). */ int RegisterPollingDevice(int fd, void (*callbackfunction)(int , void *), void *dummy) { if(npolldevs=nfds) { nfds = polldevs[j].fd+1; } } time_to_quit = 0; sigset(SIGINT, catch_sigint); /* set sig handler */ while(!time_to_quit) { int c,r; back: FD_ZERO(&read_fds); /* clear read_fds */ FD_ZERO(&write_fds); /* clear write_fds */ FD_SET(sockfd, &read_fds); { int j; for(j=0;j0) { struct ClientAddressStruct a; GotAPacket(mbuf, n, &returnAddress); if (time_to_quit) goto out; returnAddress.clilen = maxclilen; } } } /* End of while(!time_to_quit) */ out: ; }