-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplug.cpp
113 lines (101 loc) · 2.89 KB
/
splug.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
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
class splug
{
int listening;
sockaddr_in hint,client;
socklen_t clientSize;
char host[NI_MAXHOST];
char svc[NI_MAXSERV];
int clientSocket;
int result;
int buf; // *! Changed char buf to int
char passbuf[8];
int bytesRecv;
string getString; //Test var.
public:
int dec=1;
splug()
{
listening=socket(AF_INET,SOCK_STREAM,0);
if(listening==-1)
cerr<<"\033[1;31m Can't create socket.\033[0m ";
}
int bind_socket()
{
hint.sin_family=AF_INET;
hint.sin_port=htons(54000);
inet_pton(AF_INET,"0.0.0.0",&hint.sin_addr);
if(bind(listening,(sockaddr*)&hint,sizeof(hint))==-1)
{
cerr<<"\033[1;31m Can't bind to IP/PORT\033[0m";
return -1;
}
}
int listen_in()
{
if(listen(listening,SOMAXCONN)==-1)
{
cerr<<"\033[1;31m Can't listen!\033[0m";
return -1;
}
}
int accept_call()
{
clientSize=sizeof(client);
clientSocket=accept(listening,(sockaddr*)&client,&clientSize);
if(clientSocket==-1)
{
cerr<<"\033[1;31m Problem with client connecting!\033[0m";
return -1;
}
}
void close_listening()
{
close(listening);
memset(host,0,NI_MAXHOST); //Cleanup of host
memset(svc,0,NI_MAXSERV);
}
void connect_message()
{
result=getnameinfo((sockaddr*)&client,clientSize,host,NI_MAXHOST,svc,NI_MAXSERV,0);
if(result) //result==0
{
cout<<"\033[1;36m"<<host<<"\033[0m connected on "<<"\033[1;33m"<<svc<<"\033[0m"<<endl;
}
else
{
inet_ntop(AF_INET,&client.sin_addr,host,NI_MAXHOST);
cout<<"\033[1;36m"<<host<<"\033[0m connected on "<<"\033[1;33m"<<ntohs(client.sin_port)<<"\033[0m"<<endl;
}
}
int id_to_client();
int pass_to_client();
int decider()
{
int buffer;
buffer=0;
bytesRecv=recv(clientSocket,(int*)&buffer,sizeof(buffer),0);
if(bytesRecv==-1)
{
cerr<<"\033[1;31mThere was a connection issue\033[0m"<<endl;
// break;
}
else if(bytesRecv==0)
{
cout<<"\033[1;31mThe client disconnected\033[0m"<<endl;
// break;
}
switch(buffer)
{
case 1: dec=id_to_client();
break;
case 2: dec=pass_to_client();
break;
case 100: return -1;
default: cerr<<"\033[1;33mWaithing for message...\033[0m"<<endl;
}
}
~splug()
{
close(clientSocket);
}
};