ipbroadcast.c (167341) | ipbroadcast.c (167512) |
---|---|
1/*- 2 * Copyright (c) 2007 Bruce M. Simpson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 15 unchanged lines hidden (view full) --- 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Test utility for IPv4 broadcast sockets. 29 */ 30 31#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2007 Bruce M. Simpson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 15 unchanged lines hidden (view full) --- 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Test utility for IPv4 broadcast sockets. 29 */ 30 31#include <sys/cdefs.h> |
32__FBSDID("$FreeBSD: head/tools/regression/netinet/ipbroadcast/ipbroadcast.c 167341 2007-03-08 14:37:00Z bms $"); | 32__FBSDID("$FreeBSD: head/tools/regression/netinet/ipbroadcast/ipbroadcast.c 167512 2007-03-13 23:36:48Z bms $"); |
33 34#include <sys/param.h> 35#include <sys/types.h> 36#include <sys/ioctl.h> 37#include <sys/socket.h> 38 39#include <net/if.h> 40#include <net/if_dl.h> --- 14 unchanged lines hidden (view full) --- 55#include <unistd.h> 56#include <netdb.h> 57#include <libgen.h> 58 59#ifndef IP_SENDIF 60#define IP_SENDIF 24 /* XXX */ 61#endif 62 | 33 34#include <sys/param.h> 35#include <sys/types.h> 36#include <sys/ioctl.h> 37#include <sys/socket.h> 38 39#include <net/if.h> 40#include <net/if_dl.h> --- 14 unchanged lines hidden (view full) --- 55#include <unistd.h> 56#include <netdb.h> 57#include <libgen.h> 58 59#ifndef IP_SENDIF 60#define IP_SENDIF 24 /* XXX */ 61#endif 62 |
63#ifndef IPPROTO_ZEROHOP 64#define IPPROTO_ZEROHOP 114 /* any 0-hop protocol */ 65#endif 66 |
|
63#define DEFAULT_PORT 6698 64#define DEFAULT_PAYLOAD_SIZE 24 65#define DEFAULT_TTL 1 66 67#define MY_CMSG_SIZE \ 68 CMSG_SPACE(sizeof(struct in_addr)) + \ 69 CMSG_SPACE(sizeof(struct sockaddr_dl)) 70 71static char *progname = NULL; 72 73static void 74usage(void) 75{ 76 77 fprintf(stderr, "IPv4 broadcast test program. Sends a %d byte UDP " 78 "datagram to <dest>:<port>.\n\n", DEFAULT_PAYLOAD_SIZE); 79 fprintf(stderr, 80"usage: %s [-1] [-A laddr] [-b] [-B] [-d] [-i iface] [-l len]\n" | 67#define DEFAULT_PORT 6698 68#define DEFAULT_PAYLOAD_SIZE 24 69#define DEFAULT_TTL 1 70 71#define MY_CMSG_SIZE \ 72 CMSG_SPACE(sizeof(struct in_addr)) + \ 73 CMSG_SPACE(sizeof(struct sockaddr_dl)) 74 75static char *progname = NULL; 76 77static void 78usage(void) 79{ 80 81 fprintf(stderr, "IPv4 broadcast test program. Sends a %d byte UDP " 82 "datagram to <dest>:<port>.\n\n", DEFAULT_PAYLOAD_SIZE); 83 fprintf(stderr, 84"usage: %s [-1] [-A laddr] [-b] [-B] [-d] [-i iface] [-l len]\n" |
81" [-p port] [-r] [-s srcaddr] [-t ttl] <dest>\n", | 85" [-p port] [-R] [-s srcaddr] [-t ttl] <dest>\n", |
82 progname); 83 fprintf(stderr, "-1: Set IP_ONESBCAST\n"); 84 fprintf(stderr, "-A: specify laddr (default: INADDR_ANY)\n"); 85 fprintf(stderr, "-b: bind socket to <laddr>:<lport>\n"); 86 fprintf(stderr, "-B: Set SO_BROADCAST\n"); 87 fprintf(stderr, "-d: Set SO_DONTROUTE\n"); 88 fprintf(stderr, "-i: Set IP_SENDIF <iface> (if supported)\n"); 89 fprintf(stderr, "-l: Set payload size to <len>\n"); 90 fprintf(stderr, "-p: Set local and remote port (default: %d)\n", 91 DEFAULT_PORT); | 86 progname); 87 fprintf(stderr, "-1: Set IP_ONESBCAST\n"); 88 fprintf(stderr, "-A: specify laddr (default: INADDR_ANY)\n"); 89 fprintf(stderr, "-b: bind socket to <laddr>:<lport>\n"); 90 fprintf(stderr, "-B: Set SO_BROADCAST\n"); 91 fprintf(stderr, "-d: Set SO_DONTROUTE\n"); 92 fprintf(stderr, "-i: Set IP_SENDIF <iface> (if supported)\n"); 93 fprintf(stderr, "-l: Set payload size to <len>\n"); 94 fprintf(stderr, "-p: Set local and remote port (default: %d)\n", 95 DEFAULT_PORT); |
96 fprintf(stderr, "-R: Use raw IP (protocol %d)\n", IPPROTO_ZEROHOP); |
|
92#if 0 93 fprintf(stderr, "-r: Fill datagram with random bytes\n"); 94#endif 95 fprintf(stderr, "-s: Set IP_SENDSRCADDR to <srcaddr>\n"); 96 fprintf(stderr, "-t: Set IP_TTL to <ttl>\n"); 97 98 exit(EXIT_FAILURE); 99} --- 15 unchanged lines hidden (view full) --- 115 char *laddr_s; 116 char *srcaddr_s; 117 int ch; 118 int dobind; 119 int dobroadcast; 120 int dontroute; 121 int doonesbcast; 122 int dorandom; | 97#if 0 98 fprintf(stderr, "-r: Fill datagram with random bytes\n"); 99#endif 100 fprintf(stderr, "-s: Set IP_SENDSRCADDR to <srcaddr>\n"); 101 fprintf(stderr, "-t: Set IP_TTL to <ttl>\n"); 102 103 exit(EXIT_FAILURE); 104} --- 15 unchanged lines hidden (view full) --- 120 char *laddr_s; 121 char *srcaddr_s; 122 int ch; 123 int dobind; 124 int dobroadcast; 125 int dontroute; 126 int doonesbcast; 127 int dorandom; |
128 int dorawip; |
|
123 size_t buflen; 124 ssize_t nbytes; 125 int portno; 126 int ret; 127 int s; 128 socklen_t soptlen; 129 int soptval; 130 int ttl; 131 132 dobind = 0; 133 dobroadcast = 0; 134 dontroute = 0; 135 doonesbcast = 0; 136 dorandom = 0; | 129 size_t buflen; 130 ssize_t nbytes; 131 int portno; 132 int ret; 133 int s; 134 socklen_t soptlen; 135 int soptval; 136 int ttl; 137 138 dobind = 0; 139 dobroadcast = 0; 140 dontroute = 0; 141 doonesbcast = 0; 142 dorandom = 0; |
143 dorawip = 0; |
|
137 138 ifname = NULL; 139 dstaddr.s_addr = INADDR_ANY; 140 laddr_s = NULL; 141 srcaddr_s = NULL; 142 portno = DEFAULT_PORT; 143 ttl = DEFAULT_TTL; 144 145 buf = NULL; 146 buflen = DEFAULT_PAYLOAD_SIZE; 147 148 progname = basename(argv[0]); | 144 145 ifname = NULL; 146 dstaddr.s_addr = INADDR_ANY; 147 laddr_s = NULL; 148 srcaddr_s = NULL; 149 portno = DEFAULT_PORT; 150 ttl = DEFAULT_TTL; 151 152 buf = NULL; 153 buflen = DEFAULT_PAYLOAD_SIZE; 154 155 progname = basename(argv[0]); |
149 while ((ch = getopt(argc, argv, "1A:bBdi:l:p:rs:t:")) != -1) { | 156 while ((ch = getopt(argc, argv, "1A:bBdi:l:p:Rrs:t:")) != -1) { |
150 switch (ch) { 151 case '1': 152 doonesbcast = 1; 153 break; 154 case 'A': 155 laddr_s = optarg; 156 break; 157 case 'b': --- 9 unchanged lines hidden (view full) --- 167 ifname = optarg; 168 break; 169 case 'l': 170 buflen = atoi(optarg); 171 break; 172 case 'p': 173 portno = atoi(optarg); 174 break; | 157 switch (ch) { 158 case '1': 159 doonesbcast = 1; 160 break; 161 case 'A': 162 laddr_s = optarg; 163 break; 164 case 'b': --- 9 unchanged lines hidden (view full) --- 174 ifname = optarg; 175 break; 176 case 'l': 177 buflen = atoi(optarg); 178 break; 179 case 'p': 180 portno = atoi(optarg); 181 break; |
182 case 'R': 183 dorawip = 1; 184 break; |
|
175 case 'r': 176 dorandom = 1; 177 break; 178 case 's': 179 srcaddr_s = optarg; 180 break; 181 case 't': 182 ttl = atoi(optarg); --- 8 unchanged lines hidden (view full) --- 191 192 if (argc != 1) 193 usage(); 194 if (argv[0] == NULL || inet_aton(argv[0], &dstaddr) == 0) 195 usage(); 196 /* IP_SENDSRCADDR and IP_SENDIF are mutually exclusive just now. */ 197 if (srcaddr_s != NULL && ifname != NULL) 198 usage(); | 185 case 'r': 186 dorandom = 1; 187 break; 188 case 's': 189 srcaddr_s = optarg; 190 break; 191 case 't': 192 ttl = atoi(optarg); --- 8 unchanged lines hidden (view full) --- 201 202 if (argc != 1) 203 usage(); 204 if (argv[0] == NULL || inet_aton(argv[0], &dstaddr) == 0) 205 usage(); 206 /* IP_SENDSRCADDR and IP_SENDIF are mutually exclusive just now. */ 207 if (srcaddr_s != NULL && ifname != NULL) 208 usage(); |
199 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); | 209 if (dorawip) { 210 if (geteuid() != 0) 211 fprintf(stderr, "WARNING: not running as root.\n"); 212 s = socket(PF_INET, SOCK_RAW, IPPROTO_ZEROHOP); 213 } else { 214 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 215 } |
200 if (s == -1) { 201 perror("socket"); 202 exit(EXIT_FAILURE); 203 } 204 205 if (dontroute) { 206 soptval = 1; 207 soptlen = sizeof(soptval); --- 143 unchanged lines hidden --- | 216 if (s == -1) { 217 perror("socket"); 218 exit(EXIT_FAILURE); 219 } 220 221 if (dontroute) { 222 soptval = 1; 223 soptlen = sizeof(soptval); --- 143 unchanged lines hidden --- |