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>
30 static char *client
= NULL
;
31 static const char *ip
= NULL
;
32 static char *server
= NULL
;
33 static int stateval
= 0;
34 static int clientport
= 0;
35 static int sendport
= 0;
37 static void useage(void)
42 static int create_socket(const char *addr
, int port
)
45 struct sockaddr_in sock_in
;
47 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
49 printf("Failed to open local socket\n");
53 bzero(&sock_in
, sizeof(sock_in
));
54 sock_in
.sin_family
= AF_INET
;
55 sock_in
.sin_port
= htons(port
);
56 inet_aton(addr
, &sock_in
.sin_addr
);
57 if (bind(s
, (struct sockaddr
*)&sock_in
, sizeof(sock_in
)) == -1) {
58 printf("Failed to bind to local socket\n");
65 int main(int argc
, const char *argv
[])
67 struct poptOption popt_options
[] = {
69 { "client", 'c', POPT_ARG_STRING
, &client
, 0, "remote client to send the notify to", "hostname/ip" },
70 { "clientport", 0, POPT_ARG_INT
, &clientport
, 0, "clientport", "integer" },
71 { "ip", 'i', POPT_ARG_STRING
, &ip
, 0, "local ip address to send the notification from", "ip" },
72 { "sendport", 0, POPT_ARG_INT
, &sendport
, 0, "port to send the notify from", "integer" },
73 { "server", 's', POPT_ARG_STRING
, &server
, 0, "servername to use in the notification", "hostname/ip" },
74 { "stateval", 0, POPT_ARG_INT
, &stateval
, 0, "stateval", "integer" },
81 struct sockaddr_in sock_cl
;
85 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
87 while ((opt
= poptGetNextOpt(pc
)) != -1) {
90 fprintf(stderr
, "Invalid option %s: %s\n",
91 poptBadOption(pc
, 0), poptStrerror(opt
));
97 printf("ERROR: client not specified\n");
102 printf("ERROR: ip not specified\n");
106 if (server
== NULL
) {
107 printf("ERROR: server not specified\n");
112 printf("ERROR: stateval not specified\n");
117 /* Since we want to control from which address these packets are
118 sent we must create the socket ourself and use low-level rpc
121 s
= create_socket(ip
, sendport
);
123 /* only wait for at most 3 seconds before giving up */
126 /* Setup a sockaddr_in for the client we want to notify */
127 bzero(&sock_cl
, sizeof(sock_cl
));
128 sock_cl
.sin_family
= AF_INET
;
129 sock_cl
.sin_port
= htons(clientport
);
130 inet_aton(client
, &sock_cl
.sin_addr
);
135 clnt
= clntudp_create(&sock_cl
, 100024, 1, w
, &s
);
137 printf("ERROR: failed to connect to client\n");
141 /* we don't want to wait for any reply */
144 clnt_control(clnt
, CLSET_TIMEOUT
, (char *)&w
);
148 sm_notify_1(&st
, clnt
);