1/* 2 * $Id: mnc_main.c,v 1.12 2004/09/22 19:14:23 colmmacc Exp $ 3 * 4 * mnc_main.c -- Multicast NetCat 5 * 6 * Colm MacCarthaigh, <colm@apache.org> 7 * 8 * Copyright (c) 2007, Colm MacCarthaigh. 9 * Copyright (c) 2004 - 2006, HEAnet Ltd. 10 * 11 * This software is an open source. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * Redistributions of source code must retain the above copyright notice, 18 * this list of conditions and the following disclaimer. 19 * 20 * Redistributions in binary form must reproduce the above copyright notice, 21 * this list of conditions and the following disclaimer in the documentation 22 * and/or other materials provided with the distribution. 23 * 24 * Neither the name of the HEAnet Ltd. nor the names of its contributors may 25 * be used to endorse or promote products derived from this software without 26 * specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 * 40 */ 41 42#ifndef WINDOWS 43 44/* Non-windows includes */ 45 46#include <sys/types.h> 47#include <sys/socket.h> 48#include <unistd.h> 49#include <stdio.h> 50 51#else 52 53/* Windows-specific includes */ 54 55#include <winsock2.h> 56#include <ws2tcpip.h> 57#include <stdlib.h> 58#include <stdio.h> 59 60#endif /* WINDOWS */ 61 62#include "mnc.h" 63 64int main(int argc, char **argv) 65{ 66 /* Utility variables */ 67 int sock, 68 len; 69 char buffer[1024]; 70 71 /* Our main configuration */ 72 struct mnc_configuration * config; 73 74#ifdef WINDOWS 75 WSADATA wsaData; 76 77 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) 78 { 79 mnc_error("This operating system is not supported\n"); 80 } 81#endif 82 83 /* Parse the command line */ 84 config = parse_arguments(argc, argv); 85 86 /* Create a socket */ 87 if ((sock = socket(config->group->ai_family, config->group->ai_socktype, 88 config->group->ai_protocol)) < 0) 89 { 90 mnc_error("Could not create socket\n"); 91 } 92 93 /* Are we supposed to listen? */ 94 if (config->mode == LISTENER) 95 { 96 /* Set up the socket for listening */ 97 if (multicast_setup_listen(sock, config->group, config->source, 98 config->iface) < 0) 99 { 100 mnc_error("Can not listen for multicast packets.\n"); 101 } 102 103 /* Recieve the packets */ 104 while ((len = recvfrom(sock, buffer, sizeof(buffer), 105 0, NULL, NULL)) >= 0) 106 { 107 write(STDOUT_FILENO, buffer, len); 108 } 109 } 110 else /* Assume MODE == SENDER */ 111 { 112 /* Set up the socket for sending */ 113 if (multicast_setup_send(sock, config->group, config->source) 114 < 0) 115 { 116 mnc_error("Can not send multicast packets\n"); 117 } 118 119 /* Send the packets */ 120 while((len = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) 121 { 122 sendto(sock, buffer, len, 0, config->group->ai_addr, 123 config->group->ai_addrlen); 124 } 125 } 126 127 /* Close the socket */ 128 close(sock); 129 130 return 0; 131} 132