forked from z-classic/z-nomp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocknotify.c
84 lines (61 loc) · 1.84 KB
/
blocknotify.c
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
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/*
Contributed by Alex Petrov aka SysMan at sysman.net
Updated by Alejandro Reyero - TodoJuegos.com
Part of NOMP project
Simple lightweight & fast - a more efficient block notify script in pure C.
(may also work as coin switch)
Platforms : Linux, BSD, Solaris (mostly OS independent)
Build with:
gcc blocknotify.c -o blocknotify
Example usage in daemon coin.conf using default NOMP CLI port of 17117
blocknotify="/bin/blocknotify 127.0.0.1:17117 dogecoin %s"
*/
int main(int argc, char **argv)
{
int sockfd,n;
struct sockaddr_in servaddr, cliaddr;
char sendline[1000];
char recvline[1000];
char host[200];
char *p, *arg, *errptr;
int port;
if (argc < 3)
{
// print help
printf("NOMP pool block notify\n usage: <host:port> <coin> <block>\n");
exit(1);
}
strncpy(host, argv[1], (sizeof(host)-1));
p = host;
if ( (arg = strchr(p,':')) )
{
*arg = '\0';
errno = 0; // reset errno
port = strtol(++arg, &errptr, 10);
if ( (errno != 0) || (errptr == arg) )
{
fprintf(stderr, "port number fail [%s]\n", errptr);
}
}
snprintf(sendline, sizeof(sendline) - 1, "{\"command\":\"blocknotify\",\"params\":[\"%s\",\"%s\"]}\n", argv[2], argv[3]);
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(host);
servaddr.sin_port = htons(port);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
int result = send(sockfd, sendline, strlen(sendline), 0);
close(sockfd);
if(result == -1) {
printf("Error sending: %i\n", errno);
exit(-1);
}
exit(0);
}