summaryrefslogtreecommitdiff
path: root/util/idiscover.c
blob: 57471e3cf1fd4a844d63fb16f98c4f0860035ca7 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
/*
 * idiscover.c
 * Discover all IPMI-LAN-enabled nodes on this network or subnet.
 * This program is not completely reliable yet, not all IPMI-LAN-enabled
 * nodes respond.
 * Currently this utility is compiled with NO_THREADS, but threads can
 * be enabled by commenting out this flag.
 *
 * Author:  Andy Cress  arcress at users.sourceforge.net
 * Copyright (c) 2006 Intel Corporation. 
 * Copyright (c) 2009 Kontron America, Inc.
 *
 * 10/27/06 Andy Cress - created
 * 05/01/07 Andy Cress - added -g for GetChannelAuthCap method,
 *                       added -a logic for broadcast ping,
 *                       updated WIN32 logic
 * 09/20/07 Andy Cress - fixed send/receive thread order
 * 07/15/08 Andy Cress - added -r for ping repeats
 * 11/21/08 Andy Cress - detect eth intf and broadcast ip addr
 */
/*M*
Copyright (c) 2006, Intel Corporation
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

  a.. Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer. 
  b.. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution. 
  c.. Neither the name of Intel Corporation nor the names of its contributors 
      may be used to endorse or promote products derived from this software 
      without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
 *M*/
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#ifdef WIN32
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <io.h>
#include <time.h>
#include "getopt.h"
#define NO_THREADS  1
typedef unsigned int socklen_t;
WSADATA lan_ws;  /*global for WSA*/

#elif defined(DOS)
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
#define NO_THREADS  1

#else
/* Linux, BSD, Solaris */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h> 
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#if defined(HPUX)
/* getopt is defined in stdio.h */
#elif defined(MACOS)
/* getopt is defined in unistd.h */
#include <unistd.h>
#include <sys/sockio.h>
#ifndef HAVE_CONFIG_H
typedef unsigned int socklen_t;
#endif
#else
#include <getopt.h>
#endif

/* comment out NO_THREADS to use this utility in Linux with threads */
#define NO_THREADS  1
#endif
#ifndef ETH_P_IP
#define ETH_P_IP  0x0800 /* Internet Protocol packet, see linux/if_ether.h */
#endif

/* TODO: fix RAW for -m in Solaris, FreeBSD, Windows (works in Linux) */
#ifdef SOLARIS
#include <sys/sockio.h>
#define RAW_DOMAIN  AF_INET
#define RAW_PROTO   IPPROTO_RAW
static char frawok = 0;  /*raw not working in Solaris*/ 
#elif BSD
#define RAW_DOMAIN  AF_INET
#define RAW_PROTO   IPPROTO_RAW
static char frawok = 0;  /*raw not working in FreeBSD*/ 
#elif MACOS
#define RAW_DOMAIN  AF_INET
#define RAW_PROTO   IPPROTO_RAW
static char frawok = 0;  /*raw not working in FreeBSD*/ 
#elif WIN32
#define RAW_DOMAIN  AF_INET
#define RAW_PROTO   IPPROTO_ICMP
static char frawok = 0;  /*raw not working in Windows*/ 
#else
#define RAW_DOMAIN  AF_PACKET
#define RAW_PROTO   htons(ETH_P_IP)
static char frawok = 1;  /*raw works in Linux*/
#endif

#include <string.h>
#include "ipmicmd.h"
 
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif

#define RMCP_PRI_RMCP_PORT     0x26F
#define SZ_PING    12
#define IPMI_PING_MAX_LEN  50  /* usu 28 */
#define CMD_GET_CHAN_AUTH_CAP      0x38

#ifdef WIN32
int GetFirstIP(uchar *ipaddr, uchar *macadr, char *ipname, char fdb); /*ilan.c*/
#endif

/*
 * Global variables 
 */
static char * progver   = "1.9";
static char * progname  = "idiscover";
static char   fdebug    = 0;
static char   fping     = 1;
static char   fraw      = 0;
static char   fBroadcastOk = 0;
static char   fcanonical   = 0;
static int broadcast_pings = 1;
//static uchar ipmi_maj = 0;
//static uchar ipmi_min = 0;
//static uchar netfn;
static ushort   g_port   = RMCP_PRI_RMCP_PORT; 
static SockType g_sockfd = 0; 
static SockType g_sockraw = 0; 
static int    g_limit   = 30;   /* after this many 'other' packets, stop. */
static struct sockaddr_in _srcaddr;
// static struct sockaddr_in _destaddrlist[255];
static struct  in_addr    _startAddr, _endAddr; 
static char g_startDest[MAXHOSTNAMELEN+1] = {'\0'};  
static char g_endDest[MAXHOSTNAMELEN+1] = {'\0'};
static char g_interface[INET_ADDRSTRLEN+1] = {""};  /*e.g. "eth0"*/
static int  g_num_packets = 0;
static int  g_npings = 0;
static int  g_npongs = 0;
static int  g_recv_status = 0;
static int  g_wait   = 1; /* num sec to wait */
static int  g_delay  = 0; /* num usec between sends */
static int  g_repeat = 1; /* number of times to repeat ping to each node */
static char bdelim = BDELIM;  /* '|' */

#ifdef METACOMMAND
extern FILE *fpdbg;     /*from ipmicmd.c*/
extern char *strlasterr(int rv); /*from ipmilan.c*/
#else
void dump_buf(char *tag, uchar *pbuf, int sz, char fshowascii)
{
   uchar line[17];
   uchar a;
   int i, j;
   FILE *fpdbg;

   fpdbg = stdout;
   line[0] = 0; line[16] = 0;
   j = 0;
   fprintf(fpdbg,"%s (len=%d): ", tag,sz);
   for (i = 0; i < sz; i++) {
      if (i % 16 == 0) { j = 0; fprintf(fpdbg,"%s\n  %04x: ",line,i); }
      if (fshowascii) {
         a = pbuf[i];
         if (a < 0x20 || a > 0x7f) a = '.';
         line[j++] = a;
      }
      fprintf(fpdbg,"%02x ",pbuf[i]);
   }
   if (j < 16) {
      line[j] = 0;
      for (i = 0; i < (16-j); i++) fprintf(fpdbg,"   ");
   }
   fprintf(fpdbg,"%s\n",line);
   return;
} 
#endif

void printerr( const char *pattn, ...)
{
   va_list arglist;
   FILE *fderr;

   // fderr = fopen("/tmp/idiscover.log","a+");
   // if (fderr == NULL) return;
   fderr = stderr;

   va_start(arglist, pattn);
   vfprintf(fderr, pattn, arglist);
   va_end(arglist);

   // fclose(fderr);
}

static char *showlasterr(void)
{
   char *str;
#ifdef WIN32
   static char strbuf[80];
   int rv;
   char *desc;
   rv = WSAGetLastError();
#ifdef METACOMMAND
   /* get descriptions from strlasterr in ipmilan.c */
   desc = strlasterr(rv);
#else
   if (rv == WSAECONNRESET) desc = "Connection reset"; /*10054*/
   else if (rv == WSAECONNREFUSED) desc = "Connection refused"; /*10061*/
   else if (rv == WSAEHOSTUNREACH) desc = "No route to host"; /*10065*/
   else desc = "";
#endif
   sprintf(strbuf,"LastError = %d  %s",rv,desc);
   str = strbuf;
#else
   str = strerror(errno);
#endif
   return(str);
}

static void cleanup(void)
{
   SockType *pfd;
   int i;
   for (i = 0; i < 2; i++) {
     if (i == 0) pfd = &g_sockfd;
     else pfd = &g_sockraw;
     if (*pfd > 0) {
#ifdef WIN32
        closesocket(*pfd);
        WSACleanup();
#else
        close(*pfd);
#endif
     }
     *pfd = 0;
   }
}

void show_usage(void)
{
   printf("Usage: %s [-abegix] \n",progname);
   printf("       -a      all nodes, enables broadcast ping\n");
   printf("       -b <ip> beginning IP address (x.x.x.x), required\n");
   printf("       -e <ip> ending IP address (x.x.x.x), default is begin IP\n");
   printf("       -g      use GetChanAuthCap instead of RMCP ping\n");
   printf("       -i      interface name, default is eth0\n");
   printf("       -m      get MAC addresses with a raw broadcast ping\n");
   printf("       -p N    specific Port (IPMI LAN port=623)\n");
   printf("       -r N    number of Repeat pings to each node (default=1)\n");
   // printf("       -s      specific subnet\n");
   printf("       -x      show eXtra debug messages\n");
}

static int os_sleep(unsigned int s, unsigned int u)
{
#ifdef WIN32
  if (s == 0) {
     if (u >= 1000) Sleep(u/1000);
  } else {
     Sleep(s * 1000);
  }
#else
/*Linux*/
#ifdef SELECT_TIMER
  struct timeval tv;
  tv.tv_sec  = s;
  tv.tv_usec = u;
  if (select(1, NULL, NULL, NULL, &tv) < 0)
     printerr("select: %s\n", showlasterr());
#else
  if (s == 0) {
      usleep(u);
  } else {
      sleep(s);
  }
#endif
#endif
  return 0;
}

void split_ip(uint addr, uchar *ip)
{
   ip[3] = (addr & 0x000000ff);
   ip[2] = (addr & 0x0000ff00) >> 8;
   ip[1] = (addr & 0x00ff0000) >> 16;
   ip[0] = (addr & 0xff000000) >> 24;
}

int ntoi(int addr)
{
   return(addr);
}

void show_ip(int saddr)
{
   uchar ip[4];
   split_ip(saddr,ip);
   printerr("%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
}

static int ipmilan_sendto(SockType s, const void *msg, size_t len, int flags, 
				const struct sockaddr *to, socklen_t tolen)
{
    int n;
#ifdef NEED_PAD
    int fusepad = 0;
    /* Never need a pad byte for ping/pong packets */
    if (len == 56 || len == 84 || len == 112 || len == 128 || len == 156) {
        fusepad = 1;
        len += 1;
    }
#endif
    n = (int)sendto(s,msg,len,flags,to,tolen);
    // if (fusepad && (n > 0)) n--;
    return(n);
}

static int ipmilan_recvfrom(SockType s, void *buf, size_t len, int flags, 
		struct sockaddr *from, socklen_t *fromlen)
{
    int rv;
    rv = (int)recvfrom(s,buf,len,flags,from,fromlen);
    /* Sometimes the OS sends an ECONNREFUSED error, but 
     * retrying will catch the BMC's reply packet. */
#ifdef WIN32
    if (rv < 0) {
        int err;
        err = WSAGetLastError();
        if (err == WSAECONNREFUSED) /*10061*/
            rv = (int)recvfrom(s,buf,len,flags,from,fromlen);
    }
#else
    if ((rv < 0) && (errno == ECONNREFUSED))
        rv = (int)recvfrom(s,buf,len,flags,from,fromlen);
#endif
    return(rv);
}

#if defined(WIN32)
int inet_aton(const char *cp, struct in_addr *inp)
{
   int rv;
   int adr;
   inp->s_addr = inet_addr(cp);
   adr = (int)inp->s_addr;
   if (adr == INADDR_NONE) rv = 0;
   else rv = 1;  /*success*/
   return(rv);
}
#elif defined(SOLARIS)
int find_ifname(char *ifname)
{ return(-1); }
#else
#include <ifaddrs.h>
int find_ifname(char *ifname)
{
    struct ifaddrs *ifaddr, *ifa;
    int rv = -1;
    if (getifaddrs(&ifaddr) == -1) return(rv);
    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
	if (ifa->ifa_addr == NULL) continue;
	if ((ifa->ifa_addr->sa_family != AF_INET) &&
	    (ifa->ifa_addr->sa_family != AF_INET6)) continue;
	if (strcmp(ifa->ifa_name,"lo") == 0) continue;
	/* if here, we have a valid ifname */
	strcpy(ifname,ifa->ifa_name);
	if (fdebug) printf("find_ifname: found %s\n",ifname);
	rv = 0;
	break;
    }
    freeifaddrs(ifaddr);
    return(rv);
}
#endif

int sock_init( char *_interface, char *_startIP, char *_endIP)
{
  int rv;
  uchar *pb;
  uchar val;
 
#ifdef WIN32
  DWORD rvl;
  rvl = WSAStartup(0x0101,&lan_ws);
  if (rvl != 0) {
      printerr("init: WSAStartup(1.1) error %ld\n", rvl);
      return((int)rvl);
  }
#else
  char findif;
#endif

  if ((g_sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SockInvalid) {
    printerr("socket: %s\n", showlasterr());
    return(-1);
  }

  memset(&_srcaddr, 0, sizeof(_srcaddr));
  _srcaddr.sin_family = AF_INET;
  _srcaddr.sin_port = htons(0);
  _srcaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 
#ifdef WIN32
  {
     int ret;
     uchar osip[4];
     uchar osmac[6];
     uchar osname[64];
     char ipstr[20];
     char *temp_start;
     if (fBroadcastOk && (g_startDest[0] == 0)) {
	  ret = GetFirstIP(osip,osmac,osname,fdebug);  /*ilan.c*/
	  if (ret == 0) {
		 // osip[3] = 0xFF;  /*255 for broadcast*/
		 sprintf(ipstr,"%d.%d.%d.255",osip[0],osip[1],osip[2]);
	         temp_start = ipstr;
		 strcpy(g_startDest,temp_start);
		 strcpy(g_endDest,temp_start);
	  } else {  /*use some defaults*/
		 strcpy(g_startDest,"255.255.255.255");
		 strcpy(g_endDest,"255.255.255.255");
	  }
     }
  }
#elif defined(HPUX)
  {  /*find the OS eth interface to use*/
   char devname[INET_ADDRSTRLEN+1];
   int i, n;
   n = 0;
   sprintf(devname,"lan%d",n);
  }
#else
  {  /*find the OS eth interface to use*/
   struct sockaddr_in temp_sockaddr;
   char *temp_start;
   struct ifreq ifr;
   char devname[INET_ADDRSTRLEN+1];
   int i, n;
   if (_interface == NULL) findif = 1;
   else if (_interface[0] == 0) findif = 1;
   else findif = 0;
   if (findif) {
      n = find_ifname(devname);
      if (n >= 0) {
         _interface = devname;
         findif = 0;
      }
   }
   if (findif)
   {   /* try again to find the first active ethN interface */
      n = -1;
      for (i = 0; (i < 16) && (n == -1); i++) {
#ifdef SOLARIS
          sprintf(devname,"e1000g%d",i);
#elif  BSD
          sprintf(devname,"em%d",i);
#else
          sprintf(devname,"eth%d",i);
#endif
          strcpy(ifr.ifr_name, devname);
          ifr.ifr_addr.sa_family = AF_INET;
          if (ioctl(g_sockfd, SIOCGIFADDR, &ifr) >= 0) {
	     /* valid IP address, so active interface, use it */
             temp_sockaddr = *((struct sockaddr_in *)&ifr.ifr_addr);
             memcpy(&_srcaddr.sin_addr.s_addr, &temp_sockaddr.sin_addr.s_addr,
                 sizeof(_srcaddr.sin_addr.s_addr));
             strcpy(g_interface, devname);  
	     temp_start = inet_ntoa(temp_sockaddr.sin_addr);
	     if (temp_start == NULL) temp_start = "";
	     else if (fBroadcastOk && (g_startDest[0] == 0)) {
		 pb = (uchar *)&temp_sockaddr.sin_addr.s_addr;
		 pb[3] = 0xFF;  /*255 for broadcast*/
	         temp_start = inet_ntoa(temp_sockaddr.sin_addr);
		 strcpy(g_startDest,temp_start);
		 strcpy(g_endDest,temp_start);
	     }
	     printf("sock_init: found %s with %s\n",devname,temp_start);
	     n = i;
	     break;
          }
      }
      if (n < 0) rv = LAN_ERR_OTHER; /*-13*/
   } else { /* valid _interface string */
      if (strchr(_interface, '.') != NULL)
        {   /* assume it is an IP address*/
          if ((rv = inet_pton(AF_INET, _interface, &_srcaddr.sin_addr)) < 0)
            printerr("inet_pton: %s\n", showlasterr());
          if (rv == 0) 
            printerr("invalid interface address\n");
          return(rv);
        }
      else
        {   /* assume interface name, like eth0 */
          strncpy(ifr.ifr_name, _interface, IFNAMSIZ);
          ifr.ifr_addr.sa_family = AF_INET;
          if (ioctl(g_sockfd, SIOCGIFADDR, &ifr) < 0) {
             printerr("ioctl(%s): %s\n", _interface, showlasterr());
             return(-1);
          }
 
          temp_sockaddr = *((struct sockaddr_in *)&ifr.ifr_addr);
          memcpy(&_srcaddr.sin_addr.s_addr, &temp_sockaddr.sin_addr.s_addr,
                 sizeof(_srcaddr.sin_addr.s_addr));
	  if (fBroadcastOk && (g_startDest[0] == 0)) {
		 pb = (uchar *)&temp_sockaddr.sin_addr.s_addr;
		 pb[3] = 0xFF;  /*255 for broadcast*/
	         temp_start = inet_ntoa(temp_sockaddr.sin_addr);
		 strcpy(g_startDest,temp_start);
		 strcpy(g_endDest,temp_start);
	  }
        }
    }
   }
#endif

   if (fBroadcastOk) {
        rv = setsockopt(g_sockfd, SOL_SOCKET, SO_BROADCAST,
                       (char *)&broadcast_pings, sizeof(broadcast_pings));
	if (rv) {
	   printerr("setsockopt: %s\n", showlasterr());
	   return(-1);
	}
   }
 
   if (bind(g_sockfd, (struct sockaddr *)&_srcaddr, sizeof(_srcaddr)) < 0) {
      printerr("bind: %s\n", showlasterr());
      return(-1);
   }
 
   rv = inet_aton(_startIP, &_startAddr);
   if (rv ) {
        _startAddr.s_addr = ntohl(_startAddr.s_addr);
        if (fdebug) show_ip(_startAddr.s_addr);
	pb = (unsigned char*)&_startAddr.s_addr;
        if (pb[0]  < 1)
                printerr("Malformed begin IP: %s\n", _startIP);
        else if (!fBroadcastOk && (pb[0] >254) )
                printerr("Malformed begin IP: %s\n", _startIP);
        else if (fBroadcastOk) {
		val = pb[0] & 0x0f;
		if (val == 0x0f) rv = 0;
                else printerr("Malformed begin broadcast IP: %s\n", _startIP);
        } else rv = 0;
   } else {
        printerr("Invalid begin IP: %s\n", _startIP);
   }
   if (rv) return(rv);
 
   rv = inet_aton(_endIP, &_endAddr);
   if (rv ) {
        _endAddr.s_addr = ntohl(_endAddr.s_addr);
        if (fdebug) show_ip(_endAddr.s_addr);
	pb = (unsigned char*)&_endAddr.s_addr;
        if (pb[0]  < 1)
              printerr("Malformed end IP: %s\n", _endIP);
        else if (!fBroadcastOk && (pb[0] >254) )
              printerr("Malformed end IP: %s\n", _endIP);
        else rv = 0;
   } else {
        printerr("Invalid end IP: %s\n", _endIP);
   }

   /* calculate g_num_packets */
   g_num_packets = ntoi(_endAddr.s_addr) - ntoi(_startAddr.s_addr) + 1;
   if (fdebug) printerr("g_num_packets = %d\n",g_num_packets);
   if (g_num_packets < 1) g_num_packets = 0;

   return(rv);
}  /*end sock_init*/

void *receiveThread(void *p)
{
   uchar buffer[IPMI_PING_MAX_LEN];
   struct timeval tv;
   fd_set rset;
   int rv, len;
   static int needlf = 0;
   char host[200];
   SockType sockrecv;
   int nothers = 0;
   int addr_type = AF_INET;   /*or AF_INET6*/
#ifndef WIN32
   struct hostent *h_ent = NULL;
   char serv[200];
   int r;
#endif

   sockrecv = g_sockfd;
   if (fraw) {  /* opening SOCK_RAW requires admin/root privilege. */
      if ((g_sockraw = socket(RAW_DOMAIN, SOCK_RAW,RAW_PROTO)) == SockInvalid) 
      {
        printerr("raw socket: %s\n", showlasterr());
	fraw = 0;
      } else {
	sockrecv = g_sockraw;
	if (fdebug) printf("g_sockraw = %d\n",g_sockraw);
      }
   }

   /* receive while loop */
   do
   {
      tv.tv_sec  = g_wait;
      tv.tv_usec = 0;
      FD_ZERO(&rset);
      FD_SET(sockrecv, &rset);
      g_recv_status = 0;

      if (fdebug) printerr("waiting for ping %d response\n",g_npings);
      if ((rv = select((int)(sockrecv+1), &rset, NULL, NULL, &tv)) < 0) {
          printerr("select: %s\n", showlasterr());
          cleanup();
          exit(rv);  // error, exit idiscover recv thread
      }
      
      if (fdebug) printerr("select rv = %d\n",rv);
      if (rv > 0)
          {
              struct sockaddr_in from;
              socklen_t fromlen;
	      char rstr[40];
	      char macstr[20];
	      struct in_addr from_ip;
	      char estr[40];
 
	      if (needlf) { printf("\n"); needlf = 0; }
              g_recv_status = 1;
              fromlen = sizeof(from);
              len = ipmilan_recvfrom(sockrecv, buffer, IPMI_PING_MAX_LEN, 0,
                                     (struct sockaddr *)&from, &fromlen);
              if (fdebug) printerr("recvfrom rv = %d\n",rv);
              if (len < 0) { 
		printerr("ipmilan_recvfrom: %s\n", showlasterr());
		continue; 
              }
              if (fdebug) {
		/* can we get the MAC addr of the responder also? */
		// dump_buf("from_addr",(uchar *)&from,fromlen,0);
		dump_buf("recvfrom",buffer,len,0);
	      }
              g_recv_status = 2;
              g_npongs++;
	      macstr[0] = 0;
	      if (fraw) {  /* Raw packet, include MAC address string */
		  /*
		   * Sample raw packet for UDP ping response
		   *       dst_mac           src_mac           eth_p iphdr
  		   * 0000: 00 07 e9 06 15 31 00 0e 0c 2b b5 81 08 00 45 00
		   *                            udp      src_ip      dst_ip
  		   * 0010: 00 38 00 00 40 00 40 11 b6 01 c0 a8 01 c2 c0 a8
		   *                                     rmcp
  		   * 0020: 01 a1 02 6f 80 1e 00 24 0e d1 06 00 ff 06 00 00
  		   * 0030: 11 be
		   */
		  if ((buffer[23] != 0x11) || (buffer[42] != 0x06)) { 
		     /* [23]: 0x11==UDP, 0x06==TCP ; [42]: 0x06 ==RMCP */
		     if (nothers > g_limit) {
			if (fdebug) 
			    printf("got %d other packets, stop.\n",nothers);
			break;
		     }
		     nothers++;
		     continue;
		  }
		  if (buffer[6] == 0xFF || buffer[26] == 0xFF) /*broadcast*/
		     continue;
                  sprintf(macstr,"%02x:%02x:%02x:%02x:%02x:%02x %c",
			buffer[6], buffer[7], buffer[8],  /*06=src_mac*/
			buffer[9], buffer[10], buffer[11], bdelim);
		  memcpy(&from_ip,&buffer[26],4);  /*26=src_ip*/
	      } else {
		  memcpy(&from_ip,&from.sin_addr,4);
	      }
              host[0] = 0;
#ifndef WIN32
/* Linux, BSD, Solaris, MacOS */
#if !defined(CROSS_COMPILE)
	      h_ent = gethostbyaddr((void *)&from_ip,4,addr_type);
	      if (h_ent != NULL) {
		 strncpy(host,h_ent->h_name,sizeof(host));
	      } else if (!fraw) 
#endif
	      {	 
                 r = getnameinfo((struct sockaddr *)&from, fromlen, 
			       host, sizeof(host), serv, sizeof(serv), 0);
	         if (r) host[0] = 0;
	         else if (host[0] >= '0' && host[0] <= '9') host[0] = 0;
	      }
#endif
              // parse the received pong 
	      rstr[0] = 0;
	      if (fping == 0 && len > 0) {   /* -g and got rsp data */
		 /* parse GetChanAuthcap response into rstr */
		 /* 4 bytes RMCP, 10 bytes IPMI session, then msg */
		 /* 6 bytes msg hdr, then rsp data */
		 /* 20 = ccode, 21 = chan, 22 = auth type support */
		 if (buffer[20] != 0)  /*ccode error*/
		    sprintf(rstr,"%c (ccode=0x%02x)",bdelim,buffer[20]);
		 else  /*ccode is ok*/
		    sprintf(rstr,"%c (channel %d)",bdelim,buffer[21]);
	      }
	      if (fcanonical) {
		 estr[0] = 0;
		 rstr[0] = 0;
	      } else {
		 sprintf(estr,"response from %c ",bdelim);
	      }
	      /* &buffer[0] = source MAC, &buffer[6] = destination MAC */
              printf("%.2d%c %s%s %s \t%c %s %s\n",
		    g_npongs,bdelim,estr,macstr,inet_ntoa(from_ip),
		    bdelim,host,rstr);
          }
      else {   /*ping, no answer*/
	  if (!fBroadcastOk) {
		printf("."); fflush(stdout); /*show progress*/
		needlf = 1;
	  }
      }
   }
#ifdef NO_THREADS
     while(fBroadcastOk && rv > 0);
#else
     while(1);
#endif
   return(p);
} 

/*
 * send_ping_pkt:
 * RMCP Ping buffer, sent as a UDP packet to port 0x026f.
 * rmcp.ver     = 0x06          // RMCP Version 1.0
 * rmcp.resvd   = 0x00          // RESERVED
 * rmcp.seq     = 0xff          // no RMCP ACK 
 * rmcp.class   = 0x06          // RMCP_CLASS_ASF
 * asf.iana     = 0x000011BE    // ASF_RMCP_IANA (hi-lo)
 * asf.type     = 0x80          // ASF_TYPE_PING
 * asf.tag      = 0x00          // ASF sequence number
 * asf.resvd    = 0x00          // RESERVED
 * asf.len      = 0x00
 */
int send_ping_pkt(struct sockaddr_in *_daddr, uchar seq)
{
   uchar pingbuf[SZ_PING] = {06,0,0xFF,06,0x00,0x00,0x11,0xBE,0x80,0,0,0 };
   int rv, len;

   pingbuf[9] = seq;
   len = sizeof(pingbuf);
   if (fdebug) dump_buf("send_ping",pingbuf,len,0);
   rv = ipmilan_sendto(g_sockfd, pingbuf, len, 0, 
	          (struct sockaddr *)_daddr, sizeof(struct sockaddr_in));
   return(rv);
}

static int send_poke1(struct sockaddr_in *_daddr)
{
   int rv;
   uchar asfpkt[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        0x07, 0x20, 0x18, 0xc8, 0xc2, 0x01, 0x01, 0x3c };
   if (fdebug) dump_buf("send_poke1",asfpkt,16,0);
   rv = ipmilan_sendto(g_sockfd, asfpkt, 16, 0, 
	          (struct sockaddr *)_daddr, sizeof(struct sockaddr_in));
   return rv;
}

static uchar cksum(const uchar *buf, register int len)
{
        register uchar csum;
        register int i;
 
        /* 8-bit 2s compliment checksum */
        csum = 0;
        for (i = 0; i < len; i++)
           csum = (csum + buf[i]) % 256;
        csum = -csum;
        return(csum);
}

static int send_getauth(struct sockaddr_in *_daddr, uchar seq)
{
   int rv, len;
   // static uchar swseq = 0;
   uchar getauthpkt[23] = { 0x06, 0x00, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 
   		            0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x20, 0x18, 
   			    0xc8, 0x81, 0x04, 0x38, 0x0e, 0x04, 0x31 };
   /* 
    *  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
    * 06 00 ff 07 00 00 00 00 00 00 00 00 00 09 20 18 c8 81 04 38 0e 04 31 
    * [RMCP hdr ] [IPMI session hdr      (len)] [IPMI msg       ] [data]
    */
   // getauthpkt[8]  = 0x00;  /* seq always 00 for GetChanAuthCap */
   getauthpkt[21] = 0x02;  /*requested priv_level: 2=user, 4=admin*/
   getauthpkt[22] = cksum(&getauthpkt[17],5);
   len = sizeof(getauthpkt);
   if (fdebug) dump_buf("send_getauth",getauthpkt,len,0);
   rv = ipmilan_sendto(g_sockfd, getauthpkt, len, 0, 
   	          (struct sockaddr *)_daddr, sizeof(struct sockaddr_in));
   if (fdebug) 
       printf("send_getauth: rv = %d\n",rv);
   return rv;
}

int send_probe(struct sockaddr_in *_daddr, uchar seq)
{
    int rv;

    if (fBroadcastOk) {
       rv = setsockopt(g_sockfd, SOL_SOCKET, SO_BROADCAST,
                       (char *)&broadcast_pings, sizeof(broadcast_pings));
       if (fdebug) {
	  char *estr;
	  if (rv) estr = showlasterr();
	  else estr = "";
	  printerr("setsockopt(broadcast): rv=%d %s\n", rv,estr);
       }
    }
    if (fping) 
       rv = send_ping_pkt( _daddr, seq);
    else 
       rv = send_getauth( _daddr, seq);
    return(rv);
}

void *sendThread(void *p)
{
   int i, j, n;
   // char _dest[MAXHOSTNAMELEN+1];
   char _dest_ip[INET_ADDRSTRLEN+1];
   struct sockaddr_in _destaddr;
   uchar o[4];
   uint ip;
   uchar _seq;
   int rv;

   n = g_num_packets;  /*num*/
   ip = _startAddr.s_addr;

   for (i = 0; i < n; i++) 
   {
      split_ip(ip,o);
      if (o[3] == 0) continue;
      if (!fBroadcastOk && (o[3] == 255)) continue;
      sprintf(_dest_ip,"%d.%d.%d.%d",o[0],o[1],o[2],o[3]);

      /* set _destaddr */
      _destaddr.sin_family = AF_INET;
      _destaddr.sin_port = htons(g_port);
      if ( !inet_aton( _dest_ip, &_destaddr.sin_addr)) {
          printerr("inet_aton error %s\n",_dest_ip);
          continue;
      }

     for (j=0; j<g_repeat; j++)
     {
      /* send ping buffer */
      _seq = 0;
      rv = send_probe(&_destaddr,_seq);
      g_npings++;
      if (fdebug) printerr("sendto[%d,%d] %s rv = %d\n",
			    g_npings,_seq,_dest_ip,rv);
      if (rv < 0) {  /*try to send again*/
          rv = send_probe(&_destaddr,++_seq);
          if (rv < 0) {
              printerr("sendto[%d,%d] %s error %s\n",
			g_npings,_seq,_dest_ip,showlasterr()); 
              continue;
          }
      } 

#ifdef NO_THREADS
      receiveThread(NULL);
      if (g_recv_status == 0 && !fBroadcastOk) {
          /* nothing returned, try again */
          if (fping) {
             rv = send_poke1(&_destaddr);
             if (fdebug) printerr("sendto[%d,%d] %s poke rv = %d\n",
				   g_npings,_seq,_dest_ip,rv);
          }
          rv = send_probe(&_destaddr,++_seq);
          if (fdebug) printerr("sendto[%d,%d] %s rv = %d\n",
				g_npings,_seq,_dest_ip,rv);
          if (rv >= 0) {
             receiveThread(NULL);
          }
      }
#endif

      /* sleep an interval (g_delay usec) */
      if (g_delay > 0) os_sleep(0,g_delay);
     }  /*end-for g_repeat*/

     ip++; /* increment to next IP */
   }
   return(p);
}

#ifdef METACOMMAND
int i_discover(int argc, char **argv)
#else
#ifdef WIN32
int __cdecl
#else
int
#endif
main(int argc, char **argv)
#endif
{
   int rv = 0;
   int c;
#ifndef NO_THREADS
   char message[32];
   pthread_t thread[2];
   int  iret[2];
#endif

#ifdef METACOMMAND
   fpdbg = stdout;
#endif
   printf("%s ver %s\n", progname,progver);

   while ( (c = getopt( argc, argv,"ab:ce:gi:l:mp:r:s:x?")) != EOF ) 
      switch(c) {
          case 'a': fBroadcastOk = 1; fping = 1;
		break;  /*all (broadcast ping)*/
	  case 'c':  /*canonical, CSV*/
		fcanonical = 1;
		bdelim = BCOMMA;
		break; 
          case 'm':  /* show MAC address, use raw, requires root priv */
		fBroadcastOk = 1; fping = 1; fraw = 1; 
		break;  /*all (broadcast ping)*/
          case 'l': g_limit = atoi(optarg); break;
          case 'g': fping = 0; break;   /*use get chan auth cap method */
          case 'b':   /*begin*/
                strncpy(g_startDest,optarg,MAXHOSTNAMELEN);
		break;
          case 'e':   /*end*/
                strncpy(g_endDest,optarg,MAXHOSTNAMELEN);
		break;
          case 'i':   /*interface*/
                strncpy(g_interface,optarg,sizeof(g_interface));
		break;
          case 'p':   /*port/socket*/
                g_port = (ushort)atoi(optarg);
		break;
          case 'r':   /*repeat N times*/
                g_repeat = atoi(optarg);
		break;
          case 's':   /*subnet*/
                /* copy optarg from 10.243.42.0 or similar, to
                 * begin/end range. */
		break;
          case 'x': fdebug = 1;     break;  /* debug messages */
	  default:
		if (fdebug) printerr("getopt(%c) default\n",c);
                show_usage();
                rv = ERR_USAGE;
		goto do_exit;
      }
#ifdef WIN32
   /* Winsock inet_aton() does not like 255.255.255.255 */
   if (!fBroadcastOk && (g_startDest[0] == 0) ) {
      show_usage();
      printerr("A beginning IP is required, using -b\n");
      goto do_exit;
   }
#else
   if (g_startDest[0] == 0)  {
      strcpy(g_startDest,"255.255.255.255"); /* INADDR_BROADCAST */
      fBroadcastOk = 1;  
   }
#endif
    if (fraw == 1) {
      if (frawok == 0) {
	printf("Warning: SOCK_RAW not yet implemented on this OS\n");
      } 
#ifdef LINUX
     else {
        c = geteuid();
        if (c > 1) printf("Must be root/superuser to use SOCK_RAW\n");
      }
#endif
   }
   if (g_endDest[0] == 0 || fBroadcastOk) 
      strcpy(g_endDest,g_startDest);   /*only one IP address*/
   if (fdebug) 
      printerr("intf=%s begin=%s end=%s port=%d\n",
		g_interface,g_startDest,g_endDest,g_port);

   rv = sock_init(g_interface, g_startDest, g_endDest); 
   if (fdebug) printerr("sock_init rv = %d, sockfd = %d\n",rv,g_sockfd);
   if (rv != 0) {
      show_usage();
      printerr("sock_init error %d\n",rv);
      goto do_exit;
   }

   printf("Discovering IPMI Devices:\n");
#ifdef NO_THREADS
   sendThread(NULL);
#else
   iret[1] = pthread_create( &thread[1], NULL, receiveThread, (void*) message);
   iret[0] = pthread_create( &thread[0], NULL, sendThread, (void*) message);
   pthread_join( thread[0], NULL);
   pthread_join( thread[1], NULL);
#endif

   // if (fdebug) 
   printf("\n%s: %d pings sent, %d responses\n",progname,g_npings,g_npongs);

do_exit:
   cleanup();
   return(rv);
}  /* end main()*/

/* end idiscover.c */