-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreverse-listener.c
More file actions
171 lines (129 loc) · 3.2 KB
/
reverse-listener.c
File metadata and controls
171 lines (129 loc) · 3.2 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
168
169
170
171
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include "common.h"
#include "plugin.h"
static void display_tip(void)
{
fprintf(stderr, "Type 'quit' to close the connection\n");
}
static int command(const struct plugin * const plugin, int fd,
struct sockaddr *destaddr, socklen_t addrlen)
{
ssize_t len;
int llen;
char buf[CMD_BUFFER_LEN];
char *cmd;
display_tip();
while ( 1 )
{
fputs(">> ", stdout);
if ( fgets(buf, sizeof(buf), stdin) == NULL )
{
fprintf(stderr, "fgets: error occured\n");
return -1;
}
len = strnlen(buf, sizeof(buf));
cmd = trim(buf, &len);
if ( ! len )
continue;
if ( strcmp(cmd, "quit") == 0 )
return 0;
if ( plugin->send(fd, cmd, len, MSG_DONTWAIT, destaddr, addrlen) != len )
{
fprintf(stderr, "write: unable to send\n");
return 0;
}
llen = sizeof(buf);
while ( llen == sizeof(buf) &&
(llen = plugin->recv(fd, buf, sizeof(buf), 0, destaddr, &addrlen)) )
{
if ( llen < 0 )
{
if ( errno == EAGAIN || errno == EWOULDBLOCK )
break;
fprintf(stderr, "read: unable to recv\n");
return 0;
}
printf("%.*s", llen, buf);
}
}
return 0;
}
static int check_peer(const struct rshell *rshell, const struct sockaddr *peer, socklen_t len)
{
char host[NI_MAXHOST],
service[NI_MAXSERV];
if ( rshell->peer == NULL )
return 1;
if ( getnameinfo(peer, len, host, sizeof(host), service, sizeof(service), NI_NUMERICHOST) != 0 )
{
perror("getnameinfo");
return 0;
}
if ( strcasecmp(host, rshell->peer) == 0 )
return 1;
fprintf(stderr, "Peer %s mismatched (not %s).\n", host, rshell->peer);
return 0;
}
static const char *get_ip_str(const struct sockaddr *saddr)
{
static char buff[sizeof("[fe80:1212:290:1aff:fea3:1f5c]:65535")];
char ip[INET6_ADDRSTRLEN];
const char *ptr;
socklen_t len;
const char *fmt;
switch (saddr->sa_family)
{
case AF_INET:
len = sizeof(struct sockaddr_in);
fmt = "%s:%d";
break;
case AF_INET6:
len = sizeof(struct sockaddr_in6);
fmt = "[%s]:%d";
break;
default:
return "";
}
if ( (ptr = inet_ntop(saddr->sa_family, (const void*) saddr->sa_data + 2, ip, len)) == NULL )
return "";
memset(buff, 0, sizeof(buff));
snprintf(buff, sizeof(buff), fmt, ptr, ntohs(((const struct sockaddr_in *)saddr)->sin_port));
return buff;
}
int rshell_serve(const struct rshell *rshell)
{
int sockfd,
client;
struct addrinfo hints = {0};
struct sockaddr_storage storage;
struct sockaddr *peeraddr = (struct sockaddr *) &storage;
socklen_t len = sizeof(struct sockaddr_storage);
hints.ai_family = rshell->family;
if ( (sockfd = rshell->plugin->listen(&hints, rshell->host, rshell->service)) < 0 )
return -1;;
fprintf(stderr, "Waiting for connection...\n");
while ( 1 )
{
if ( (client = rshell->plugin->accept(sockfd, peeraddr, &len)) < 0 )
{
perror("accept");
return errno;
}
if ( check_peer(rshell, peeraddr, len) )
break;
}
fprintf(stderr, "\nNew connection from %s!\n", get_ip_str(peeraddr));
while ( command(rshell->plugin, client, peeraddr, len) )
;
close(client);
close(sockfd);
return 0;
}