-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsget.c
More file actions
137 lines (109 loc) · 3.4 KB
/
httpsget.c
File metadata and controls
137 lines (109 loc) · 3.4 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <ctype.h>
#include "ssl.h"
char request[65536];
char response[65536];
char buffer[4096];
int main(int argc, char **argv)
{
int client_fd;
struct sockaddr_in server_addr;
struct hostent *host_entry;
char *hostname = NULL;
char *path = NULL;
int port = 0;
int bytes_sent, bytes_received;
int total_received = 0;
int status_code;
char *html_body;
int i;
// デフォルト値のリセット(オプション処理後)
hostname = "www.yahoo.co.jp";
path = "/";
port = 443;
if (argc >= 2) {
hostname = argv[1];
}
if (argc >= 3) {
path = argv[2];
}
printf("Connecting to %s:%d%s\n", hostname, port, path);
// ホスト名の解決
host_entry = gethostbyname(hostname);
if (host_entry == NULL) {
printf("gethostbyname() failed for %s\n", hostname);
return 1;
}
printf("Host resolved to: %s\n", inet_ntoa(*(struct in_addr*)host_entry->h_addr_list[0]));
// ソケットの作成
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket() failed");
return 1;
}
printf("Socket created successfully\n");
// サーバーアドレスの設定
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
memcpy(&server_addr.sin_addr, host_entry->h_addr_list[0], host_entry->h_length);
// サーバーに接続
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect() failed");
close(client_fd);
return 1;
}
printf("Connected to server successfully\n");
SSL_CTX *ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER, SSL_DEFAULT_CLNT_SESS);
SSL_EXTENSIONS *ext = ssl_ext_new();
SSL *ssl_sock = ssl_client_new(ssl_ctx, client_fd, NULL, 0, ext);
int r = ssl_handshake_status(ssl_sock);
printf("Handshake status: %d\n", r);
// HTTP GETリクエストの作成
snprintf(request, sizeof(request),
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
"User-Agent: X68000-HTTPS-Client/1.0\r\n"
"Accept: */*\r\n"
"Connection: close\r\n"
"\r\n",
path, hostname);
printf("Sending HTTP request:\n");
printf("--------------------\n");
printf("%s", request);
printf("--------------------\n\n");
// HTTPリクエストを送信
bytes_sent = ssl_write(ssl_sock, request, strlen(request));
if (bytes_sent < 0) {
perror("send() failed");
close(client_fd);
return 1;
}
printf("Sent %d bytes\n", bytes_sent);
printf("Receiving HTTP response...\n\n");
// HTTPレスポンスを受信してバッファに蓄積
memset(response, 0, sizeof(response));
total_received = 0;
// bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
uint8_t *ptr;
int res;
while (1) {
res = ssl_read(ssl_sock, &ptr);
if (res < 0) {
break;
}
// printf("Received %d bytes\n", res);
write(1, ptr, res);
}
ssl_free(ssl_sock);
ssl_ctx_free(ssl_ctx);
return 0;
}