Add missing files
[bcusdk.git] / eibd / eibnet / eibnetsearch.cpp
blobdd74114460432872e84943c2534e0e14f5cb7b0a
1 /*
2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <arpa/inet.h>
25 #include "eibnetip.h"
27 /** aborts program with a printf like message */
28 void
29 die (const char *msg, ...)
31 va_list ap;
32 va_start (ap, msg);
33 vprintf (msg, ap);
34 printf ("\n");
35 va_end (ap);
37 exit (1);
40 void
41 HexDump (const uchar * data, int Len)
43 for (int i = 0; i < Len; i++)
44 printf (" %02X", data[i]);
45 printf ("\n");
48 int
49 main (int ac, char *ag[])
51 int tracelevel;
52 int sport;
53 int dport;
54 bool shortlist = false;
55 char *a, *b, *c;
56 if (ac != 2 && ac != 3)
57 die
58 ("Usage: %s [/]ip[:dst-port[:src-port]] [tracelevel] use - as default ip",
59 ag[0]);
60 struct sockaddr_in saddr;
61 struct sockaddr_in caddr;
62 EIBNetIPSocket *sock;
63 pth_event_t timeout = pth_event (PTH_EVENT_RTIME, pth_time (10, 0));
65 pth_init ();
66 tracelevel = 0;
67 if (ac == 3)
68 tracelevel = atoi (ag[2]);
69 a = strdup (ag[1]);
70 if (!a)
71 die ("out of memory");
72 if (*a == '/')
74 a++;
75 shortlist = true;
77 for (b = a; *b; b++)
78 if (*b == ':')
79 break;
80 sport = 3672;
81 if (*b == ':')
83 *b = 0;
84 for (c = b + 1; *c; c++)
85 if (*c == ':')
86 break;
87 if (*c == ':')
89 *c = 0;
90 sport = atoi (c + 1);
92 dport = atoi (b + 1);
94 else
95 dport = 3671;
97 Trace t;
98 t.SetTraceLevel (tracelevel);
99 if (!strcmp (a, "-"))
100 a = (char *) "224.0.23.12";
102 printf ("Asking %s at port %d from port %d\n\n", a, dport, sport);
104 if (!GetHostIP (&caddr, a))
105 die ("Host not found");
106 caddr.sin_port = htons (dport);
107 if (!GetSourceAddress (&caddr, &saddr))
108 die ("No route found");
109 saddr.sin_port = htons (sport);
110 sock = new EIBNetIPSocket (saddr, 0, &t);
111 sock->sendaddr = caddr;
112 sock->recvaddr = caddr;
113 sock->recvall = 1;
114 if (!sock->init ())
115 die ("IP initialisation failed");
117 EIBnet_SearchRequest req;
118 EIBnet_SearchResponse resp;
119 EIBNetIPPacket *p1;
120 req.caddr = saddr;
121 sock->Send (req.ToPacket ());
124 p1 = sock->Get (timeout);
125 if (p1)
127 if (parseEIBnet_SearchResponse (*p1, resp))
129 printf ("Invalid Search response\n");
130 continue;
132 printf ("Answer from %s at port %d\n",
133 inet_ntoa (resp.caddr.sin_addr),
134 ntohs (resp.caddr.sin_port));
135 if (shortlist)
137 printf ("Addr: %s\n", FormatEIBAddr (resp.individual_addr) ());
138 printf ("Name: %s\n", resp.name);
140 else
142 printf
143 ("Medium: %d\nState: %d\nAddr: %s\nInstallID: %d\nSerial:",
144 resp.KNXmedium, resp.devicestatus,
145 FormatEIBAddr (resp.individual_addr) (), resp.installid);
146 HexDump (resp.serial, sizeof (resp.serial));
147 printf ("Multicast-Addr: %s\nMAC:",
148 inet_ntoa (resp.multicastaddr));
149 HexDump (resp.MAC, sizeof (resp.MAC));
150 printf ("Name: %s\n", resp.name);
151 for (int i = 0; i < resp.services (); i++)
152 printf ("Service %d Version %d\n", resp.services[i].family,
153 resp.services[i].version);
155 printf ("\n");
158 while (p1);
159 delete sock;
160 return 0;