38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN 1
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
60 #include <MultiSense/MultiSenseChannel.hh>
70 void usage(
const char *programNameP)
72 fprintf(stderr,
"USAGE: %s [<options>]\n", programNameP);
73 fprintf(stderr,
"Where <options> are:\n");
74 fprintf(stderr,
"\t-a <current_address> : CURRENT IPV4 address (default=10.66.171.21)\n");
75 fprintf(stderr,
"\t-A <new_address> : NEW IPV4 address (default=10.66.171.21)\n");
76 fprintf(stderr,
"\t-G <new_gateway> : NEW IPV4 gateway (default=10.66.171.1)\n");
77 fprintf(stderr,
"\t-N <new_netmask> : NEW IPV4 address (default=255.255.240.0)\n");
79 fprintf(stderr,
"\t-b <interface> : send broadcast packet to specified network interface. "
80 "This sets the ip address to the default 10.66.171.21\n");
82 fprintf(stderr,
"\t-y : disable confirmation prompt\n");
87 bool decodeIpv4(
const std::string& addr,
94 4 != sscanf(addr.c_str(),
"%3u.%3u.%3u.%3u",
95 &p1, &p2, &p3, &p4) ||
96 (p1 > 255 || p2 > 255 || p3 > 255 || p4 > 255)) {
97 fprintf(stderr,
"Unable to decode \"%s\" as IPV4 dotted-quad \n",
112 std::string currentAddress =
"10.66.171.21";
113 std::string desiredAddress =
"10.66.171.21";
114 std::string desiredGateway =
"10.66.171.1";
115 std::string desiredNetmask =
"255.255.240.0";
116 std::string iface =
"eth0";
125 while(-1 != (c =
getopt(argc, argvPP,
"a:A:G:N:b:y")))
127 case 'a': currentAddress = std::string(
optarg);
break;
128 case 'A': desiredAddress = std::string(
optarg);
break;
129 case 'G': desiredGateway = std::string(
optarg);
break;
130 case 'N': desiredNetmask = std::string(
optarg);
break;
131 case 'b': blind =
true; iface = std::string(
optarg);
break;
132 case 'y': prompt =
false;
break;
133 default:
usage(*argvPP);
break;
147 if (!decodeIpv4(desiredAddress, a1, a2, a3, a4))
159 if (NULL == channelP) {
160 fprintf(stderr,
"Failed to establish communications with \"%s\"\n",
161 currentAddress.c_str());
172 fprintf(stderr,
"failed to query sensor version: %s\n",
180 fprintf(stderr,
"failed to query device info %s\n",
186 192 == a1 && 168 == a2 && 0 == a3)
188 fprintf(stderr,
"MultiSense SL units use the 192.168.0 subnet to talk to the Hokuyo ");
189 fprintf(stderr,
"laser. Setting the IP address of the MultiSense to 192.168.0.X will ");
190 fprintf(stderr,
"interfere with the networking configuration.\n");
191 fprintf(stderr,
"\n");
192 fprintf(stderr,
"Aborting IP Change.\n");
200 perror (
"broadcast is not yet supported on Windows");
206 sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
208 perror(
"socket() failed");
213 if (setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
reinterpret_cast<char const*
>(&broadcast),
sizeof(broadcast))==-1) {
214 perror(
"setsockopt(...SO_BROADCAST) failed");
219 if (setsockopt(sockfd,SOL_SOCKET,IP_RECVIF,iface.c_str(),iface.size()+1)==-1) {
220 perror(
"setsockopt(...SO_BINDTODEVICE) failed");
226 if (setsockopt(sockfd,SOL_SOCKET,SO_BINDTODEVICE,iface.c_str(),iface.size()+1)==-1) {
227 perror(
"setsockopt(...SO_BINDTODEVICE) failed");
237 fprintf(stdout,
"NEW address: %s\n", desiredAddress.c_str());
238 fprintf(stdout,
"NEW gateway: %s\n", desiredGateway.c_str());
239 fprintf(stdout,
"NEW netmask: %s\n\n", desiredNetmask.c_str());
242 fprintf(stdout,
"** WARNING: All MultiSense devices attached to interface '%s' will have their addresses changed **\n\n", iface.c_str());
245 fprintf(stdout,
"Really update network configuration? (y/n): ");
248 int reply = getchar();
249 if (
'Y' != reply &&
'y' != reply) {
250 fprintf(stdout,
"Aborting\n");
266 fprintf(stderr,
"Failed to set the network configuration: %s\n",
269 fprintf(stdout,
"Network parameters changed successfully\n");
274 struct sockaddr_in si;
277 memset(&si,0,
sizeof(si));
278 si.sin_family = AF_INET;
279 si.sin_port = htons(9001);
280 si.sin_addr.s_addr = htonl(INADDR_BROADCAST);
305 header.sequenceIdentifier = 0;
310 if(sendto(sockfd,
reinterpret_cast<char const*
>(buffer.
data()),buffer.
tell(),0,
reinterpret_cast<sockaddr*
>(&si),
sizeof(si)) == -1) {
311 perror(
"sendto() failed");
315 fprintf(stdout,
"Successfully transmitted network parameter change command\n");