]> pd.if.org Git - zpackage/blob - crypto/https.c
remove stray debug fprintf
[zpackage] / crypto / https.c
1 #define _POSIX_C_SOURCE 200112L
2 #include <sys/socket.h>
3 #include <sys/types.h>
4 #include <netdb.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 int open_tcp_connection(char *host, int port) {
10         int sock, rv;
11         struct addrinfo hint = { 0 };
12         struct addrinfo *addr;
13
14         hint.ai_family = AF_INET;
15         hint.ai_socktype = SOCK_STREAM;
16
17         rv = getaddrinfo(host, NULL, &hint, &addr); 
18         if (rv != 0) {
19                 perror(gai_strerror(rv));
20                 return -1;
21         }
22
23         ((struct sockaddr_in *)addr->ai_addr)->sin_port = htons(port);
24
25         sock = socket(PF_INET, SOCK_STREAM, 0);
26         if (sock != -1) {
27                 if (connect(sock, addr->ai_addr, addr->ai_addrlen) == -1) {
28                         perror("can't connect");
29                         close(sock);
30                         sock = -1;
31                 }
32         }
33         freeaddrinfo(addr);
34         return sock;
35 }
36
37 #if 0
38 int main(int ac, char **av) {
39         int socket;
40         char *req = "GET / HTTP/1.1\r\nHost: granicus.if.org\r\nConnection: close\r\n\r\n";
41         ssize_t bytes;
42         char buffer[1024];
43
44         socket = open_tcp_connection(av[1], 80);
45         write(socket, req, strlen(req));
46         while ((bytes = read(socket, buffer, sizeof buffer)) > 0) {
47                 write(1, buffer, bytes);
48         }
49
50         close(socket);
51         return 0;
52         
53 }
54 #endif