-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.cpp
More file actions
168 lines (139 loc) · 3.09 KB
/
Client.cpp
File metadata and controls
168 lines (139 loc) · 3.09 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Client.hpp"
#include "Channel.hpp"
Client::Client()
{
}
Client::Client(int socket, sockaddr_in client_addr)
{
this->socket = socket;
this->addr = client_addr;
this->nickname = "";
this->username = "";
this->realname = "";
this->hostname = get_client_ip(socket);
this->servername = "";
this->buffer = "";
this->pass_state = NOPASS;
this->nick_state = NONICK;
this->user_state = NOUSER;
this->grade = NOT_AUTHENTICATED;
}
Client::~Client()
{
}
std::string Client::get_nickname() const
{
return this->nickname;
}
void Client::set_nickname(std::string nickname)
{
this->nickname = nickname;
}
std::string Client::get_username() const
{
return this->username;
}
void Client::set_username(std::string username)
{
this->username = username;
}
std::string Client::get_realname() const
{
return this->realname;
}
void Client::set_realname(std::string realname)
{
this->realname = realname;
}
int Client::get_pass_state() const
{
return this->pass_state;
}
void Client::set_pass_state(int state)
{
this->pass_state = state;
}
int Client::get_socket() const
{
return this->socket;
}
int Client::get_nick_state() const
{
return this->nick_state;
}
void Client::set_nick_state(int state)
{
this->nick_state = state;
}
int Client::get_user_state() const
{
return this->user_state;
}
void Client::set_user_state(int state)
{
this->user_state = state;
}
int Client::get_grade() const
{
return this->grade;
}
void Client::set_grade(int grade)
{
this->grade = grade;
}
void Client::set_hostname(std::string hostname)
{
this->hostname = hostname;
}
std::string Client::get_servername() const
{
return this->servername;
}
void Client::set_servername(std::string servername)
{
this->servername = servername;
}
std::string Client::get_hostname() const
{
return this->hostname;
}
void Client::add_to_buffer(std::string message)
{
this->buffer += message;
}
std::string Client::get_buffer() const
{
return this->buffer;
}
void Client::clear_buffer()
{
this->buffer = "";
}
std::string Client::get_client_ip(int socket_fd) const
{
struct sockaddr_in addr;
socklen_t addr_size = sizeof(struct sockaddr_in);
int res = getpeername(socket_fd, (struct sockaddr*)&addr, &addr_size);
char clientip[INET_ADDRSTRLEN];
if (res != 0) {
perror("getpeername");
} else {
inet_ntop(AF_INET, &(addr.sin_addr), clientip, sizeof(clientip));
// If the IP address is a loopback address, retrieve the public IP
if (clientip == std::string("127.0.0.1"))
{
char hostbuffer[256];
char *IPbuffer;
struct hostent *host_entry;
int hostname;
// Fetch the hostname
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
// Fetch the IP address
if ((host_entry = gethostbyname(hostbuffer))) {
IPbuffer = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
strcpy(clientip, IPbuffer);
}
}
}
return clientip;
}