-
Notifications
You must be signed in to change notification settings - Fork 56
/
common-client.c
159 lines (145 loc) · 4.74 KB
/
common-client.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
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
158
159
/*
* Copyright (c) 2011 Vincent Bernat <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Common client functions */
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* Display usage for clients and exit */
static void
usage(char * const name) {
fail("Usage: %s [-r] [-R {number}] [-d {secs}] [-S] [-T] [-C {client_cert}] [-K {client_key}] [-U URI ] [-M METHOD] host port\n"
"\n"
" Connect to an SSL HTTP server and requests `/'\n"
"\n"
"Options:\n"
"\t-r: reconnect (may be repeated)\n"
"\t-R: number of reconnects\n"
"\t-d: delay between each renegotiation in seconds\n"
"\t-S: disable support for session identifier\n"
"\t-T: disable support for tickets\n"
"\t-C: use a client certificate for the connection and this specifies a certificate as a file in PEM format. Optionally the key can be here too\n"
"\t-K: use the key {client_key}, a PEM formated key file, in the connection\n"
"\t-U: use a different URI\n"
"\t-M: use a different method\n"
, name);
}
/* Parse arguments and call back connect function */
int client(int argc, char * const argv[],
int (*connect)(char *, char *, int, int, int, int,
const char *, const char *, const char *,const char *)) {
int opt, status;
int reconnect = 0;
int use_sessionid = 1;
int use_ticket = 1;
int delay = 0;
char *host = NULL;
char *port = NULL;
const char *client_cert = NULL;
const char *client_key = NULL;
const char *opt_uri = "/";
const char *opt_method = "GET";
/* Parse arguments */
opterr = 0;
start("Parse arguments");
while ((opt = getopt(argc, argv, "rR:d:STC:K:U:M:")) != -1) {
switch (opt) {
case 'r':
reconnect++;
break;
case 'R':
reconnect = atoi(optarg);
break;
case 'S':
use_sessionid = 0;
break;
case 'T':
use_ticket = 0;
break;
case 'd':
delay = atoi(optarg);
break;
case 'C':
client_cert = optarg;
break;
case 'K':
client_key = optarg;
break;
case 'U':
opt_uri = optarg;
break;
case 'M':
opt_method = optarg;
break;
default:
usage(argv[0]);
}
}
if (client_key && !client_cert) {
fail("a client key_file is specified without a client_certificate file. If both are in the same file use -C");
}
if (client_cert && !client_key) {
client_key = client_cert;
}
if (optind != argc - 2)
usage(argv[0]);
host = argv[optind];
port = argv[optind + 1];
/* Callback */
status = connect(host, port, reconnect, use_sessionid, use_ticket, delay, client_cert, client_key, opt_uri, opt_method);
end(NULL);
return status;
}
struct addrinfo *
solve(char *host, char *port) {
int err;
char name[INET6_ADDRSTRLEN];
struct addrinfo hints;
struct addrinfo *result;
start("Solve %s:%s", host, port);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
if ((err = getaddrinfo(host, port, &hints, &result)) != 0)
fail("Unable to solve ‘%s:%s’:\n%s", host, port, gai_strerror(err));
if ((err = getnameinfo(result->ai_addr, result->ai_addrlen,
name, sizeof(name), NULL, 0,
NI_NUMERICHOST)) != 0)
fail("Unable to format ‘%s:%s’:\n%s", host, port, gai_strerror(err));
end("Will connect to %s", name);
return result;
}
int
connect_socket(struct addrinfo *result, char *host, char *port) {
int s, err;
start("Connect to %s:%s", host, port);
if ((s = socket(result->ai_family,
result->ai_socktype,
result->ai_protocol)) == -1)
fail("Unable to create socket:\n%m");
if ((err = connect(s, result->ai_addr, result->ai_addrlen)) == -1)
fail("Unable to connect to ‘%s:%s’:\n%m", host, port);
return s;
}