CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/unixasm/fndsockserver.c
Views: 11766
1
/*
2
* fndsockserver.c
3
* Copyright 2006 Ramon de Carvalho Valle <[email protected]>
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
*
19
*/
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <sys/types.h>
25
#include <sys/socket.h>
26
#include <arpa/inet.h>
27
#include <netdb.h>
28
#include <sys/select.h>
29
#include <unistd.h>
30
#include <errno.h>
31
32
#define BACKLOG 5
33
34
int
35
hexdump(char *buf, int len)
36
{
37
int i, j;
38
39
for (i=0; i<len; i++) {
40
for (j=0; j<16; j++) {
41
if (i+j >= len)
42
printf("%3s","");
43
else
44
printf("%02x ", (unsigned char)buf[i+j]);
45
}
46
47
printf("%3s","");
48
49
for (j=0; j<16; j++) {
50
if (i+j >= len)
51
printf("%1s","");
52
else
53
if (buf[i+j]>'\x1f' && buf[i+j]<'\x7f')
54
printf("%c", buf[i+j]);
55
else
56
printf(".");
57
}
58
59
i += 15;
60
61
printf("\n");
62
}
63
64
return 0;
65
}
66
67
int
68
main(int argc, char **argv)
69
{
70
char *addr = "0.0.0.0";
71
int port = 1234;
72
int c, s;
73
int debug = 0, verbose = 0;
74
struct sockaddr_in sin;
75
struct hostent *he;
76
77
while ((c = getopt(argc, argv, "a:dp:v")) != -1) {
78
switch (c) {
79
case 'a':
80
addr = optarg;
81
break;
82
case 'd':
83
debug = 1;
84
break;
85
case 'p':
86
port = atoi(optarg);
87
break;
88
case 'v':
89
verbose = 1;
90
}
91
}
92
93
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
94
perror("socket");
95
exit(EXIT_FAILURE);
96
}
97
98
memset(&sin, 0, sizeof(sin));
99
sin.sin_family = AF_INET;
100
sin.sin_port = htons(port);
101
if ((sin.sin_addr.s_addr = inet_addr(addr)) == -1) {
102
if ((he = gethostbyname(addr)) == NULL) {
103
errno = EADDRNOTAVAIL;
104
perror("gethostbyname");
105
exit(EXIT_FAILURE);
106
}
107
memcpy(&sin.sin_addr.s_addr, he->h_addr, 4);
108
}
109
110
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
111
perror("bind");
112
exit(EXIT_FAILURE);
113
}
114
115
if (listen(s, BACKLOG) == -1) {
116
perror("listen");
117
exit(EXIT_FAILURE);
118
}
119
120
if (debug || verbose)
121
printf("listening on %s:%d\n", addr, port);
122
123
while (1) {
124
int tmp;
125
struct sockaddr_in sin;
126
socklen_t sin_len = sizeof(sin);
127
128
if((tmp = accept(s, (struct sockaddr *)&sin, &sin_len)) == -1) {
129
perror("accept");
130
exit(EXIT_FAILURE);
131
}
132
133
if (debug || verbose)
134
printf("accepted connection from %s:%d\n", inet_ntoa(sin.sin_addr),
135
ntohs(sin.sin_port));
136
137
if (!fork()) {
138
int count;
139
char buf[1024];
140
141
count = recv(tmp, buf, sizeof(buf), 0);
142
143
if (debug)
144
hexdump(buf, count);
145
146
if (debug || verbose)
147
printf("%d bytes received\n", count);
148
149
sleep(2);
150
151
#if defined(_AIX) || (defined(__linux__) && defined(__powerpc64__))
152
{
153
/* fake function descriptor */
154
unsigned long fdesc[2] = {(unsigned long)buf, 0};
155
(*(void (*)())fdesc)();
156
}
157
#else
158
(*(void (*)())buf)();
159
#endif
160
161
exit(EXIT_SUCCESS);
162
}
163
}
164
165
exit(EXIT_SUCCESS);
166
}
167
168
169