ipbroadcast.c (167140) | ipbroadcast.c (167149) |
---|---|
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 167140 2007-03-01 13:26:13Z bms $"); | 32__FBSDID("$FreeBSD: head/tools/regression/netinet/ipbroadcast/ipbroadcast.c 167149 2007-03-01 18:36:16Z 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 <netinet/in.h> 40#include <arpa/inet.h> --- 10 unchanged lines hidden (view full) --- 51#include <getopt.h> 52#include <pwd.h> 53#include <unistd.h> 54#include <netdb.h> 55#include <libgen.h> 56 57#define DEFAULT_PORT 6698 58#define DEFAULT_PAYLOAD_SIZE 24 | 33 34#include <sys/param.h> 35#include <sys/types.h> 36#include <sys/ioctl.h> 37#include <sys/socket.h> 38 39#include <netinet/in.h> 40#include <arpa/inet.h> --- 10 unchanged lines hidden (view full) --- 51#include <getopt.h> 52#include <pwd.h> 53#include <unistd.h> 54#include <netdb.h> 55#include <libgen.h> 56 57#define DEFAULT_PORT 6698 58#define DEFAULT_PAYLOAD_SIZE 24 |
59#define DEFAULT_TTL 1 |
|
59#define MY_CMSG_SIZE CMSG_SPACE(sizeof(struct in_addr)) 60 61static char *progname = NULL; 62 63static void 64usage(void) 65{ 66 67 fprintf(stderr, | 60#define MY_CMSG_SIZE CMSG_SPACE(sizeof(struct in_addr)) 61 62static char *progname = NULL; 63 64static void 65usage(void) 66{ 67 68 fprintf(stderr, |
68"usage: %s [-1] [-b] [-B] [-d] [-l len] [-p port] [-r] [-s srcaddr] \n" | 69"usage: %s [-1] [-b] [-B] [-d] [-l len] [-p port] [-r] [-s srcaddr] [-t ttl]\n" |
69" <dest>\n", 70 progname); 71 fprintf(stderr, "IPv4 broadcast test program. Sends a %d byte UDP " 72 "datagram to <dest>:<port>.\n", DEFAULT_PAYLOAD_SIZE); 73 fprintf(stderr, "-1: Set IP_ONESBCAST\n"); 74 fprintf(stderr, "-b: bind socket to INADDR_ANY:<sport>\n"); 75 fprintf(stderr, "-B: Set SO_BROADCAST\n"); 76 fprintf(stderr, "-d: Set SO_DONTROUTE\n"); 77#if 0 78 fprintf(stderr, "-r: Fill datagram with random bytes\n"); 79#endif | 70" <dest>\n", 71 progname); 72 fprintf(stderr, "IPv4 broadcast test program. Sends a %d byte UDP " 73 "datagram to <dest>:<port>.\n", DEFAULT_PAYLOAD_SIZE); 74 fprintf(stderr, "-1: Set IP_ONESBCAST\n"); 75 fprintf(stderr, "-b: bind socket to INADDR_ANY:<sport>\n"); 76 fprintf(stderr, "-B: Set SO_BROADCAST\n"); 77 fprintf(stderr, "-d: Set SO_DONTROUTE\n"); 78#if 0 79 fprintf(stderr, "-r: Fill datagram with random bytes\n"); 80#endif |
80 fprintf(stderr, "-s: Set IP_SENDSRCADDR to <srcaddr>\n"); | |
81 fprintf(stderr, "-l: Set payload size to <len>\n"); 82 fprintf(stderr, "-p: Set source and destination port (default: %d)\n", 83 DEFAULT_PORT); | 81 fprintf(stderr, "-l: Set payload size to <len>\n"); 82 fprintf(stderr, "-p: Set source and destination port (default: %d)\n", 83 DEFAULT_PORT); |
84 fprintf(stderr, "-s: Set IP_SENDSRCADDR to <srcaddr>\n"); 85 fprintf(stderr, "-t: Set IP_TTL to <ttl>\n"); |
|
84 85 exit(EXIT_FAILURE); 86} 87 88int 89main(int argc, char *argv[]) 90{ 91 char *buf; --- 13 unchanged lines hidden (view full) --- 105 int dorandom; 106 size_t buflen; 107 ssize_t nbytes; 108 int portno; 109 int ret; 110 int s; 111 socklen_t soptlen; 112 int soptval; | 86 87 exit(EXIT_FAILURE); 88} 89 90int 91main(int argc, char *argv[]) 92{ 93 char *buf; --- 13 unchanged lines hidden (view full) --- 107 int dorandom; 108 size_t buflen; 109 ssize_t nbytes; 110 int portno; 111 int ret; 112 int s; 113 socklen_t soptlen; 114 int soptval; |
115 int ttl; |
|
113 114 dobind = 0; 115 dobroadcast = 0; 116 dontroute = 0; 117 doonesbcast = 0; 118 dorandom = 0; 119 120 dstaddr.s_addr = INADDR_ANY; 121 srcaddr_s = NULL; 122 portno = DEFAULT_PORT; | 116 117 dobind = 0; 118 dobroadcast = 0; 119 dontroute = 0; 120 doonesbcast = 0; 121 dorandom = 0; 122 123 dstaddr.s_addr = INADDR_ANY; 124 srcaddr_s = NULL; 125 portno = DEFAULT_PORT; |
126 ttl = DEFAULT_TTL; |
|
123 124 buf = NULL; 125 buflen = DEFAULT_PAYLOAD_SIZE; 126 127 progname = basename(argv[0]); | 127 128 buf = NULL; 129 buflen = DEFAULT_PAYLOAD_SIZE; 130 131 progname = basename(argv[0]); |
128 while ((ch = getopt(argc, argv, "1bBdl:p:rs:")) != -1) { | 132 while ((ch = getopt(argc, argv, "1bBdl:p:rs:t:")) != -1) { |
129 switch (ch) { 130 case '1': 131 doonesbcast = 1; 132 break; 133 case 'b': 134 dobind = 1; 135 break; 136 case 'B': --- 9 unchanged lines hidden (view full) --- 146 portno = atoi(optarg); 147 break; 148 case 'r': 149 dorandom = 1; 150 break; 151 case 's': 152 srcaddr_s = optarg; 153 break; | 133 switch (ch) { 134 case '1': 135 doonesbcast = 1; 136 break; 137 case 'b': 138 dobind = 1; 139 break; 140 case 'B': --- 9 unchanged lines hidden (view full) --- 150 portno = atoi(optarg); 151 break; 152 case 'r': 153 dorandom = 1; 154 break; 155 case 's': 156 srcaddr_s = optarg; 157 break; |
158 case 't': 159 ttl = atoi(optarg); 160 break; |
|
154 default: 155 usage(); 156 break; 157 } 158 } 159 argc -= optind; 160 argv += optind; 161 --- 26 unchanged lines hidden (view full) --- 188 soptlen); 189 if (ret == -1) { 190 perror("setsockopt SO_BROADCAST"); 191 close(s); 192 exit(EXIT_FAILURE); 193 } 194 } 195 | 161 default: 162 usage(); 163 break; 164 } 165 } 166 argc -= optind; 167 argv += optind; 168 --- 26 unchanged lines hidden (view full) --- 195 soptlen); 196 if (ret == -1) { 197 perror("setsockopt SO_BROADCAST"); 198 close(s); 199 exit(EXIT_FAILURE); 200 } 201 } 202 |
203 soptval = ttl; 204 soptlen = sizeof(soptval); 205 ret = setsockopt(s, IPPROTO_IP, IP_TTL, &soptval, soptlen); 206 if (ret == -1) { 207 perror("setsockopt IPPROTO_IP IP_TTL"); 208 close(s); 209 exit(EXIT_FAILURE); 210 } 211 |
|
196 if (doonesbcast) { 197 soptval = 1; 198 soptlen = sizeof(soptval); 199 ret = setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &soptval, 200 soptlen); 201 if (ret == -1) { 202 perror("setsockopt IP_ONESBCAST"); 203 close(s); --- 63 unchanged lines hidden --- | 212 if (doonesbcast) { 213 soptval = 1; 214 soptlen = sizeof(soptval); 215 ret = setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &soptval, 216 soptlen); 217 if (ret == -1) { 218 perror("setsockopt IP_ONESBCAST"); 219 close(s); --- 63 unchanged lines hidden --- |