/* $Id: aprs-udp-rx.c,v 1.1 2005/08/10 12:10:10 james Exp james $ udplx, listener, displays what is sent only Copyright (C) 2005 James Cameron This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef lint static char vcid[] = "$Id: aprs-udp-rx.c,v 1.1 2005/08/10 12:10:10 james Exp james $"; #endif /* lint */ #include #include #include #include #include #include #include int main (int argc, char *argv[]) { unsigned short port; char *host; char *var; int subs = 1; int sock; struct sockaddr_in address; int stat, len; char buf[BUFSIZ]; port = 8135; if (argc > 1) host = argv[1]; if (argc > 2) port = atoi(argv[2]); var = getenv("APRS_UDP_RX_SUBSCRIBE"); if (var != NULL) subs = atoi(var); sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("socket"); exit(1); } if (!subs) { address.sin_addr.s_addr = INADDR_ANY; address.sin_family = AF_INET; address.sin_port = htons(port); stat = bind(sock, (struct sockaddr *) &address, sizeof(address)); if (stat < 0) { perror("bind"); exit(2); } } address.sin_family = AF_INET; address.sin_port = htons(port); if ((address.sin_addr.s_addr = inet_addr(host)) == -1) { struct hostent *hp; if ((hp = gethostbyname(host)) == NULL) { herror("gethostbyname"); exit(2); } else { address.sin_addr.s_addr = *(long *) hp->h_addr; } } while(1) { if (subs) { /* send empty, to subscribe */ stat = sendto(sock, buf, 0, 0, (struct sockaddr *) &address, sizeof(struct sockaddr)); if (stat < 0) { perror("sendto"); exit(4); } } /* receive response */ len = recvfrom(sock, buf, BUFSIZ, 0, NULL, NULL); if (len < 0) { perror("recvfrom"); exit(3); } if (len == 0) { fprintf(stderr, "subscription acknowledged\n"); } else { /* display response */ stat = write(STDOUT_FILENO, buf, len); if (stat < 0) { perror("write"); exit(4); } } } close(sock); }