]>
git.saurik.com Git - apple/network_cmds.git/blob - mnc.tproj/mnc_opts.c
2 * $Id: mnc_opts.c,v 1.3 2004/09/22 16:02:26 colmmacc Exp $
4 * mnc_opts.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.
49 #include <sys/types.h>
50 #include <sys/socket.h>
54 /* WINDOWS-y includes */
61 /* Display a usage statement */
65 "Usage: mnc [-l] [-i interface] [-p port] group-id "
66 "[source-address]\n\n"
68 "-i : specify interface to listen\n"
69 "-p : specify port to listen/send on\n\n");
73 struct mnc_configuration
* parse_arguments(int argc
, char **argv
)
75 /* Utility variables */
78 struct addrinfo hints
;
80 /* Our persisting configuration */
81 static struct mnc_configuration config
;
83 /* Set some defaults */
85 config
.port
= MNC_DEFAULT_PORT
;
89 /* Loop through the arguments */
90 for (optind
= 1; optind
< (argc
- 1); optind
++)
92 if ( (argv
[optind
][0] == '-') || (argv
[optind
][0] == '/') )
94 switch(argv
[optind
][1])
96 /* Set listening mode */
97 case 'l': config
.mode
= LISTENER
;
101 case 'p': config
.port
= argv
[++optind
];
104 /* Set an interface */
105 case 'i': config
.iface
= argv
[++optind
];
108 /* Unrecognised option */
115 /* assume we've ran out of options */
120 /* There's a chance we were passed one option */
121 if (optind
>= argc
|| argv
[optind
][0] == '-')
126 /* Now make sure we have either exactly 1 or 2 more arguments */
127 if ( (argc
- optind
) != 1 && (argc
- optind
) != 2 )
129 /* We have not been given the right ammount of
134 /* You can't have an interface without also listening */
135 if (config
.mode
== SENDER
&& config
.iface
!= NULL
)
137 mnc_error("You may only specify the interface when in"
138 " listening mode\n");
141 /* Set some hints for getaddrinfo */
142 memset(&hints
, 0, sizeof(hints
));
144 /* We want a UDP socket */
145 hints
.ai_socktype
= SOCK_DGRAM
;
147 /* Don't do any name-lookups */
148 hints
.ai_flags
= AI_NUMERICHOST
;
150 /* Get the group-id information */
152 getaddrinfo(argv
[optind
], config
.port
, &hints
, &config
.group
)) != 0)
154 mnc_error("Error getting group-id address information: %s\n",
155 gai_strerror(errorcode
));
158 /* Move on to next argument */
161 /* Get the source information */
162 if ( (argc
- optind
) == 1)
166 getaddrinfo(argv
[optind
], config
.port
, &hints
, &config
.source
))
169 mnc_error("Error getting source-address information: %s\n",
170 gai_strerror(errorcode
));
173 /* Confirm that the source and group are in the same Address Family */
174 if ( config
.source
->ai_family
!= config
.group
->ai_family
)
176 mnc_error("Group ID and Source address are not of "