summaryrefslogtreecommitdiffstats
path: root/tests/diagtalker.c
blob: 6a721e474608273bc24d312686d11e58633850ee (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
/* A yet very simple tool to talk to imdiag (this replaces the
 * previous Java implementation in order to get fewer dependencies).
 *
 * Copyright 2010 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of rsyslog.
 *
 * Rsyslog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rsyslog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>


static char *targetIP = "127.0.0.1";
static int targetPort = 13500;


/* open a single tcp connection
 */
int openConn(int *fd)
{
	int sock;
	struct sockaddr_in addr;
	int port;
	int retries = 0;

	if((sock=socket(AF_INET, SOCK_STREAM, 0))==-1) {
		perror("socket()");
		exit(1);
	}

	port = targetPort;
	memset((char *) &addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	if(inet_aton(targetIP, &addr.sin_addr)==0) {
		fprintf(stderr, "inet_aton() failed\n");
		exit(1);
	}
	while(1) { /* loop broken inside */
		if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
			break;
		} else {
			if(retries++ == 50) {
				perror("connect()");
				fprintf(stderr, "connect() failed\n");
				exit(1);
			} else {
				fprintf(stderr, "connect failed, retrying...\n");
				usleep(100000); /* ms = 1000 us! */
			}
		}
	} 

	*fd = sock;
	return 0;
}


/* send a string
 */
static void
sendCmd(int fd, char *buf, int len)
{
	int lenSend;

	lenSend = send(fd, buf, len, 0);
	if(lenSend != len) {
		perror("sending string");
		exit(1);
	}
}


/* wait for a response from remote system
 */
static void
waitRsp(int fd, char *buf, int len)
{
	int ret;

	ret = recv(fd, buf, len - 1, 0);
	if(ret < 0) {
		perror("receiving response");
		exit(1);
	}
	/* we assume the message was complete, it may be better to wait
	 * for a LF...
	 */
	buf[ret] = '\0';
}


/* do the actual processing
 */
static void
doProcessing()
{
	int fd;
	int len;
	char line[2048];

	openConn(&fd);
	while(!feof(stdin)) {
		if(fgets(line, sizeof(line) - 1, stdin) == NULL)
			break;
		len = strlen(line);
		sendCmd(fd, line, len);
		waitRsp(fd, line, sizeof(line));
		printf("imdiag: %s", line);
	}
}


/* Run the test.
 * rgerhards, 2009-04-03
 */
int main(int argc, char *argv[])
{
	int ret = 0;
	int opt;

	while((opt = getopt(argc, argv, "f:t:p:c:C:m:i:I:P:d:n:M:rB")) != -1) {
		switch (opt) {
		case 't':	targetIP = optarg;
				break;
		case 'p':	targetPort = atoi(optarg);
				break;
		default:	printf("invalid option '%c' or value missing - terminating...\n", opt);
				exit (1);
				break;
		}
	}

	doProcessing();

	exit(ret);
}