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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/* portecho.c
*
* by Gnther Montag dl4mge
* test program for telnet port, it uses "toupper"
* and just echoes the input in capitals.
* derived from tfwd.c, which is taken from the
* very interesting select_tut man page (see man select_tut).
*
* This is part of hf package, GPL. Have fun!
*/
/* ----------------------------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <ctype.h>
#undef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#define SHUT_FD1 { \
if (fd1 >= 0) { \
shutdown (fd1, SHUT_RDWR); \
close (fd1); \
fd1 = -1; \
} \
}
#define BUF_SIZE 1024
int quit = 0;
char* greeting = "Connected to portecho!\nQuit me by #.\n";
char* byebye = "Bye ! \n";
/* ----------------------------------------------------------------- */
static int listen_socket (int listen_port) {
struct sockaddr_in a;
int s;
int yes;
if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
return -1;
}
yes = 1;
if (setsockopt
(s, SOL_SOCKET, SO_REUSEADDR,
(char *) &yes, sizeof (yes)) < 0) {
perror ("setsockopt");
close (s);
return -1;
}
memset (&a, 0, sizeof (a));
a.sin_port = htons (listen_port);
a.sin_family = AF_INET;
if (bind
(s, (struct sockaddr *) &a, sizeof (a)) < 0) {
perror ("bind");
close (s);
return -1;
}
printf ("accepting connections on port %d\n",
(int) listen_port);
listen (s, 10);
return s;
}
/* ----------------------------------------------------------------- */
int main (int argc, char **argv) {
int h, i, fd1 = -1;
char buf1[BUF_SIZE], buf2[BUF_SIZE];
// buf1 what port reads from extern prog = tx
// buf2 what port sends to extern prog = rx
int buf1_avail = 0, buf1_written = 0;
int buf2_avail = 0, buf2_written = 0;
// vars of my test prog
char zeichen;
char quitsignal = '#';
if (argc != 2) {
fprintf (stderr,
"\n\t* * * dl4mge 's PORTECHO. Little TCP test program * * *\n"
"\t* * * for tests with hf's mailbox function. * * *\n"
"\n"
"\tUsage: portecho <port>, e.g. 'portecho 3333'.\n"
"\tQuit me with '#'. \n\n");
// <forward-to-port> <forward-to-ip-address>\n");
exit (1);
}
fprintf (stdout,
"\n\t* * * dl4mge 's PORTECHO. Little TCP test program * * *\n"
"\t* * * for tests with hf's mailbox function. * * *\n"
"\n"
"\tTest me on another console by 'telnet localhost <port>'.\n"
"\tQuit me with '#'. \n\n");
signal (SIGPIPE, SIG_IGN);
h = listen_socket (atoi (argv[1]));
if (h < 0) {
fprintf (stderr, "error on opening port\n");
exit (1);
}
for (;;) {
for (;;) {
int r, n = 0;
fd_set rd, wr, er;
FD_ZERO (&rd);
FD_ZERO (&wr);
FD_ZERO (&er);
FD_SET (h, &rd);
n = max (n, h);
if (fd1 > 0 && buf1_avail < BUF_SIZE) {
fprintf (stderr, "testport ready for read\n");
FD_SET (fd1, &rd);
n = max (n, fd1);
}
if (fd1 > 0
&& buf2_avail - buf2_written > 0) {
fprintf (stderr, "testport ready for write\n");
FD_SET (fd1, &wr);
n = max (n, fd1);
}
if (fd1 > 0) {
fprintf (stderr, "testport ready for err\n");
FD_SET (fd1, &er);
n = max (n, fd1);
}
r = select (n + 1, &rd, &wr, &er, NULL);
if (r == -1 && errno == EINTR)
continue;
if (r < 0) {
perror ("select()");
exit (1);
}
if (FD_ISSET (h, &rd)) {
unsigned int l;
struct sockaddr_in client_address;
fprintf (stderr, "something tries to access port.\n");
memset (&client_address, 0, l =sizeof (client_address));
r = accept (h, (struct sockaddr *) &client_address, &l);
fprintf (stderr, "I will try to accept peer port\n");
if (r < 0) {
perror ("accept() peer port");
} else {
fprintf (stderr, "I accepted peer port\n");
fprintf (stderr, "r = %d\n",r);
SHUT_FD1;
buf1_avail = buf1_written = 0;
buf2_avail = buf2_written = 0;
fd1 = r;
sprintf(buf2, "%s", greeting);
buf2_avail = strlen(greeting);
}
}
// /* NB: read oob data before normal reads
if (fd1 > 0)
if (FD_ISSET (fd1, &er)) {
char c;
errno = 0;
r = recv (fd1, &c, 1, MSG_OOB);
if (r < 1) {
SHUT_FD1;
} else
fprintf (stderr, "error: %s, errno %d\n", &c, errno);
}
// READ
if (fd1 > 0) {
if (FD_ISSET (fd1, &rd)) {
fprintf (stderr, "try read\n");
r = read (fd1, buf1 + buf1_avail, BUF_SIZE - buf1_avail);
if (r < 1) {
SHUT_FD1;
} else {
buf1_avail += r;
fprintf (stderr, "read %d chars\n", r);
fprintf (stderr, "buf1: %s\n", buf1);
}
// PROCESS
for (i = 0; i < buf1_avail && i < (BUF_SIZE - buf2_avail); i++) {
zeichen = buf1[buf1_written++];
if (zeichen != quitsignal) {
buf2[buf2_avail++] = toupper(zeichen);
}
if (zeichen == quitsignal) {
fprintf (stderr,
"read: Signal to quit: %c !\n", zeichen);
quit = 1;
sprintf(buf2, "%s", byebye);
buf2_avail = strlen(byebye);
//exit(0);
}
}
fprintf (stderr, "buf2: %s\n", buf2);
}
}
// WRITE
if (fd1 > 0) {
if (FD_ISSET (fd1, &wr)) {
fprintf (stderr, "write\n");
r = write (fd1, buf2 + buf2_written,
buf2_avail - buf2_written);
if (r < 1) {
SHUT_FD1;
} else {
buf2_written += r;
}
}
}
// /* check if write data has caught read data
if (buf1_written == buf1_avail) {
buf1_written = buf1_avail = 0;
memset(buf1, 0, sizeof(buf1));
}
if (buf2_written == buf2_avail) {
buf2_written = buf2_avail = 0;
memset(buf2, 0, sizeof(buf2));
if (quit) {
SHUT_FD1
quit = 0;
break;
}
}
}
}
return 0;
}
|