]>
git.saurik.com Git - apple/network_cmds.git/blob - mnc.tproj/mnc_main.c
2 * $Id: mnc_main.c,v 1.12 2004/09/22 19:14:23 colmmacc Exp $
4 * mnc_main.c -- Multicast NetCat
6 * Colm MacCarthaigh, <colm@apache.org>
8 * Copyright (c) 2007, Colm MacCarthaigh.
9 * Copyright (c) 2004 - 2006, HEAnet Ltd.
11 * This software is an open source.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
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.
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.
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.
44 /* Non-windows includes */
46 #include <sys/types.h>
47 #include <sys/socket.h>
53 /* Windows-specific includes */
64 int main(int argc
, char **argv
)
66 /* Utility variables */
71 /* Our main configuration */
72 struct mnc_configuration
* config
;
77 if (WSAStartup(MAKEWORD(2,2), &wsaData
) != 0)
79 mnc_error("This operating system is not supported\n");
83 /* Parse the command line */
84 config
= parse_arguments(argc
, argv
);
87 if ((sock
= socket(config
->group
->ai_family
, config
->group
->ai_socktype
,
88 config
->group
->ai_protocol
)) < 0)
90 mnc_error("Could not create socket\n");
93 /* Are we supposed to listen? */
94 if (config
->mode
== LISTENER
)
96 /* Set up the socket for listening */
97 if (multicast_setup_listen(sock
, config
->group
, config
->source
,
100 mnc_error("Can not listen for multicast packets.\n");
103 /* Recieve the packets */
104 while ((len
= recvfrom(sock
, buffer
, sizeof(buffer
),
105 0, NULL
, NULL
)) >= 0)
107 write(STDOUT_FILENO
, buffer
, len
);
110 else /* Assume MODE == SENDER */
112 /* Set up the socket for sending */
113 if (multicast_setup_send(sock
, config
->group
, config
->source
)
116 mnc_error("Can not send multicast packets\n");
119 /* Send the packets */
120 while((len
= read(STDIN_FILENO
, buffer
, sizeof(buffer
))) > 0)
122 sendto(sock
, buffer
, len
, 0, config
->group
->ai_addr
,
123 config
->group
->ai_addrlen
);
127 /* Close the socket */