]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libcharon/plugins/load_tester/load_tester.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libcharon / plugins / load_tester / load_tester.c
CommitLineData
965f7bd5
MW
1/*
2 * Copyright (C) 2012 Martin Willi
19ef2aec
TB
3 *
4 * Copyright (C) secunet Security Networks AG
965f7bd5
MW
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17#include "load_tester_control.h"
18
19#include <sys/socket.h>
20#include <sys/un.h>
21#include <unistd.h>
22#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
98c09357 25#include <string.h>
965f7bd5
MW
26#include <errno.h>
27
28/**
29 * Connect to the daemon, return stream
30 */
31static FILE* make_connection()
32{
33 struct sockaddr_un addr;
34 FILE *stream;
35 int fd;
36
37 addr.sun_family = AF_UNIX;
38 strcpy(addr.sun_path, LOAD_TESTER_SOCKET);
39
73da4ed8 40 fd = socket(AF_UNIX, SOCK_STREAM, 0);
965f7bd5
MW
41 if (fd < 0)
42 {
43 fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
44 return NULL;
45 }
46 if (connect(fd, (struct sockaddr *)&addr,
47 offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)) < 0)
48 {
49 fprintf(stderr, "connecting to %s failed: %s\n",
50 LOAD_TESTER_SOCKET, strerror(errno));
51 close(fd);
52 return NULL;
53 }
54 stream = fdopen(fd, "r+");
55 if (!stream)
56 {
57 close(fd);
58 return NULL;
59 }
60 return stream;
61}
62
63/**
64 * Initiate load-tests
65 */
fdd94fc8 66static int initiate(unsigned int count, unsigned int delay)
965f7bd5
MW
67{
68 FILE *stream;
5947d48f 69 int c;
965f7bd5
MW
70
71 stream = make_connection();
72 if (!stream)
73 {
74 return 1;
75 }
76
fdd94fc8 77 fprintf(stream, "%u %u\n", count, delay);
965f7bd5
MW
78
79 while (1)
80 {
81 fflush(stream);
82 c = fgetc(stream);
83 if (c == EOF)
84 {
85 break;
86 }
87 if (fputc(c, stdout) == EOF)
88 {
89 break;
90 }
91 fflush(stdout);
92 }
93 fclose(stream);
94 return 0;
95}
96
97int main(int argc, char *argv[])
98{
fdd94fc8 99 if (argc >= 3 && strcmp(argv[1], "initiate") == 0)
965f7bd5 100 {
fdd94fc8 101 return initiate(atoi(argv[2]), argc > 3 ? atoi(argv[3]) : 0);
965f7bd5
MW
102 }
103 fprintf(stderr, "Usage:\n");
fdd94fc8 104 fprintf(stderr, " %s initiate <count> [<delay in ms>]\n", argv[0]);
965f7bd5
MW
105 return 1;
106}