File: rmcpping.c

package info (click to toggle)
freeipmi 1.4.11-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,952 kB
  • ctags: 18,990
  • sloc: ansic: 299,490; sh: 11,087; makefile: 1,958; perl: 1,078
file content (263 lines) | stat: -rw-r--r-- 8,120 bytes parent folder | download | duplicates (2)
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
/*****************************************************************************\
 *  $Id: rmcpping.c,v 1.55 2010-02-08 22:02:31 chu11 Exp $
 *****************************************************************************
 *  Copyright (C) 2007-2014 Lawrence Livermore National Security, LLC.
 *  Copyright (C) 2003-2007 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Albert Chu <chu11@llnl.gov>
 *  UCRL-CODE-155448
 *
 *  This file is part of Ipmiping, tools for pinging IPMI and RMCP compliant
 *  remote systems. For details, see http://www.llnl.gov/linux/.
 *
 *  Ipmiping is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, or (at your
 *  option) any later version.
 *
 *  Ipmiping is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with Ipmiping.  If not, see <http://www.gnu.org/licenses/>.
\*****************************************************************************/

#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#if STDC_HEADERS
#include <string.h>
#endif /* STDC_HEADERS */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <assert.h>
#include <errno.h>

#include <freeipmi/freeipmi.h>

#include "freeipmi-portability.h"
#include "debug-util.h"
#include "ping-tool-common.h"

#define _supported(x)   (x) ? "supported" : "not-supported"

int
createpacket (const char *destination,
              void *buf,
              unsigned int buflen,
              unsigned int sequence_number,
              int version,
              int debug)
{
  fiid_obj_t obj_rmcp_hdr = NULL;
  fiid_obj_t obj_rmcp_cmd = NULL;
  int len;

  assert (destination);
  assert (buf);

  if (!buflen)
    return (0);

  if (!(obj_rmcp_hdr = fiid_obj_create (tmpl_rmcp_hdr)))
    ipmi_ping_err_exit ("fiid_obj_create: %s", strerror (errno));
  if (!(obj_rmcp_cmd = fiid_obj_create (tmpl_cmd_asf_presence_ping)))
    ipmi_ping_err_exit ("fiid_obj_create: %s", strerror (errno));

  if (fill_rmcp_hdr_asf (obj_rmcp_hdr) < 0)
    ipmi_ping_err_exit ("fill_rmcp_hdr_asf: %s", strerror (errno));

  /* Avoid use of message tag number 0xFF.  Behavior of message tag 0xFF
   * is unpredictable.  See IPMI 1.5 Specification and DMTF RMCP
   * specification for details.
   */

  if (fill_cmd_asf_presence_ping (sequence_number % (RMCP_ASF_MESSAGE_TAG_MAX + 1),
                                  obj_rmcp_cmd) < 0)
    ipmi_ping_err_exit ("fill_cmd_asf_presence_ping: %s", strerror (errno));

  if ((len = assemble_rmcp_pkt (obj_rmcp_hdr,
                                obj_rmcp_cmd,
                                buf,
                                buflen,
				IPMI_INTERFACE_FLAGS_DEFAULT)) < 0)
    ipmi_ping_err_exit ("assemble_rmcp_pkt: %s", strerror (errno));

  if (debug)
    {
      char hdrbuf[DEBUG_UTIL_HDR_BUFLEN];

      debug_hdr_str (DEBUG_UTIL_TYPE_NONE,
                     DEBUG_UTIL_DIRECTION_NONE,
		     DEBUG_UTIL_FLAGS_DEFAULT,
                     DEBUG_UTIL_RMCPPING_STR,
                     hdrbuf,
                     DEBUG_UTIL_HDR_BUFLEN);

      if (ipmi_dump_rmcp_packet (STDERR_FILENO,
                                 destination,
                                 hdrbuf,
                                 NULL,
                                 buf,
                                 len,
                                 tmpl_cmd_asf_presence_ping) < 0)
        ipmi_ping_err_exit ("ipmi_dump_rmcp_packet: %s", strerror (errno));
    }

  fiid_obj_destroy (obj_rmcp_hdr);
  fiid_obj_destroy (obj_rmcp_cmd);
  return (len);
}

int
parsepacket (const char *destination,
             const void *buf,
             unsigned int buflen,
             const char *from,
             unsigned int sequence_number,
             int verbose,
             int version,
             int debug)
{
  fiid_obj_t obj_rmcp_hdr = NULL;
  fiid_obj_t obj_rmcp_cmd = NULL;
  uint8_t message_type, message_tag;
  uint64_t val;
  int ret, rv = -1;

  assert (destination);
  assert (buf);
  assert (from);

  if (!buflen)
    return (0);

  if (!(obj_rmcp_hdr = fiid_obj_create (tmpl_rmcp_hdr)))
    ipmi_ping_err_exit ("fiid_obj_create: %s", strerror (errno));
  if (!(obj_rmcp_cmd = fiid_obj_create (tmpl_cmd_asf_presence_pong)))
    ipmi_ping_err_exit ("fiid_obj_create: %s", strerror (errno));

  if (debug)
    {
      char hdrbuf[DEBUG_UTIL_HDR_BUFLEN];

      debug_hdr_str (DEBUG_UTIL_TYPE_NONE,
                     DEBUG_UTIL_DIRECTION_NONE,
		     DEBUG_UTIL_FLAGS_DEFAULT,
                     DEBUG_UTIL_RMCPPONG_STR,
                     hdrbuf,
                     DEBUG_UTIL_HDR_BUFLEN);

      if (ipmi_dump_rmcp_packet (STDERR_FILENO,
                                 destination,
                                 hdrbuf,
                                 NULL,
                                 buf,
                                 buflen,
                                 tmpl_cmd_asf_presence_pong) < 0)
        ipmi_ping_err_exit ("ipmi_dump_rmcp_packet: %s", strerror (errno));
    }

  if ((ret = unassemble_rmcp_pkt (buf,
                                  buflen,
                                  obj_rmcp_hdr,
                                  obj_rmcp_cmd,
				  IPMI_INTERFACE_FLAGS_DEFAULT)) < 0)
    ipmi_ping_err_exit ("unassemble_rmcp_pkt: %s", strerror (errno));

  if (!ret)
    {
      rv = 0;
      goto cleanup;
    }

  if (FIID_OBJ_GET (obj_rmcp_cmd,
                    "message_type",
                    &val) < 0)
    ipmi_ping_err_exit ("fiid_obj_get: 'message_type': %s",
                        fiid_obj_errormsg (obj_rmcp_cmd));
  message_type = val;
  
  if (message_type != RMCP_ASF_MESSAGE_TYPE_PRESENCE_PONG)
    {
      rv = 0;
      goto cleanup;
    }

  if (FIID_OBJ_GET (obj_rmcp_cmd,
                    "message_tag",
                    &val) < 0)
    ipmi_ping_err_exit ("fiid_obj_get: 'message_tag': %s",
                        fiid_obj_errormsg (obj_rmcp_cmd));
  message_tag = val;
  
  if (message_tag != (sequence_number % (RMCP_ASF_MESSAGE_TAG_MAX + 1)))
    {
      rv = 0;
      goto cleanup;
    }

  printf ("pong received from %s: message_tag=%u", from, message_tag);
  if (verbose)
    {
      uint8_t ipmi_supported;
      
      if (FIID_OBJ_GET (obj_rmcp_cmd,
                        "supported_entities.ipmi_supported",
                        &val) < 0)
        ipmi_ping_err_exit ("fiid_obj_get: 'supported_entities.ipmi_supported': %s",
                            fiid_obj_errormsg (obj_rmcp_cmd));
      ipmi_supported = val;
      
      printf (", ipmi %s", _supported (ipmi_supported));
    }
  printf ("\n");

  rv = 1;
 cleanup:
  fiid_obj_destroy (obj_rmcp_hdr);
  fiid_obj_destroy (obj_rmcp_cmd);
  return (rv);
}

void
latepacket (unsigned int sequence_number)
{
  printf ("pong timed out: message_tag=%u\n", sequence_number % (RMCP_ASF_MESSAGE_TAG_MAX + 1));
}

int
endresult (const char *progname,
           const char *destination,
           unsigned int sent_count,
           unsigned int recv_count)
{
  double percent = 0;

  assert (progname);
  assert (destination);

  if (sent_count > 0)
    percent = ((double)(sent_count - recv_count)/sent_count)*100;

  printf ("--- %s %s statistics ---\n", progname, destination);
  printf ("%d pings transmitted, %d pongs received in time, "
          "%2.1f%% packet loss\n",
          sent_count, recv_count, percent);

  return ((recv_count > 0) ? 0 : 1);
}

int
main (int argc, char **argv)
{
  ipmi_ping_setup (argc, argv, 0, RMCP_ASF_MESSAGE_TAG_MAX, "hVc:i:I:t:vs:d");
  ipmi_ping_loop (createpacket, parsepacket, latepacket, endresult);
  exit (EXIT_FAILURE);                    /* NOT REACHED */
}