1 /*
2 * Copyright (c) 2017 Linaro Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #if !defined(__ZEPHYR__)
11
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <unistd.h>
16 #include <netdb.h>
17
18 #else
19
20 #include <zephyr/net/socket.h>
21 #include <zephyr/kernel.h>
22
23 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
24 #include <zephyr/net/tls_credentials.h>
25 #include "ca_certificate.h"
26 #endif
27
28 #include "net_sample_common.h"
29
30 #endif
31
32 /* HTTP server to connect to */
33 #define HTTP_HOST "google.com"
34 /* Port to connect to, as string */
35 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
36 #define HTTP_PORT "443"
37 #else
38 #define HTTP_PORT "80"
39 #endif
40 /* HTTP path to request */
41 #define HTTP_PATH "/"
42
43 #define SSTRLEN(s) (sizeof(s) - 1)
44 #define CHECK(r) { if (r < 0) { printf("Error: %d\n", (int)r); exit(1); } }
45
46 #define REQUEST "GET " HTTP_PATH " HTTP/1.1\r\nHost: " HTTP_HOST "\r\n\r\n"
47
48 static char response[1024];
49
dump_addrinfo(const struct addrinfo * ai)50 void dump_addrinfo(const struct addrinfo *ai)
51 {
52 printf("addrinfo @%p: ai_family=%d, ai_socktype=%d, ai_protocol=%d, "
53 "sa_family=%d, sin_port=%x\n",
54 ai, ai->ai_family, ai->ai_socktype, ai->ai_protocol, ai->ai_addr->sa_family,
55 ntohs(((struct sockaddr_in *)ai->ai_addr)->sin_port));
56 }
57
main(void)58 int main(void)
59 {
60 static struct addrinfo hints;
61 struct addrinfo *res;
62 int st, sock;
63
64 wait_for_network();
65
66 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
67 tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
68 ca_certificate, sizeof(ca_certificate));
69 #endif
70
71 printf("Preparing HTTP GET request for http://" HTTP_HOST
72 ":" HTTP_PORT HTTP_PATH "\n");
73
74 hints.ai_family = AF_INET;
75 hints.ai_socktype = SOCK_STREAM;
76 st = getaddrinfo(HTTP_HOST, HTTP_PORT, &hints, &res);
77 printf("getaddrinfo status: %d\n", st);
78
79 if (st != 0) {
80 printf("Unable to resolve address, quitting\n");
81 return 0;
82 }
83
84 #if 0
85 for (; res; res = res->ai_next) {
86 dump_addrinfo(res);
87 }
88 #endif
89
90 dump_addrinfo(res);
91
92 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
93 sock = socket(res->ai_family, res->ai_socktype, IPPROTO_TLS_1_2);
94 #else
95 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
96 #endif
97 CHECK(sock);
98 printf("sock = %d\n", sock);
99
100 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
101 sec_tag_t sec_tag_opt[] = {
102 CA_CERTIFICATE_TAG,
103 };
104 CHECK(setsockopt(sock, SOL_TLS, TLS_SEC_TAG_LIST,
105 sec_tag_opt, sizeof(sec_tag_opt)));
106
107 CHECK(setsockopt(sock, SOL_TLS, TLS_HOSTNAME,
108 HTTP_HOST, sizeof(HTTP_HOST)))
109 #endif
110
111 printf("Connecting to server...\n");
112 CHECK(connect(sock, res->ai_addr, res->ai_addrlen));
113 printf("Connected!\r\nSending request...\n");
114 CHECK(send(sock, REQUEST, SSTRLEN(REQUEST), 0));
115
116 printf("Response:\n\n");
117
118 while (1) {
119 int len = recv(sock, response, sizeof(response) - 1, 0);
120
121 if (len < 0) {
122 printf("Error reading response\n");
123 return 0;
124 }
125
126 if (len == 0) {
127 break;
128 }
129
130 response[len] = 0;
131 printf("%s", response);
132 }
133
134 printf("\nClose socket\n");
135
136 (void)close(sock);
137 return 0;
138 }
139