4 Copyright (C) Ronnie Sahlberg 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
29 static char *client = NULL;
30 static const char *ip = NULL;
31 static char *server = NULL;
32 static int stateval = 0;
33 static int clientport = 0;
34 static int sendport = 0;
36 static void useage(void)
41 static int create_socket(const char *addr, int port)
44 struct sockaddr_in sock_in;
46 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
48 printf("Failed to open local socket\n");
52 bzero(&sock_in, sizeof(sock_in));
53 sock_in.sin_family = PF_INET;
54 sock_in.sin_port = htons(port);
55 inet_aton(addr, &sock_in.sin_addr);
56 if (bind(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) == -1) {
57 printf("Failed to bind to local socket\n");
64 int main(int argc, const char *argv[])
66 struct poptOption popt_options[] = {
68 { "client", 'c', POPT_ARG_STRING, &client, 0, "remote client to send the notify to", "hostname/ip" },
69 { "clientport", 0, POPT_ARG_INT, &clientport, 0, "clientport", "integer" },
70 { "ip", 'i', POPT_ARG_STRING, &ip, 0, "local ip address to send the notification from", "ip" },
71 { "sendport", 0, POPT_ARG_INT, &sendport, 0, "port to send the notify from", "integer" },
72 { "server", 's', POPT_ARG_STRING, &server, 0, "servername to use in the notification", "hostname/ip" },
73 { "stateval", 0, POPT_ARG_INT, &stateval, 0, "stateval", "integer" },
80 struct sockaddr_in sock_cl;
84 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
86 while ((opt = poptGetNextOpt(pc)) != -1) {
89 fprintf(stderr, "Invalid option %s: %s\n",
90 poptBadOption(pc, 0), poptStrerror(opt));
96 printf("ERROR: client not specified\n");
101 printf("ERROR: ip not specified\n");
105 if (server == NULL) {
106 printf("ERROR: server not specified\n");
111 printf("ERROR: stateval not specified\n");
116 /* Since we want to control from which address these packets are
117 sent we must create the socket ourself and use low-level rpc
120 s = create_socket(ip, sendport);
122 /* only wait for at most 3 seconds before giving up */
125 /* Setup a sockaddr_in for the client we want to notify */
126 bzero(&sock_cl, sizeof(sock_cl));
127 sock_cl.sin_family = PF_INET;
128 sock_cl.sin_port = htons(clientport);
129 inet_aton(client, &sock_cl.sin_addr);
134 clnt = clntudp_create(&sock_cl, 100024, 1, w, &s);
136 printf("ERROR: failed to connect to client\n");
140 /* we dont want to wait for any reply */
143 clnt_control(clnt, CLSET_TIMEOUT, (char *)&w);
147 sm_notify_1(&st, clnt);