diff options
| author | SkyperTHC | 2026-03-03 06:28:55 +0000 |
|---|---|---|
| committer | SkyperTHC | 2026-03-03 06:28:55 +0000 |
| commit | 5d3573ef7a109ee70416fe94db098fe6a769a798 (patch) | |
| tree | dc2d5b294c9db8ab2db7433511f94e1c4bb8b698 /other/fizzbounce | |
| parent | c6c59dc73cc4586357f93ab38ecf459e98675cc5 (diff) | |
packetstorm sync
Diffstat (limited to 'other/fizzbounce')
| -rw-r--r-- | other/fizzbounce/Makefile | 15 | ||||
| -rw-r--r-- | other/fizzbounce/client.c | 106 | ||||
| -rw-r--r-- | other/fizzbounce/client.h | 47 | ||||
| -rw-r--r-- | other/fizzbounce/main.c | 125 | ||||
| -rw-r--r-- | other/fizzbounce/main.h | 14 | ||||
| -rw-r--r-- | other/fizzbounce/network.c | 691 | ||||
| -rw-r--r-- | other/fizzbounce/network.h | 205 | ||||
| -rw-r--r-- | other/fizzbounce/relay.c | 166 | ||||
| -rw-r--r-- | other/fizzbounce/relay.h | 12 |
9 files changed, 1381 insertions, 0 deletions
diff --git a/other/fizzbounce/Makefile b/other/fizzbounce/Makefile new file mode 100644 index 0000000..98544a4 --- /dev/null +++ b/other/fizzbounce/Makefile | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | |||
| 2 | CFILES=client.c main.c network.c relay.c | ||
| 3 | LIBS=-lpthread | ||
| 4 | CC=gcc | ||
| 5 | CFLAGS=-g -DDEBUG | ||
| 6 | PREFIX=/usr/local | ||
| 7 | |||
| 8 | all: fizzbounce | ||
| 9 | |||
| 10 | clean: rm -f *.o fizzbounce | ||
| 11 | |||
| 12 | fizzbounce: | ||
| 13 | ${CC} -o fizzbounce ${CFILES} ${CFLAGS} ${LIBS} | ||
| 14 | |||
| 15 | |||
diff --git a/other/fizzbounce/client.c b/other/fizzbounce/client.c new file mode 100644 index 0000000..f017525 --- /dev/null +++ b/other/fizzbounce/client.c | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | |||
| 2 | /* bounce 4 all | ||
| 3 | * 1999 (c) scut | ||
| 4 | * | ||
| 5 | * client routines | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <sys/socket.h> | ||
| 9 | #include <netinet/in.h> | ||
| 10 | #include <arpa/inet.h> | ||
| 11 | #include <pthread.h> | ||
| 12 | #include <signal.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <string.h> | ||
| 16 | #include "client.h" | ||
| 17 | #include "network.h" | ||
| 18 | #include "relay.h" | ||
| 19 | |||
| 20 | extern int vuln; | ||
| 21 | |||
| 22 | void * | ||
| 23 | cl_handle (client *cl) | ||
| 24 | { | ||
| 25 | int n; | ||
| 26 | char buff[1024]; | ||
| 27 | |||
| 28 | pthread_mutex_lock (&cl->cl_mutex); | ||
| 29 | printf ("new client from %s port %d\n", inet_ntoa (cl->csa.sin_addr), cl->csa.sin_port); | ||
| 30 | pthread_mutex_unlock (&cl->cl_mutex); | ||
| 31 | |||
| 32 | /* now, since we have a client that want's to get relayed, and passed all checks, | ||
| 33 | * establish a connection to the remote server by choosing a bounceip / siteip | ||
| 34 | */ | ||
| 35 | |||
| 36 | printf ("control connection to %s:%d\n", cl->connip, cl->connport); | ||
| 37 | |||
| 38 | pthread_mutex_lock (&cl->cl_mutex); | ||
| 39 | cl->ss = net_connect (&cl->css, cl->connip, cl->connport, 45); | ||
| 40 | if (cl->ss == -1) { | ||
| 41 | printf ("failed to relay client from %s:%d to %s:%d\n", | ||
| 42 | inet_ntoa (cl->csa.sin_addr), cl->csa.sin_port, cl->connip, cl->connport); | ||
| 43 | pthread_mutex_unlock (&cl->cl_mutex); | ||
| 44 | return (NULL); | ||
| 45 | } else { | ||
| 46 | printf ("successfully relayed client from %s:%d to %s:%d\n", | ||
| 47 | inet_ntoa (cl->csa.sin_addr), cl->csa.sin_port, cl->connip, cl->connport); | ||
| 48 | } | ||
| 49 | pthread_mutex_unlock (&cl->cl_mutex); | ||
| 50 | |||
| 51 | /* now since we have both, a connection from the client to us, | ||
| 52 | * and a connection from us to the real server we call the main relay handler | ||
| 53 | */ | ||
| 54 | |||
| 55 | if (vuln == 1) { | ||
| 56 | net_write (cl->ss, "CONNECT %s:%s HTTP/1.0\n\n", cl->ircip, cl->ircport); | ||
| 57 | } else if (vuln == 2) { | ||
| 58 | net_write (cl->ss, "POST http://%s:%s/ HTTP/1.0\n\n", cl->ircip, cl->ircport); | ||
| 59 | } | ||
| 60 | // memset (buff, '\0', sizeof (buff)); | ||
| 61 | // n = net_rlinet (cl->ss, buff, sizeof (buff), 45); | ||
| 62 | |||
| 63 | // if (n <= 0) | ||
| 64 | // goto clerror; | ||
| 65 | |||
| 66 | // printf ("READ: %s\n", buff); | ||
| 67 | // if (strncmp (buff, "HTTP/1.0 200", 12) != 0) | ||
| 68 | // goto clerror; | ||
| 69 | |||
| 70 | sleep (5); | ||
| 71 | memset (buff, '\0', sizeof (buff)); | ||
| 72 | rly_client (cl); | ||
| 73 | |||
| 74 | clerror: | ||
| 75 | close (cl->ss); | ||
| 76 | close (cl->cs); | ||
| 77 | |||
| 78 | /* the relay handler only exits on failure or connection close request, | ||
| 79 | * either from the remote server or our little client | ||
| 80 | * in any case, we have to terminate the client | ||
| 81 | */ | ||
| 82 | |||
| 83 | /* should never happen */ | ||
| 84 | return (NULL); | ||
| 85 | } | ||
| 86 | |||
| 87 | client * | ||
| 88 | cl_add (void) | ||
| 89 | { | ||
| 90 | int n; | ||
| 91 | client *cl; | ||
| 92 | |||
| 93 | cl = (client *) calloc (1, sizeof (client)); | ||
| 94 | if (cl) | ||
| 95 | cl_init (cl); | ||
| 96 | |||
| 97 | return (cl); | ||
| 98 | } | ||
| 99 | |||
| 100 | void | ||
| 101 | cl_init (client *cl) | ||
| 102 | { | ||
| 103 | pthread_mutex_init (&cl->cl_mutex, NULL); | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
diff --git a/other/fizzbounce/client.h b/other/fizzbounce/client.h new file mode 100644 index 0000000..23f8b35 --- /dev/null +++ b/other/fizzbounce/client.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | |||
| 2 | #include <netinet/in.h> | ||
| 3 | #include <pthread.h> | ||
| 4 | |||
| 5 | #ifndef FIZZ_CLIENT_H | ||
| 6 | #define FIZZ_CLIENT_H | ||
| 7 | |||
| 8 | typedef struct client { | ||
| 9 | pthread_t tid; /* thread id */ | ||
| 10 | pthread_mutex_t cl_mutex; /* client mutex */ | ||
| 11 | int cs; /* client socket */ | ||
| 12 | struct sockaddr_in csa; | ||
| 13 | |||
| 14 | char *connip; | ||
| 15 | unsigned short connport; | ||
| 16 | char *ircip, *ircport; | ||
| 17 | int ss; /* control connection socket to server */ | ||
| 18 | struct sockaddr_in css; /* server socket address */ | ||
| 19 | } client; | ||
| 20 | |||
| 21 | /* cl_handle | ||
| 22 | * | ||
| 23 | * thread that handles one client. once a new client connects this thread | ||
| 24 | * is started and handles anything the client wants. | ||
| 25 | * client *cl is a new client structure, which has to be initialized already | ||
| 26 | * | ||
| 27 | * returns nothing | ||
| 28 | */ | ||
| 29 | |||
| 30 | void *cl_handle (client *cl); | ||
| 31 | |||
| 32 | /* cl_add | ||
| 33 | * | ||
| 34 | * adds a new client and returns | ||
| 35 | * NULL on failure | ||
| 36 | * client * to new client if succes | ||
| 37 | */ | ||
| 38 | client *cl_add (void); | ||
| 39 | |||
| 40 | /* cl_init | ||
| 41 | * | ||
| 42 | * initializes a fresh client structure =) | ||
| 43 | */ | ||
| 44 | void cl_init (client *cl); | ||
| 45 | |||
| 46 | #endif | ||
| 47 | |||
diff --git a/other/fizzbounce/main.c b/other/fizzbounce/main.c new file mode 100644 index 0000000..ffad0b8 --- /dev/null +++ b/other/fizzbounce/main.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | |||
| 2 | /* bounce 4 all | ||
| 3 | * 1999 (c) scut | ||
| 4 | * | ||
| 5 | * main routines | ||
| 6 | */ | ||
| 7 | |||
| 8 | #define B4A_MAIN_C | ||
| 9 | |||
| 10 | #include <sys/socket.h> | ||
| 11 | #include <netinet/in.h> | ||
| 12 | #include <arpa/inet.h> | ||
| 13 | #include <errno.h> | ||
| 14 | #include <pthread.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | #include <string.h> | ||
| 18 | #include <unistd.h> | ||
| 19 | #include "relay.h" | ||
| 20 | #include "client.h" | ||
| 21 | #include "main.h" | ||
| 22 | #include "network.h" | ||
| 23 | |||
| 24 | bound *b; | ||
| 25 | int vuln = 0; /* 1 = connect, 2 = post */ | ||
| 26 | |||
| 27 | int | ||
| 28 | main (int argc, char **argv) | ||
| 29 | { | ||
| 30 | int n; | ||
| 31 | |||
| 32 | banner (); | ||
| 33 | |||
| 34 | if (argc != 8) { | ||
| 35 | usage (); | ||
| 36 | } | ||
| 37 | |||
| 38 | /* if (fork () != 0) | ||
| 39 | exit (0); | ||
| 40 | */ | ||
| 41 | if (argv[1][0] == 'c') | ||
| 42 | vuln = 1; | ||
| 43 | if (argv[1][0] == 'p') | ||
| 44 | vuln = 2; | ||
| 45 | if (vuln == 0) | ||
| 46 | usage (); | ||
| 47 | |||
| 48 | |||
| 49 | b = net_bind (argv[2], atoi (argv[3])); | ||
| 50 | if (b == NULL) { | ||
| 51 | printf ("cannot bind to %s:%u\n", argv[2], atoi (argv[3])); | ||
| 52 | quit (); | ||
| 53 | } | ||
| 54 | printf ("bound to %s, port %u\n", argv[2], atoi (argv[3])); | ||
| 55 | |||
| 56 | srv_main (argv[4], argv[5], argv[6], argv[7]); | ||
| 57 | |||
| 58 | /* should never happen */ | ||
| 59 | return (0); | ||
| 60 | } | ||
| 61 | |||
| 62 | void | ||
| 63 | srv_main (char *connip, char *connport, char *ircip, char *ircport) | ||
| 64 | { | ||
| 65 | struct sockaddr_in csa; | ||
| 66 | client *cnew; | ||
| 67 | int cs, n; | ||
| 68 | |||
| 69 | while (1) { | ||
| 70 | cs = net_accept (b->bs, &csa, 20); | ||
| 71 | if (cs == -1) { | ||
| 72 | quit(); | ||
| 73 | } | ||
| 74 | |||
| 75 | /* if we actually experience a new connection start a new client thread */ | ||
| 76 | if (cs) { | ||
| 77 | cnew = cl_add (); | ||
| 78 | if (cnew == NULL) { | ||
| 79 | printf ("cannot add new client\n"); | ||
| 80 | } else { | ||
| 81 | pthread_mutex_lock (&cnew->cl_mutex); | ||
| 82 | cnew->cs = cs; | ||
| 83 | cnew->connip = strdup (connip); | ||
| 84 | cnew->connport = atoi (connport); | ||
| 85 | cnew->ircip = strdup (ircip); | ||
| 86 | cnew->ircport = strdup (ircport); | ||
| 87 | memcpy (&cnew->csa, &csa, sizeof (struct sockaddr_in)); | ||
| 88 | pthread_mutex_unlock (&cnew->cl_mutex); | ||
| 89 | |||
| 90 | n = pthread_create (&cnew->tid, NULL, (void *) cl_handle, (void *) cnew); | ||
| 91 | if (n == -1) { | ||
| 92 | printf ("cannot create new client thread: %s\n", strerror (errno)); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | quit (); | ||
| 99 | } | ||
| 100 | |||
| 101 | void | ||
| 102 | usage (void) | ||
| 103 | { | ||
| 104 | printf ("usage: fizzbnc <c|p> <local-ip | *> <local-port> <squid-proxy>\n" | ||
| 105 | " <proxy-port> <irc-host> <irc-port>\n"); | ||
| 106 | exit (EXIT_FAILURE); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | void | ||
| 111 | banner (void) | ||
| 112 | { | ||
| 113 | printf ("fizzbounce "VERSION" by "AUTHORS"\n"); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | void | ||
| 118 | quit (void) | ||
| 119 | { | ||
| 120 | printf ("shutting fizzbounce down.\n"); | ||
| 121 | |||
| 122 | /* terminate main thread */ | ||
| 123 | exit (0); | ||
| 124 | } | ||
| 125 | |||
diff --git a/other/fizzbounce/main.h b/other/fizzbounce/main.h new file mode 100644 index 0000000..de6875f --- /dev/null +++ b/other/fizzbounce/main.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | |||
| 2 | #ifndef FIZZ_MAIN_H | ||
| 3 | #define FIZZ_MAIN_H | ||
| 4 | |||
| 5 | #define VERSION "v0.2-b990709-double-spin-kick-edition-:-)" | ||
| 6 | #define AUTHORS "team teso" | ||
| 7 | |||
| 8 | void srv_main (char *connip, char *connport, char *ircip, char *ircport); | ||
| 9 | void usage (void); | ||
| 10 | void banner (void); | ||
| 11 | void quit (void); | ||
| 12 | |||
| 13 | #endif | ||
| 14 | |||
diff --git a/other/fizzbounce/network.c b/other/fizzbounce/network.c new file mode 100644 index 0000000..5e73e5c --- /dev/null +++ b/other/fizzbounce/network.c | |||
| @@ -0,0 +1,691 @@ | |||
| 1 | |||
| 2 | /* scut's leet network library ;) | ||
| 3 | * 1999 (c) scut | ||
| 4 | * | ||
| 5 | * networking routines | ||
| 6 | * based on my hbot networking sources, | ||
| 7 | * revised, extended and adapted 990405 | ||
| 8 | * extended, improved and fixed 990430 | ||
| 9 | * | ||
| 10 | * nearly all of this code wouldn't have been possible without w. richard stevens | ||
| 11 | * excellent network coding book. if you are interested in network coding, | ||
| 12 | * there is no way around it. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <sys/ioctl.h> | ||
| 16 | #include <sys/socket.h> | ||
| 17 | #include <sys/time.h> | ||
| 18 | #include <sys/types.h> | ||
| 19 | #include <arpa/inet.h> | ||
| 20 | #include <netdb.h> | ||
| 21 | #include <net/if.h> | ||
| 22 | #include <netinet/in.h> | ||
| 23 | #include <errno.h> | ||
| 24 | #include <fcntl.h> | ||
| 25 | #include <stdarg.h> | ||
| 26 | #include <stdio.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | #include "network.h" | ||
| 31 | |||
| 32 | int net_readtimeout = NET_READTIMEOUT; | ||
| 33 | int net_conntimeout = NET_CONNTIMEOUT; | ||
| 34 | int net_identtimeout = NET_IDENTTIMEOUT; | ||
| 35 | |||
| 36 | int | ||
| 37 | net_socks_connect (char *socks, unsigned short int sport, char *server, unsigned short int port, int sec) | ||
| 38 | { | ||
| 39 | int s5s; | ||
| 40 | struct sockaddr_in cs; | ||
| 41 | |||
| 42 | s5s = net_connect (&cs, socks, sport, sec); | ||
| 43 | if (s5s == -1) | ||
| 44 | return (-1); | ||
| 45 | |||
| 46 | if (net_socks_put_s5info (s5s, server, port, sec) == -1) { | ||
| 47 | close (s5s); | ||
| 48 | return (-1); | ||
| 49 | } | ||
| 50 | return (s5s); | ||
| 51 | } | ||
| 52 | |||
| 53 | int | ||
| 54 | net_socks_put_s5info (int s5s, char *server, unsigned short int port, int sec) | ||
| 55 | { | ||
| 56 | int n; | ||
| 57 | char buff[1024]; | ||
| 58 | |||
| 59 | /* v5 + noauth */ | ||
| 60 | net_write (s5s, "\x05\x01%c", 0); | ||
| 61 | if (net_rtimeout (s5s, sec) == -1) | ||
| 62 | return (-1); | ||
| 63 | recv (s5s, buff, sizeof (buff), 0); | ||
| 64 | |||
| 65 | /* chain us =) */ | ||
| 66 | net_write (s5s, "\x05\x01%c\x03%c%s%c%c", 0, strlen (server), server, (port >> 8) & 0xff, port & 0xff); | ||
| 67 | if (net_rtimeout (s5s, sec) == -1) | ||
| 68 | return (-1); | ||
| 69 | n = recv (s5s, buff, sizeof (buff), 0); | ||
| 70 | if (buff[1] != 0x00) { | ||
| 71 | return (-1); | ||
| 72 | } | ||
| 73 | return (1); | ||
| 74 | } | ||
| 75 | |||
| 76 | int | ||
| 77 | net_parseip (char *inp, char **ip, unsigned short int *port) | ||
| 78 | { | ||
| 79 | int n; | ||
| 80 | |||
| 81 | if (inp == NULL) | ||
| 82 | return (0); | ||
| 83 | if (strchr (inp, ':') == NULL) | ||
| 84 | return (0); | ||
| 85 | |||
| 86 | n = sscanf (inp, "%a[^:]:%hu", ip, port); | ||
| 87 | if (n != 2) | ||
| 88 | return (0); | ||
| 89 | |||
| 90 | if (*ip == NULL || (*port < 1 || *port > 65535)) | ||
| 91 | return (0); | ||
| 92 | |||
| 93 | return (1); | ||
| 94 | } | ||
| 95 | |||
| 96 | char * | ||
| 97 | net_getlocalip (void) | ||
| 98 | { | ||
| 99 | struct sockaddr_in pf; | ||
| 100 | char name[255]; | ||
| 101 | |||
| 102 | memset (name, '\0', sizeof (name)); | ||
| 103 | |||
| 104 | if (gethostname (name, sizeof (name) - 1) == -1) { | ||
| 105 | return (NULL); | ||
| 106 | } | ||
| 107 | |||
| 108 | pf.sin_addr.s_addr = net_resolve (name); | ||
| 109 | |||
| 110 | return (strdup (inet_ntoa (pf.sin_addr)));; | ||
| 111 | } | ||
| 112 | |||
| 113 | FILE * | ||
| 114 | net_descriptify (int socket) | ||
| 115 | { | ||
| 116 | FILE *fp; | ||
| 117 | |||
| 118 | fp = fdopen (socket, "r+"); | ||
| 119 | return ((fp == NULL) ? (NULL) : (fp)); | ||
| 120 | } | ||
| 121 | |||
| 122 | /* loosely based on rfc931.c */ | ||
| 123 | |||
| 124 | int | ||
| 125 | net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport, | ||
| 126 | struct sockaddr_in *remotes, unsigned short int remoteport) | ||
| 127 | { | ||
| 128 | int is; /* ident socket */ | ||
| 129 | struct sockaddr_in isa; | ||
| 130 | int n; | ||
| 131 | char identreply[512], *cp; | ||
| 132 | unsigned int rmt_port, our_port; | ||
| 133 | |||
| 134 | |||
| 135 | *ident = NULL; | ||
| 136 | |||
| 137 | is = net_connect (&isa, inet_ntoa (remotes->sin_addr), 113, net_identtimeout); | ||
| 138 | if (is == -1) | ||
| 139 | return (-1); | ||
| 140 | |||
| 141 | /* ident request */ | ||
| 142 | net_write (is, "%u,%u\r\n", remoteport, localport); | ||
| 143 | memset (identreply, '\0', sizeof (identreply)); | ||
| 144 | |||
| 145 | n = net_rlinet (is, identreply, sizeof(identreply) -1, net_identtimeout); | ||
| 146 | if (n == -1) { | ||
| 147 | close (is); | ||
| 148 | return (-1); | ||
| 149 | } | ||
| 150 | close (is); | ||
| 151 | |||
| 152 | *ident = calloc (1, 256); | ||
| 153 | #ifdef DEBUG | ||
| 154 | printf("%s\n", identreply); | ||
| 155 | #endif | ||
| 156 | n = sscanf (identreply, "%u , %u : USERID :%*[^:]:%255s", &rmt_port, &our_port, *ident); | ||
| 157 | if (n != 3) { | ||
| 158 | free (*ident); | ||
| 159 | *ident = NULL; | ||
| 160 | return (-1); | ||
| 161 | } | ||
| 162 | |||
| 163 | /* check the ports 'man */ | ||
| 164 | if ((rmt_port != remoteport) || (our_port != localport)) { | ||
| 165 | free (*ident); | ||
| 166 | *ident = NULL; | ||
| 167 | return (-1); | ||
| 168 | } | ||
| 169 | |||
| 170 | /* strip character and save some memory */ | ||
| 171 | if ((cp = strchr (*ident, '\r'))) | ||
| 172 | *cp = '\0'; | ||
| 173 | n = strlen (*ident); | ||
| 174 | *ident = realloc (*ident, n + 1); | ||
| 175 | (*ident)[n] = '\0'; | ||
| 176 | |||
| 177 | #ifdef DEBUG | ||
| 178 | printf("ident-return: %s\n", *ident); | ||
| 179 | #endif | ||
| 180 | return (1); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | int | ||
| 185 | net_accept (int s, struct sockaddr_in *cs, int maxsec) | ||
| 186 | { | ||
| 187 | int flags, n; | ||
| 188 | fd_set ac_s; | ||
| 189 | int len; | ||
| 190 | struct timeval tval; | ||
| 191 | |||
| 192 | flags = fcntl(s, F_GETFL, 0); | ||
| 193 | if (flags == -1) | ||
| 194 | return (-1); | ||
| 195 | n = fcntl(s, F_SETFL, flags | O_NONBLOCK); | ||
| 196 | if (flags == -1) | ||
| 197 | return (-1); | ||
| 198 | |||
| 199 | FD_ZERO(&ac_s); | ||
| 200 | FD_SET(s, &ac_s); | ||
| 201 | tval.tv_sec = maxsec; | ||
| 202 | tval.tv_usec = 0; | ||
| 203 | |||
| 204 | n = select(s + 1, &ac_s, NULL, NULL, maxsec ? &tval : NULL); | ||
| 205 | if (n == 0) | ||
| 206 | return (0); | ||
| 207 | |||
| 208 | if (FD_ISSET(s, &ac_s)) { | ||
| 209 | len = sizeof(struct sockaddr_in); | ||
| 210 | n = accept(s, (struct sockaddr *) cs, &len); | ||
| 211 | if (n == -1) { | ||
| 212 | switch (errno) { | ||
| 213 | case EWOULDBLOCK: | ||
| 214 | case ECONNABORTED: | ||
| 215 | case EPROTO: | ||
| 216 | case EINTR: if (fcntl(s, F_SETFL, flags) == -1) | ||
| 217 | return (-1); | ||
| 218 | return (0); | ||
| 219 | default: return (-1); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | if (fcntl(s, F_SETFL, flags) == -1) | ||
| 223 | return (-1); | ||
| 224 | return (n); | ||
| 225 | } | ||
| 226 | if (fcntl(s, F_SETFL, flags) == -1) | ||
| 227 | return (-1); | ||
| 228 | return (0); | ||
| 229 | } | ||
| 230 | |||
| 231 | int | ||
| 232 | net_testvip (char *ip) | ||
| 233 | { | ||
| 234 | struct ifi_info *ifi, *ifc; | ||
| 235 | struct in_addr ip_n; | ||
| 236 | |||
| 237 | if (ip == NULL) | ||
| 238 | return (1); | ||
| 239 | if (strcmp(ip, "*") == 0) | ||
| 240 | return (1); | ||
| 241 | |||
| 242 | ip_n.s_addr = net_resolve(ip); | ||
| 243 | if (!(ip_n.s_addr)) | ||
| 244 | return (0); | ||
| 245 | |||
| 246 | ifi = net_ifi_get (AF_INET, 1); | ||
| 247 | if (ifi == NULL) | ||
| 248 | return (0); | ||
| 249 | for (ifc = ifi; ifc != NULL; ifc = ifc->ifi_next) { | ||
| 250 | if (memcmp(&ip_n.s_addr, &ifc->ifi_saddr.s_addr, sizeof(struct in_addr)) == 0) { | ||
| 251 | net_ifi_free(ifi); | ||
| 252 | return (1); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | net_ifi_free(ifi); | ||
| 256 | return (0); | ||
| 257 | } | ||
| 258 | |||
| 259 | void | ||
| 260 | net_ifi_free (struct ifi_info *tf) | ||
| 261 | { | ||
| 262 | struct ifi_info *ifi, *ifil; | ||
| 263 | |||
| 264 | ifil = NULL; | ||
| 265 | for (ifi = tf; ifi != NULL; ifi = ifi->ifi_next) { | ||
| 266 | if (ifil) | ||
| 267 | free (ifil); | ||
| 268 | if (ifi->ifi_addr) | ||
| 269 | free (ifi->ifi_addr); | ||
| 270 | ifil = ifi; | ||
| 271 | } | ||
| 272 | if (ifil) | ||
| 273 | free (ifil); | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | |||
| 277 | struct ifi_info * | ||
| 278 | net_ifi_get (int family, int doaliases) | ||
| 279 | { | ||
| 280 | struct ifi_info *ifi, *ifihead, **ifipnext; | ||
| 281 | int sockfd, len, lastlen, flags, myflags; | ||
| 282 | char *ptr, *buf, lastname[IFNAMSIZ], *cptr; | ||
| 283 | struct ifconf ifc; | ||
| 284 | struct ifreq *ifr, ifrcopy; | ||
| 285 | struct sockaddr_in *sinptr; | ||
| 286 | |||
| 287 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 288 | if (sockfd == -1) | ||
| 289 | return (NULL); | ||
| 290 | |||
| 291 | lastlen = 0; | ||
| 292 | len = 100 * sizeof(struct ifreq); | ||
| 293 | for (;;) { | ||
| 294 | buf = malloc(len); | ||
| 295 | if (buf == NULL) | ||
| 296 | return (NULL); | ||
| 297 | ifc.ifc_len = len; | ||
| 298 | ifc.ifc_buf = buf; | ||
| 299 | if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { | ||
| 300 | if (errno != EINVAL || lastlen != 0) | ||
| 301 | return (NULL); | ||
| 302 | } else { | ||
| 303 | if (ifc.ifc_len == lastlen) | ||
| 304 | break; | ||
| 305 | lastlen = ifc.ifc_len; | ||
| 306 | } | ||
| 307 | len += 10 * sizeof(struct ifreq); | ||
| 308 | free (buf); | ||
| 309 | } | ||
| 310 | ifihead = NULL; | ||
| 311 | ifipnext = &ifihead; | ||
| 312 | lastname[0] = 0; | ||
| 313 | |||
| 314 | for (ptr = buf; ptr < buf + ifc.ifc_len;) { | ||
| 315 | ifr = (struct ifreq *) ptr; | ||
| 316 | if (ifr->ifr_addr.sa_family == AF_INET) | ||
| 317 | len = sizeof(struct sockaddr); | ||
| 318 | ptr += sizeof(ifr->ifr_name) + len; | ||
| 319 | |||
| 320 | if (ifr->ifr_addr.sa_family != family) | ||
| 321 | continue; | ||
| 322 | myflags = 0; | ||
| 323 | if ((cptr = strchr(ifr->ifr_name, ':')) != NULL) | ||
| 324 | *cptr = 0; | ||
| 325 | if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) { | ||
| 326 | if (doaliases == 0) | ||
| 327 | continue; | ||
| 328 | myflags = IFI_ALIAS; | ||
| 329 | } | ||
| 330 | memcpy(lastname, ifr->ifr_name, IFNAMSIZ); | ||
| 331 | |||
| 332 | ifrcopy = *ifr; | ||
| 333 | if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0) | ||
| 334 | return (NULL); | ||
| 335 | flags = ifrcopy.ifr_flags; | ||
| 336 | if ((flags & IFF_UP) == 0) | ||
| 337 | continue; | ||
| 338 | |||
| 339 | ifi = calloc(1, sizeof(struct ifi_info)); | ||
| 340 | if (ifi == NULL) | ||
| 341 | return (NULL); | ||
| 342 | *ifipnext = ifi; | ||
| 343 | ifipnext = &ifi->ifi_next; | ||
| 344 | ifi->ifi_flags = flags; | ||
| 345 | ifi->ifi_myflags = myflags; | ||
| 346 | memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME); | ||
| 347 | ifi->ifi_name[IFI_NAME - 1] = '\0'; | ||
| 348 | |||
| 349 | #ifdef DEBUG | ||
| 350 | printf("got: %s\n", ifi->ifi_name); | ||
| 351 | #endif | ||
| 352 | |||
| 353 | switch (ifr->ifr_addr.sa_family) { | ||
| 354 | case AF_INET: | ||
| 355 | sinptr = (struct sockaddr_in *) &ifr->ifr_addr; | ||
| 356 | memcpy(&ifi->ifi_saddr, &sinptr->sin_addr, sizeof(struct in_addr)); | ||
| 357 | if (ifi->ifi_addr == NULL) { | ||
| 358 | ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in)); | ||
| 359 | if (ifi->ifi_addr == NULL) | ||
| 360 | return (NULL); | ||
| 361 | memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in)); | ||
| 362 | } | ||
| 363 | break; | ||
| 364 | default: | ||
| 365 | break; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | free (buf); | ||
| 369 | return (ifihead); | ||
| 370 | } | ||
| 371 | |||
| 372 | void | ||
| 373 | net_boundfree (bound *bf) | ||
| 374 | { | ||
| 375 | close (bf->bs); | ||
| 376 | free(bf); | ||
| 377 | return; | ||
| 378 | } | ||
| 379 | |||
| 380 | bound * | ||
| 381 | net_bind (char *ip, unsigned short int port) | ||
| 382 | { | ||
| 383 | bound *b; | ||
| 384 | int br, gsnr, lr; | ||
| 385 | int len; | ||
| 386 | struct sockaddr_in *sap; | ||
| 387 | |||
| 388 | if (port >= 65536) | ||
| 389 | return (NULL); | ||
| 390 | |||
| 391 | b = calloc(1, sizeof(bound)); | ||
| 392 | if (b == NULL) | ||
| 393 | return (NULL); | ||
| 394 | b->bs = socket(AF_INET, SOCK_STREAM, 0); | ||
| 395 | if (b->bs == -1) | ||
| 396 | goto berror; | ||
| 397 | |||
| 398 | sap = (struct sockaddr_in *) &b->bsa; | ||
| 399 | sap->sin_family = AF_INET; | ||
| 400 | sap->sin_port = htons(port); /* 0 = ephemeral */ | ||
| 401 | |||
| 402 | if (ip != NULL) { | ||
| 403 | if (strcmp(ip,"*") == 0) { | ||
| 404 | sap->sin_addr.s_addr = htonl(INADDR_ANY); | ||
| 405 | } else { | ||
| 406 | if (!(sap->sin_addr.s_addr = net_resolve(ip))) { | ||
| 407 | goto berror; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | } else { | ||
| 411 | sap->sin_addr.s_addr = htonl(INADDR_ANY); | ||
| 412 | } | ||
| 413 | |||
| 414 | br = bind(b->bs, (struct sockaddr *) &b->bsa, sizeof(struct sockaddr)); | ||
| 415 | if (br == -1) | ||
| 416 | goto berror; | ||
| 417 | |||
| 418 | len = sizeof(struct sockaddr); | ||
| 419 | gsnr = getsockname(b->bs, (struct sockaddr *) &b->bsa, &len); | ||
| 420 | b->port = ntohs(sap->sin_port); | ||
| 421 | if (gsnr == -1) | ||
| 422 | goto berror; | ||
| 423 | |||
| 424 | lr = listen(b->bs, 16); | ||
| 425 | if (lr == -1) { | ||
| 426 | goto berror; | ||
| 427 | } | ||
| 428 | return (b); | ||
| 429 | |||
| 430 | berror: | ||
| 431 | free(b); | ||
| 432 | |||
| 433 | return(NULL); | ||
| 434 | } | ||
| 435 | |||
| 436 | unsigned long int | ||
| 437 | net_resolve (char *host) | ||
| 438 | { | ||
| 439 | long i; | ||
| 440 | struct hostent *he; | ||
| 441 | |||
| 442 | i = inet_addr(host); | ||
| 443 | if (i == -1) { | ||
| 444 | he = gethostbyname(host); | ||
| 445 | if (he == NULL) { | ||
| 446 | return (0); | ||
| 447 | } else { | ||
| 448 | return (*(unsigned long *) he->h_addr); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | return (i); | ||
| 452 | } | ||
| 453 | |||
| 454 | int | ||
| 455 | net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, int sec) | ||
| 456 | { | ||
| 457 | int n, len, error, flags; | ||
| 458 | int fd; | ||
| 459 | struct timeval tv; | ||
| 460 | fd_set rset, wset; | ||
| 461 | |||
| 462 | if (!(cs->sin_addr.s_addr = net_resolve(server))) { | ||
| 463 | return (-1); | ||
| 464 | } | ||
| 465 | cs->sin_family = AF_INET; | ||
| 466 | cs->sin_port = htons(port); | ||
| 467 | |||
| 468 | fd = socket(cs->sin_family, SOCK_STREAM, 0); | ||
| 469 | if (fd == -1) | ||
| 470 | return (-1); | ||
| 471 | |||
| 472 | flags = fcntl(fd, F_GETFL, 0); | ||
| 473 | if (flags == -1) | ||
| 474 | return (-1); | ||
| 475 | n = fcntl(fd, F_SETFL, flags | O_NONBLOCK); | ||
| 476 | if (n == -1) | ||
| 477 | return (-1); | ||
| 478 | |||
| 479 | error = 0; | ||
| 480 | |||
| 481 | n = connect(fd, (struct sockaddr *) cs, sizeof(struct sockaddr_in)); | ||
| 482 | if (n < 0) { | ||
| 483 | if (errno != EINPROGRESS) { | ||
| 484 | return (-1); | ||
| 485 | } | ||
| 486 | } | ||
| 487 | if (n == 0) | ||
| 488 | goto done; | ||
| 489 | |||
| 490 | FD_ZERO(&rset); | ||
| 491 | FD_ZERO(&wset); | ||
| 492 | FD_SET(fd, &rset); | ||
| 493 | FD_SET(fd, &wset); | ||
| 494 | tv.tv_sec = sec; | ||
| 495 | tv.tv_usec = 0; | ||
| 496 | |||
| 497 | n = select(fd + 1, &rset, &wset, NULL, &tv); | ||
| 498 | if (n == 0) { | ||
| 499 | close(fd); | ||
| 500 | errno = ETIMEDOUT; | ||
| 501 | return (-1); | ||
| 502 | } | ||
| 503 | if (n == -1) | ||
| 504 | return (-1); | ||
| 505 | |||
| 506 | if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) { | ||
| 507 | if (FD_ISSET(fd, &rset) && FD_ISSET(fd, &wset)) { | ||
| 508 | len = sizeof(error); | ||
| 509 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { | ||
| 510 | errno = ETIMEDOUT; | ||
| 511 | return (-1); | ||
| 512 | } | ||
| 513 | if (error == 0) { | ||
| 514 | goto done; | ||
| 515 | } else { | ||
| 516 | errno = error; | ||
| 517 | return (-1); | ||
| 518 | } | ||
| 519 | } | ||
| 520 | } else | ||
| 521 | return (-1); | ||
| 522 | |||
| 523 | done: | ||
| 524 | n = fcntl(fd, F_SETFL, flags); | ||
| 525 | if (n == -1) | ||
| 526 | return (-1); | ||
| 527 | return (fd); | ||
| 528 | } | ||
| 529 | |||
| 530 | int | ||
| 531 | net_tline (char *buf, int bufsize) | ||
| 532 | { | ||
| 533 | int p; | ||
| 534 | |||
| 535 | for (p = 0; p < bufsize; p++) { | ||
| 536 | if (buf[p] == '\n') | ||
| 537 | return (p + 1); | ||
| 538 | } | ||
| 539 | return (-1); | ||
| 540 | } | ||
| 541 | |||
| 542 | int | ||
| 543 | net_rlinet (int fd, char *buf, int bufsize, int sec) | ||
| 544 | { | ||
| 545 | int n; | ||
| 546 | unsigned long int rb = 0; | ||
| 547 | struct timeval tv_start, tv_cur; | ||
| 548 | |||
| 549 | memset(buf, '\0', bufsize); | ||
| 550 | (void) gettimeofday(&tv_start, NULL); | ||
| 551 | |||
| 552 | do { | ||
| 553 | (void) gettimeofday(&tv_cur, NULL); | ||
| 554 | if (sec > 0) { | ||
| 555 | if ((((tv_cur.tv_sec * 1000000) + (tv_cur.tv_usec)) - | ||
| 556 | ((tv_start.tv_sec * 1000000) + (tv_start.tv_usec))) > (sec * 1000000)) { | ||
| 557 | return (-1); | ||
| 558 | } | ||
| 559 | } | ||
| 560 | n = net_rtimeout(fd, net_readtimeout); | ||
| 561 | if (n <= 0) { | ||
| 562 | return (-1); | ||
| 563 | } | ||
| 564 | n = read(fd, buf, 1); | ||
| 565 | if (n <= 0) { | ||
| 566 | return (n); | ||
| 567 | } | ||
| 568 | rb++; | ||
| 569 | if (*buf == '\n') | ||
| 570 | return (rb); | ||
| 571 | buf++; | ||
| 572 | if (rb >= bufsize) | ||
| 573 | return (-1); | ||
| 574 | } while (1); | ||
| 575 | } | ||
| 576 | |||
| 577 | long int | ||
| 578 | net_rbuf (int fd, char **dst) | ||
| 579 | { | ||
| 580 | long int ml = 0; | ||
| 581 | long int read_bytes; | ||
| 582 | int p; | ||
| 583 | |||
| 584 | while ((p = net_rtimeout(fd, net_readtimeout)) == 1) { | ||
| 585 | *dst = (char *) realloc(*dst, ml + NET_BSIZE); | ||
| 586 | if (*dst == NULL) | ||
| 587 | return (-1); | ||
| 588 | ml += read_bytes = read(fd, *dst + ml, NET_BSIZE); | ||
| 589 | if (read_bytes == 0) { | ||
| 590 | *dst = (char *) realloc(*dst, ml); | ||
| 591 | if ((*dst == NULL) && (ml == 0)) { | ||
| 592 | return (1); | ||
| 593 | } else if (*dst == NULL) { | ||
| 594 | return (-1); | ||
| 595 | } else { | ||
| 596 | return (ml); | ||
| 597 | } | ||
| 598 | } | ||
| 599 | } | ||
| 600 | return (-1); | ||
| 601 | } | ||
| 602 | |||
| 603 | int | ||
| 604 | net_rbuft (int fd, char *dst, unsigned long int dsize) | ||
| 605 | { | ||
| 606 | unsigned long int bl = 0, m; | ||
| 607 | int p; | ||
| 608 | |||
| 609 | while (bl < dsize) { | ||
| 610 | p = net_rtimeout(fd, net_readtimeout); | ||
| 611 | if ((p == 0) || (p == -1)) { | ||
| 612 | return (-1); | ||
| 613 | } | ||
| 614 | |||
| 615 | m = read(fd, dst + bl, (dsize - bl)); | ||
| 616 | if ((m == 0) || (m == -1)) { | ||
| 617 | return (-1); | ||
| 618 | } | ||
| 619 | bl += m; | ||
| 620 | } | ||
| 621 | return (1); | ||
| 622 | } | ||
| 623 | |||
| 624 | int | ||
| 625 | net_rtimeout (int fd, int sec) | ||
| 626 | { | ||
| 627 | fd_set rset; | ||
| 628 | struct timeval tv; | ||
| 629 | int n, error, flags; | ||
| 630 | |||
| 631 | error = 0; | ||
| 632 | flags = fcntl(fd, F_GETFL, 0); | ||
| 633 | n = fcntl(fd, F_SETFL, flags | O_NONBLOCK); | ||
| 634 | if (n == -1) | ||
| 635 | return (-1); | ||
| 636 | |||
| 637 | FD_ZERO(&rset); | ||
| 638 | FD_SET(fd, &rset); | ||
| 639 | tv.tv_sec = sec; | ||
| 640 | tv.tv_usec = 0; | ||
| 641 | |||
| 642 | /* now we wait until more data is received then the tcp low level watermark, | ||
| 643 | * which should be setted to 1 in this case (1 is default) | ||
| 644 | */ | ||
| 645 | |||
| 646 | n = select(fd + 1, &rset, NULL, NULL, &tv); | ||
| 647 | if (n == 0) { | ||
| 648 | n = fcntl(fd, F_SETFL, flags); | ||
| 649 | if (n == -1) | ||
| 650 | return (-1); | ||
| 651 | errno = ETIMEDOUT; | ||
| 652 | return (-1); | ||
| 653 | } | ||
| 654 | if (n == -1) { | ||
| 655 | return (-1); | ||
| 656 | } | ||
| 657 | /* socket readable ? */ | ||
| 658 | if (FD_ISSET(fd, &rset)) { | ||
| 659 | n = fcntl(fd, F_SETFL, flags); | ||
| 660 | if (n == -1) | ||
| 661 | return (-1); | ||
| 662 | return (1); | ||
| 663 | } else { | ||
| 664 | n = fcntl(fd, F_SETFL, flags); | ||
| 665 | if (n == -1) | ||
| 666 | return (-1); | ||
| 667 | errno = ETIMEDOUT; | ||
| 668 | return (-1); | ||
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | void | ||
| 673 | net_write (int fd, const char *str, ...) | ||
| 674 | { | ||
| 675 | char tmp[1025]; | ||
| 676 | va_list vl; | ||
| 677 | int i; | ||
| 678 | |||
| 679 | va_start(vl, str); | ||
| 680 | memset(tmp, 0, sizeof(tmp)); | ||
| 681 | i = vsnprintf(tmp, sizeof(tmp), str, vl); | ||
| 682 | va_end(vl); | ||
| 683 | |||
| 684 | #ifdef DEBUG | ||
| 685 | printf("[snd] %s\n", tmp); | ||
| 686 | #endif | ||
| 687 | |||
| 688 | send(fd, tmp, i, 0); | ||
| 689 | return; | ||
| 690 | } | ||
| 691 | |||
diff --git a/other/fizzbounce/network.h b/other/fizzbounce/network.h new file mode 100644 index 0000000..0fcaa06 --- /dev/null +++ b/other/fizzbounce/network.h | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | /* scut's leet network library ;) | ||
| 2 | * 1999 (c) scut | ||
| 3 | * | ||
| 4 | * networking code | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <sys/socket.h> | ||
| 8 | #include <net/if.h> | ||
| 9 | #include <netinet/in.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | |||
| 12 | #ifndef _NETWORK_H | ||
| 13 | #define _NETWORK_H | ||
| 14 | |||
| 15 | #define NET_READTIMEOUT 180 | ||
| 16 | #define NET_CONNTIMEOUT 60 | ||
| 17 | #define NET_IDENTTIMEOUT 15 | ||
| 18 | |||
| 19 | #define IFI_NAME 16 | ||
| 20 | #define IFI_HADDR 8 | ||
| 21 | |||
| 22 | /* pointer to this struct list returned by net_get_ifi | ||
| 23 | */ | ||
| 24 | struct ifi_info { | ||
| 25 | char ifi_name[IFI_NAME]; | ||
| 26 | u_char ifi_haddr[IFI_HADDR]; | ||
| 27 | u_short ifi_hlen; | ||
| 28 | short ifi_flags; | ||
| 29 | short ifi_myflags; | ||
| 30 | struct sockaddr *ifi_addr; | ||
| 31 | struct in_addr ifi_saddr; | ||
| 32 | struct ifi_info *ifi_next; | ||
| 33 | }; | ||
| 34 | |||
| 35 | #define IFI_ALIAS 1 | ||
| 36 | |||
| 37 | typedef struct bound { | ||
| 38 | int bs; /* bound socket */ | ||
| 39 | unsigned short port; /* port we bound to */ | ||
| 40 | struct sockaddr bsa; /* bs_in */ | ||
| 41 | } bound; | ||
| 42 | |||
| 43 | extern int net_readtimeout; | ||
| 44 | extern int net_conntimeout; | ||
| 45 | extern int net_identtimeout; | ||
| 46 | |||
| 47 | /* net_socks_connect | ||
| 48 | * | ||
| 49 | * relays through an open socks 5 server (NO AUTH type) | ||
| 50 | * returns a socket descriptor which is already connected | ||
| 51 | */ | ||
| 52 | |||
| 53 | int net_socks_connect (char *socks, unsigned short int sport, char *server, unsigned short int port, int sec); | ||
| 54 | |||
| 55 | /* net_socks_put_s5info | ||
| 56 | * | ||
| 57 | * inserts socks 5 compatible relay information into socket s5s, | ||
| 58 | * used to relay over more then just one socks server | ||
| 59 | */ | ||
| 60 | |||
| 61 | int net_socks_put_s5info (int s5s, char *server, unsigned short int port, int sec); | ||
| 62 | |||
| 63 | /* net_parseip | ||
| 64 | * | ||
| 65 | * reads an ip in the format "1.1.1.1:299" or "blabla:481" into | ||
| 66 | * the char pointer *ip and into the port *port | ||
| 67 | * | ||
| 68 | * returns 0 on failure | ||
| 69 | * returns 1 on success | ||
| 70 | */ | ||
| 71 | int net_parseip (char *inp, char **ip, unsigned short int *port); | ||
| 72 | |||
| 73 | /* net_getlocalip | ||
| 74 | * | ||
| 75 | * returns the local IP address as string on success | ||
| 76 | * returns NULL on failure | ||
| 77 | */ | ||
| 78 | |||
| 79 | char *net_getlocalip (void); | ||
| 80 | |||
| 81 | /* net_descriptify | ||
| 82 | * | ||
| 83 | * descriptifies a socket ;) | ||
| 84 | * returns -1 on failure | ||
| 85 | * returns file descriptor on success | ||
| 86 | */ | ||
| 87 | |||
| 88 | FILE *net_descriptify (int socket); | ||
| 89 | |||
| 90 | /* net_ident | ||
| 91 | * | ||
| 92 | * idents a connection identified by the host:port pairs on both sides, | ||
| 93 | * returning the ident in *ident | ||
| 94 | * | ||
| 95 | * returns 1 on success | ||
| 96 | * returns -1 on failure | ||
| 97 | */ | ||
| 98 | |||
| 99 | int net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport, | ||
| 100 | struct sockaddr_in *remotes, unsigned short int remoteport); | ||
| 101 | |||
| 102 | /* net_accept | ||
| 103 | * accepts a connection from socket s, and stores the connection | ||
| 104 | * into cs. | ||
| 105 | * wait a maximum amount of maxsec seconds for connections | ||
| 106 | * maxsec can also be zero (infinite wait, until connection) | ||
| 107 | * | ||
| 108 | * returns 0 if no connection has been made within maxsec seconds | ||
| 109 | * returns -1 if an error appears | ||
| 110 | * returns the socket number if a connection has been made | ||
| 111 | */ | ||
| 112 | |||
| 113 | int net_accept (int s, struct sockaddr_in *cs, int maxsec); | ||
| 114 | |||
| 115 | /* net_get_ifi | ||
| 116 | * get interface information | ||
| 117 | * returns NULL on failure | ||
| 118 | * returns a pointer to a linked list structure ifi_info (see above) | ||
| 119 | */ | ||
| 120 | |||
| 121 | struct ifi_info *net_ifi_get (int family, int doaliases); | ||
| 122 | void net_ifi_free (struct ifi_info *tf); | ||
| 123 | |||
| 124 | /* net_testvip | ||
| 125 | * tests if virtual ip/hostname is available for use on the local machine, | ||
| 126 | * returns 1 if the ip can be used | ||
| 127 | * 0 if the ip/host is not available | ||
| 128 | */ | ||
| 129 | int net_testvip (char *ip); | ||
| 130 | |||
| 131 | /* net_bind | ||
| 132 | * binds a socket to an ip:port on the local machine, | ||
| 133 | * ip can be either NULL (bind to all IP's on the host), or a pointer | ||
| 134 | * to a virtual host name, or a real IP, or "*" for any. | ||
| 135 | * port can be either 0 (ephemeral port), or any free port. | ||
| 136 | * | ||
| 137 | * returns NULL on failure | ||
| 138 | * pointer to bound structure on success | ||
| 139 | */ | ||
| 140 | bound *net_bind (char *ip, unsigned short int port); | ||
| 141 | void net_boundfree (bound *bf); | ||
| 142 | |||
| 143 | /* net_resolve | ||
| 144 | * resolves host into s_addr | ||
| 145 | */ | ||
| 146 | unsigned long int net_resolve(char *host); | ||
| 147 | |||
| 148 | /* net_connect | ||
| 149 | * connects to the given server and port with a max timeout of sec | ||
| 150 | * initializes the sockaddr_in struct correctly (ipv4), accepts any | ||
| 151 | * ip "123.123.123.123" or hostname "localhost", "www.yahoo.de" as hostname | ||
| 152 | * creates a new socket and returns either -1 if failed or | ||
| 153 | * the socket if connection has been established within the timeout limit | ||
| 154 | * routine is still IPv4 biased :-/ | ||
| 155 | * | ||
| 156 | * returns -1 on failure | ||
| 157 | * returns socket if success | ||
| 158 | */ | ||
| 159 | int net_connect(struct sockaddr_in *cs, char *server, unsigned short int port, int sec); | ||
| 160 | |||
| 161 | /* net_rtimeout | ||
| 162 | * waits max <sec> seconds for fd to become readable | ||
| 163 | * returns -1 on error (errno set) | ||
| 164 | * returns 1 on readability | ||
| 165 | */ | ||
| 166 | int net_rtimeout(int fd, int sec); | ||
| 167 | |||
| 168 | /* net_rbuf | ||
| 169 | * allocates memory and reads to *dst until connection close | ||
| 170 | * returns n if success (n = number of bytes read) | ||
| 171 | * returns -1 if failed | ||
| 172 | */ | ||
| 173 | long int net_rbuf(int fd, char **dst); | ||
| 174 | #define NET_BSIZE 4096 | ||
| 175 | |||
| 176 | /* net_rbuft | ||
| 177 | * reads dsize bytes into dst from fd, with timeout | ||
| 178 | * returns 1 on success | ||
| 179 | * returns -1 on failure | ||
| 180 | */ | ||
| 181 | int net_rbuft(int fd, char *dst, unsigned long int dsize); | ||
| 182 | |||
| 183 | /* net_rlinet | ||
| 184 | * reads a line from socket descriptor with timeout to buffer | ||
| 185 | * if sec = 0, then only a continuous stream of data is required, not | ||
| 186 | * an overall timeout. | ||
| 187 | * returns -1 on timeout | ||
| 188 | * returns 0 on connection close | ||
| 189 | * returns length of readen line (including '\n') | ||
| 190 | */ | ||
| 191 | int net_rlinet(int fd, char *buf, int bufsize, int sec); | ||
| 192 | |||
| 193 | /* net_tline | ||
| 194 | * returns length if string contains '\n' | ||
| 195 | * returns -1 if no '\n' in string | ||
| 196 | */ | ||
| 197 | int net_tline(char *buf, int bufsize); | ||
| 198 | |||
| 199 | /* net_write | ||
| 200 | * prints a formatted string to a socket | ||
| 201 | */ | ||
| 202 | void net_write(int fd, const char *str, ...); | ||
| 203 | |||
| 204 | #endif | ||
| 205 | |||
diff --git a/other/fizzbounce/relay.c b/other/fizzbounce/relay.c new file mode 100644 index 0000000..194824c --- /dev/null +++ b/other/fizzbounce/relay.c | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | |||
| 2 | /* bounce 4 all | ||
| 3 | * 1999 (c) scut | ||
| 4 | * | ||
| 5 | * relay routines | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <sys/socket.h> | ||
| 9 | #include <sys/types.h> | ||
| 10 | #include <sys/time.h> | ||
| 11 | #include <netinet/in.h> | ||
| 12 | #include <unistd.h> | ||
| 13 | #include <arpa/inet.h> | ||
| 14 | #include <pthread.h> | ||
| 15 | #include <errno.h> | ||
| 16 | #include <stdio.h> | ||
| 17 | #include <stdlib.h> | ||
| 18 | #include <string.h> | ||
| 19 | #include "client.h" | ||
| 20 | #include "network.h" | ||
| 21 | #include "relay.h" | ||
| 22 | |||
| 23 | void | ||
| 24 | rly_client (client *cl) | ||
| 25 | { | ||
| 26 | int n, i = 0, maxfd; | ||
| 27 | int bc = 1; | ||
| 28 | char cbuff[4096], sbuff[4096]; | ||
| 29 | fd_set chainset; | ||
| 30 | struct timeval tval; | ||
| 31 | |||
| 32 | pthread_mutex_lock (&cl->cl_mutex); | ||
| 33 | |||
| 34 | /* now, since we authorized us, we should just chain every control | ||
| 35 | * connection command except it is in our parser array, in this case | ||
| 36 | * we should call the parser function :) | ||
| 37 | */ | ||
| 38 | |||
| 39 | pthread_mutex_unlock (&cl->cl_mutex); | ||
| 40 | |||
| 41 | memset (cbuff, '\0', sizeof (cbuff)); | ||
| 42 | memset (sbuff, '\0', sizeof (sbuff)); | ||
| 43 | |||
| 44 | while (bc) { | ||
| 45 | pthread_mutex_lock (&cl->cl_mutex); | ||
| 46 | |||
| 47 | FD_ZERO (&chainset); | ||
| 48 | FD_SET (cl->cs, &chainset); | ||
| 49 | FD_SET (cl->ss, &chainset); | ||
| 50 | maxfd = ((cl->cs > cl->ss) ? cl->cs : cl->ss) + 1; | ||
| 51 | tval.tv_sec = 1; | ||
| 52 | tval.tv_usec = 0; | ||
| 53 | |||
| 54 | /* choose a dual approach, a bit polling (once per second, but also | ||
| 55 | * select, to let the mutex live | ||
| 56 | */ | ||
| 57 | n = select (maxfd, &chainset, NULL, NULL, &tval); | ||
| 58 | switch (n) { | ||
| 59 | case (0): break; | ||
| 60 | case (-1): return; | ||
| 61 | default: if (FD_ISSET (cl->cs, &chainset)) { | ||
| 62 | i = rly_rb (cl->cs, cbuff, sizeof (cbuff)); | ||
| 63 | if (i <= 0) { | ||
| 64 | bc = 0; | ||
| 65 | } else { | ||
| 66 | rly_clparse (cl, cbuff, sizeof (cbuff)); | ||
| 67 | } | ||
| 68 | } else if (FD_ISSET (cl->ss, &chainset)) { | ||
| 69 | i = rly_rb (cl->ss, sbuff, sizeof (sbuff)); | ||
| 70 | if (i <= 0) { | ||
| 71 | bc = 0; | ||
| 72 | } else { | ||
| 73 | rly_srvparse (cl, sbuff, sizeof (sbuff)); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | |||
| 79 | pthread_mutex_unlock (&cl->cl_mutex); | ||
| 80 | } | ||
| 81 | |||
| 82 | switch (i) { | ||
| 83 | case (0): printf ("connection close\n"); | ||
| 84 | break; | ||
| 85 | case (-1): printf ("system error (%d)\n", errno); | ||
| 86 | break; | ||
| 87 | case (-2): printf ("buffer too short to hold all recv data\n"); | ||
| 88 | break; | ||
| 89 | default: printf ("weird i = %d\n", i); | ||
| 90 | break; | ||
| 91 | } | ||
| 92 | |||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | int | ||
| 97 | rly_clparse (client *cl, char *buffer, int buflen) | ||
| 98 | { | ||
| 99 | int r, n, llen, prslen; | ||
| 100 | |||
| 101 | while ((llen = net_tline (buffer, buflen)) != -1) { | ||
| 102 | |||
| 103 | /* if (strncmp (buffer, "USER", 4) == 0) { | ||
| 104 | char *a, *b; | ||
| 105 | int n; | ||
| 106 | |||
| 107 | n = sscanf (buffer, "USER %a[^ ] \"%*[^\"]\" \"%*[^\"]\"%*[^:]:%as\n", | ||
| 108 | &a, &b); | ||
| 109 | if (n != 2) { | ||
| 110 | printf ("wrong user command: %s\n", buffer); | ||
| 111 | goto pfft; | ||
| 112 | } | ||
| 113 | net_write (cl->ss, "USER %s \"%s\" \"%s\" :%s", a, cl->connip, cl->ircip, b); | ||
| 114 | goto mhhh; | ||
| 115 | } | ||
| 116 | */ | ||
| 117 | /* if line was terminated, replace \n with \0 */ | ||
| 118 | pfft: buffer[llen-1] = '\0'; | ||
| 119 | net_write (cl->ss, "%s\n", buffer); | ||
| 120 | |||
| 121 | mhhh: memmove (buffer, buffer + llen, buflen - llen); | ||
| 122 | |||
| 123 | memset (buffer + (buflen - llen), '\0', llen); | ||
| 124 | } | ||
| 125 | |||
| 126 | return (1); | ||
| 127 | } | ||
| 128 | |||
| 129 | int | ||
| 130 | rly_srvparse (client *cl, char *buffer, int buflen) | ||
| 131 | { | ||
| 132 | int code, r, n, llen; | ||
| 133 | |||
| 134 | while ((llen = net_tline (buffer, buflen)) != -1) { | ||
| 135 | buffer[llen-1] = '\0'; | ||
| 136 | net_write (cl->cs, "%s\n", buffer); | ||
| 137 | memmove (buffer, buffer + llen, buflen - llen); | ||
| 138 | memset (buffer + (buflen - llen), '\0', llen); | ||
| 139 | } | ||
| 140 | return (1); | ||
| 141 | } | ||
| 142 | |||
| 143 | int | ||
| 144 | rly_rb (int fd, char *buffer, int buflen) | ||
| 145 | { | ||
| 146 | int n, csize; | ||
| 147 | |||
| 148 | /* assume asciiz buffer, line based, | ||
| 149 | * now append the read data to this buffer | ||
| 150 | */ | ||
| 151 | |||
| 152 | csize = strlen (buffer); | ||
| 153 | if (csize >= (buflen - 1)) | ||
| 154 | return (-2); /* buffer size exceeded, lame script kiddie */ | ||
| 155 | |||
| 156 | n = read (fd, buffer + csize, buflen - csize - 1); | ||
| 157 | switch (n) { | ||
| 158 | case (0): return (0); | ||
| 159 | case (-1): return (-1); | ||
| 160 | default: return (strlen (buffer)); | ||
| 161 | } | ||
| 162 | |||
| 163 | /* should never happen */ | ||
| 164 | return (0); | ||
| 165 | } | ||
| 166 | |||
diff --git a/other/fizzbounce/relay.h b/other/fizzbounce/relay.h new file mode 100644 index 0000000..a99fa94 --- /dev/null +++ b/other/fizzbounce/relay.h | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | #ifndef FIZZ_RELAY_H | ||
| 2 | #define FIZZ_RELAY_H | ||
| 3 | #include "client.h" | ||
| 4 | |||
| 5 | void rly_client (client *cl); | ||
| 6 | int rly_clparse (client *cl, char *buffer, int buflen); | ||
| 7 | int rly_srvparse (client *cl, char *buffer, int buflen); | ||
| 8 | int rly_rb (int fd, char *buffer, int buflen); | ||
| 9 | |||
| 10 | |||
| 11 | #endif | ||
| 12 | |||
