-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.cpp
77 lines (59 loc) · 1.64 KB
/
connection.cpp
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
/*
* Copyright (C) tanserver.org
* Copyright (C) Daniele Affinita
* Copyright (C) Chen Daye
*/
#include "connection.h"
/*
struct sockaddr_in {
sa_family_t sin_family; // address family: AF_INET
in_port_t sin_port; // port in network byte order
struct in_addr sin_addr; // internet address
};
struct in_addr {
uint32_t s_addr; // address in network byte order
};
*/
int _connect(const char* host, in_port_t port)
{
struct sockaddr_in addr;
int s,
connRes;
//returns file descriptor of socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) handleError("unable to create socket\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(host);
connRes = connect(s, (struct sockaddr *) &addr, sizeof(addr));
if(connRes == -1)
{
close(s);
char buff[48];
sprintf(buff, "System Call connect() failed with errno code %d: %s \n", errno, strerror(errno));
handleError(buff);
}
return s;
}
static bool check_crlf(std::string s)
{
size_t dimension = s.size();
std::string sb;
if(dimension < 2)
return false;
sb = s.substr(dimension - 2, 2);
return sb == "\r\n";
}
std::string getAnswer(SSL* ssl)
{
std::string out;
char buffer[2];
memset(buffer, 0, 2);
while( !check_crlf(out) ){
SSL_read(ssl, buffer, 1);
out.append(buffer);
}
if(out == "")
handleError("Empty response from the server\n");
return out;
}