1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* rmcp_ping.c
*
* OpenIPMI RMCP pinger
*
* Author: Montavista Software
* Corey Minyard <cminyard@mvista.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <netdb.h>
unsigned char ping_msg[12] =
{
0x06, /* RMCP version 1.0 */
0x00, /* reserved */
0xff, /* RMCP seq num, not used for IPMI */
0x06, /* ASF message */
0x00, 0x00, 0x11, 0xbe, /* ASF IANA */
0x80, /* Presence Ping */
0x00, /* Message tag */
0x00, /* Reserved */
0x00 /* Data Length */
};
static char *progname;
static void
usage(void)
{
fprintf(stderr,
"Send an RMCP ping packet onces a second to the destination,\n");
fprintf(stderr,
"printing unique responses it receives\n");
fprintf(stderr,
"Usage:\n");
fprintf(stderr,
" %s [-p <port>] [-t <waittime>] [-s <starttag>]"
" [-d] [destination]\n",
progname);
fprintf(stderr,
" -p - Destination port, defaults to 623\n");
fprintf(stderr,
" -t - Sets the number of seconds to wait for responses"
" (default 10)\n");
fprintf(stderr,
" -s - Sets the start tag to start sending at"
" (0-254, default 0\n");
fprintf(stderr,
" -d - enable debugging\n");
fprintf(stderr,
" destination - the target address, default is the broadcast\n");
fprintf(stderr,
" address (default 255.255.255.255)\n");
exit(1);
}
unsigned int port = 0x26f;
unsigned int waittime = 10;
unsigned int starttag = 0;
int debug_packet = 0;
struct socklist
{
struct in_addr addr;
struct socklist *next;
};
struct socklist *socklist = NULL;
static int
add_host(struct sockaddr *addr)
{
struct socklist *curr = socklist;
struct sockaddr_in *ip;
if (addr->sa_family != AF_INET)
return 0;
ip = (struct sockaddr_in *) addr;
while (curr) {
if (ip->sin_addr.s_addr == curr->addr.s_addr)
break;
curr = curr->next;
}
if (curr)
return 0;
curr = malloc(sizeof(*curr));
curr->addr = ip->sin_addr;
curr->next = socklist;
socklist = curr;
return 1;
}
int
main(int argc, char *argv[])
{
unsigned char rsp[28];
int sock;
int rv;
struct sockaddr dest;
size_t destlen;
struct sockaddr src;
int val;
socklen_t fromlen;
fd_set readfds;
struct timeval currtime;
struct timeval endtime;
struct timeval twait;
char host[200];
char serv[200];
char *addrname;
int send_next;
int i;
progname = argv[0];
for (i=1; i<argc; i++) {
if (argv[i][0] != '-')
break;
if (argv[i][1] == '\0')
usage();
if (strcmp(argv[i], "--") == 0) {
i++;
break;
} else if (strcmp(argv[i], "-p") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "No parameter given for -p\n\n");
usage();
}
port = strtoul(argv[i], NULL, 0);
} else if (strcmp(argv[i], "-t") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "No parameter given for -t\n\n");
usage();
}
waittime = strtoul(argv[i], NULL, 0);
} else if (strcmp(argv[i], "-s") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "No parameter given for -s\n\n");
usage();
}
starttag = strtoul(argv[i], NULL, 0);
if (starttag > 254) {
fprintf(stderr, "Invalid start tag for -s\n\n");
usage();
}
} else if (strcmp(argv[i], "-d") == 0) {
debug_packet++;
}
}
if (i < argc) {
struct addrinfo hints, *res0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
rv = getaddrinfo(argv[i], "623", &hints, &res0);
if (rv != 0) {
fprintf(stderr, "Unable to handle given address: %s\n\n",
gai_strerror(rv));
usage();
}
dest = *(res0->ai_addr);
destlen = res0->ai_addrlen;
freeaddrinfo(res0);
} else {
struct sockaddr_in *ip = (struct sockaddr_in *) &dest;
ip->sin_family = AF_INET;
ip->sin_port = htons(port);
ip->sin_addr.s_addr = INADDR_BROADCAST;
destlen = sizeof(*ip);
}
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == -1) {
perror("socket");
exit(1);
}
/* We want it to be non-blocking. */
rv = fcntl(sock, F_SETFL, O_NONBLOCK);
if (rv) {
close(sock);
perror("fcntl(sock, F_SETFL, O_NONBLOCK)");
exit(1);
}
val = 1;
rv = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
if (rv) {
close(sock);
perror("setsockopt(sock, SO_BROADCAST)");
exit(1);
}
gettimeofday(&currtime, NULL);
endtime = currtime;
endtime.tv_sec += waittime;
send_next = 1;
for (;;) {
if ((endtime.tv_sec < currtime.tv_sec)
|| ((endtime.tv_sec == currtime.tv_sec)
&& (endtime.tv_usec < currtime.tv_usec)))
break;
if (send_next) {
ping_msg[9] = starttag;
starttag++;
if (starttag > 254)
starttag = 0;
rv = sendto(sock, ping_msg, sizeof(ping_msg), 0, &dest, destlen);
if (rv < 0) {
close(sock);
perror("sendto");
exit(1);
}
send_next = 0;
}
twait.tv_usec = 0;
twait.tv_sec = 1;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
rv = select(sock+1, &readfds, NULL, NULL, &twait);
if (rv < 0) {
close(sock);
perror("select");
exit(1);
}
if (rv == 0) {
send_next = 1;
goto next;
}
fromlen = sizeof(src);
rv = recvfrom(sock, rsp, sizeof(rsp), 0, &src, &fromlen);
if (debug_packet && (rv > 0)) {
int i;
printf("Got packet:");
for (i=0; i<rv; i++) {
if ((i % 16) == 0)
printf("\n ");
printf(" %2.2x", rsp[i]);
}
printf("\n");
}
if (rv < 0) {
perror("recvfrom");
} else if (rv < 28) {
fprintf(stderr, "Invalid receive length: %d, should be 28\n", rv);
} else if ((rsp[0] != 6) || (rsp[3] != 6) || (rsp[4] != 0x00)
|| (rsp[5] != 0x00) || (rsp[6] != 0x11) || (rsp[7] != 0xbe)
|| (rsp[8] != 0x40) || (rsp[11] < 16))
{
fprintf(stderr, "Invalid ping response\n");
} else {
if (! add_host(&src))
goto next;
rv = getnameinfo(&src, fromlen, host, sizeof(host),
serv, sizeof(serv), 0);
if (rv) {
struct sockaddr_in *ip = (struct sockaddr_in *) &src;
addrname = inet_ntoa(ip->sin_addr);
} else
addrname = host;
printf("%s", addrname);
if ((rsp[20] & 0x80) && ((rsp[20] & 0xf) == 0x01))
printf(" IPMI");
printf("\n");
}
next:
gettimeofday(&currtime, NULL);
}
return 0;
}
|