diff options
Diffstat (limited to 'other/zylyx/src')
| -rw-r--r-- | other/zylyx/src/Makefile | 32 | ||||
| -rw-r--r-- | other/zylyx/src/common.c | 290 | ||||
| -rw-r--r-- | other/zylyx/src/common.h | 25 | ||||
| -rw-r--r-- | other/zylyx/src/network.c | 861 | ||||
| -rw-r--r-- | other/zylyx/src/network.h | 342 | ||||
| -rw-r--r-- | other/zylyx/src/proxy.c | 206 | ||||
| -rw-r--r-- | other/zylyx/src/proxy.h | 32 | ||||
| -rw-r--r-- | other/zylyx/src/screen.c | 311 | ||||
| -rw-r--r-- | other/zylyx/src/screen.h | 45 | ||||
| -rw-r--r-- | other/zylyx/src/zylyx.c | 187 | ||||
| -rw-r--r-- | other/zylyx/src/zylyx.h | 30 |
11 files changed, 2361 insertions, 0 deletions
diff --git a/other/zylyx/src/Makefile b/other/zylyx/src/Makefile new file mode 100644 index 0000000..05b9167 --- /dev/null +++ b/other/zylyx/src/Makefile | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | |||
| 2 | # zylyx - makefile | ||
| 3 | # by scut of teso | ||
| 4 | |||
| 5 | DFLAGS=-Wall | ||
| 6 | LIBS=-lpthread -lslang | ||
| 7 | CC=gcc | ||
| 8 | CFLAGS=$(DFLAGS) | ||
| 9 | PREFIX=/usr/local | ||
| 10 | |||
| 11 | all: zylyx | ||
| 12 | |||
| 13 | clean: | ||
| 14 | rm -f *.o | ||
| 15 | |||
| 16 | zylyx: common.o network.o proxy.o screen.o zylyx.c | ||
| 17 | $(CC) $(CFLAGS) -o zylyx zylyx.c common.o network.o proxy.o screen.o $(LIBS) | ||
| 18 | strip zylyx | ||
| 19 | mv zylyx ../ | ||
| 20 | |||
| 21 | common.o: common.c | ||
| 22 | $(CC) $(CFLAGS) -c common.c | ||
| 23 | |||
| 24 | network.o: network.c | ||
| 25 | $(CC) $(CFLAGS) -c network.c | ||
| 26 | |||
| 27 | proxy.o: proxy.c | ||
| 28 | $(CC) $(CFLAGS) -c proxy.c | ||
| 29 | |||
| 30 | screen.o: screen.c | ||
| 31 | $(CC) $(CFLAGS) -c screen.c | ||
| 32 | |||
diff --git a/other/zylyx/src/common.c b/other/zylyx/src/common.c new file mode 100644 index 0000000..3f3a99a --- /dev/null +++ b/other/zylyx/src/common.c | |||
| @@ -0,0 +1,290 @@ | |||
| 1 | |||
| 2 | #include <sys/time.h> | ||
| 3 | #include <netinet/in.h> | ||
| 4 | #include <unistd.h> | ||
| 5 | #include <pthread.h> | ||
| 6 | #include <time.h> | ||
| 7 | #include <stdarg.h> | ||
| 8 | #include <stdio.h> | ||
| 9 | #include <string.h> | ||
| 10 | #include <stdlib.h> | ||
| 11 | #include <unistd.h> | ||
| 12 | #include "common.h" | ||
| 13 | |||
| 14 | #ifdef DEBUG | ||
| 15 | void | ||
| 16 | debugp (char *filename, const char *str, ...) | ||
| 17 | { | ||
| 18 | FILE *fp; /* temporary file pointer */ | ||
| 19 | va_list vl; | ||
| 20 | |||
| 21 | fp = fopen (filename, "a"); | ||
| 22 | if (fp == NULL) | ||
| 23 | return; | ||
| 24 | |||
| 25 | va_start (vl, str); | ||
| 26 | vfprintf (fp, str, vl); | ||
| 27 | va_end (vl); | ||
| 28 | |||
| 29 | fclose (fp); | ||
| 30 | |||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | void | ||
| 35 | hexdump (char *filename, unsigned char *data, unsigned int amount) | ||
| 36 | { | ||
| 37 | FILE *fp; /* temporary file pointer */ | ||
| 38 | unsigned int dp, p; /* data pointer */ | ||
| 39 | const char trans[] = "................................ !\"#$%&'()*+,-./0123456789" | ||
| 40 | ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm" | ||
| 41 | "nopqrstuvwxyz{|}~...................................." | ||
| 42 | "....................................................." | ||
| 43 | "........................................"; | ||
| 44 | |||
| 45 | fp = fopen (filename, "a"); | ||
| 46 | if (fp == NULL) | ||
| 47 | return; | ||
| 48 | |||
| 49 | fprintf (fp, "\n-packet-\n"); | ||
| 50 | |||
| 51 | for (dp = 1; dp <= amount; dp++) { | ||
| 52 | fprintf (fp, "%02x ", data[dp-1]); | ||
| 53 | if ((dp % 8) == 0) | ||
| 54 | fprintf (fp, " "); | ||
| 55 | if ((dp % 16) == 0) { | ||
| 56 | fprintf (fp, "| "); | ||
| 57 | p = dp; | ||
| 58 | for (dp -= 16; dp < p; dp++) | ||
| 59 | fprintf (fp, "%c", trans[data[dp]]); | ||
| 60 | fflush (fp); | ||
| 61 | fprintf (fp, "\n"); | ||
| 62 | } | ||
| 63 | fflush (fp); | ||
| 64 | } | ||
| 65 | if ((amount % 16) != 0) { | ||
| 66 | p = dp = 16 - (amount % 16); | ||
| 67 | for (dp = p; dp > 0; dp--) { | ||
| 68 | fprintf (fp, " "); | ||
| 69 | if (((dp % 8) == 0) && (p != 8)) | ||
| 70 | fprintf (fp, " "); | ||
| 71 | fflush (fp); | ||
| 72 | } | ||
| 73 | fprintf (fp, " | "); | ||
| 74 | for (dp = (amount - (16 - p)); dp < amount; dp++) | ||
| 75 | fprintf (fp, "%c", trans[data[dp]]); | ||
| 76 | fflush (fp); | ||
| 77 | } | ||
| 78 | fprintf (fp, "\n"); | ||
| 79 | |||
| 80 | fclose (fp); | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | #endif | ||
| 85 | |||
| 86 | |||
| 87 | /* m_random | ||
| 88 | * | ||
| 89 | * return a random number between `lowmark´ and `highmark´ | ||
| 90 | */ | ||
| 91 | |||
| 92 | int | ||
| 93 | m_random (int lowmark, int highmark) | ||
| 94 | { | ||
| 95 | long int rnd; | ||
| 96 | |||
| 97 | /* flip/swap them in case user messed up | ||
| 98 | */ | ||
| 99 | if (lowmark > highmark) { | ||
| 100 | lowmark ^= highmark; | ||
| 101 | highmark ^= lowmark; | ||
| 102 | lowmark ^= highmark; | ||
| 103 | } | ||
| 104 | rnd = lowmark; | ||
| 105 | |||
| 106 | srandom ((unsigned int) time (NULL)); | ||
| 107 | rnd += (random () % (highmark - lowmark)); | ||
| 108 | |||
| 109 | /* this is lame, i know :) | ||
| 110 | */ | ||
| 111 | return (rnd); | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | /* set_tv | ||
| 116 | * | ||
| 117 | * initializes a struct timeval pointed to by `tv' to a second value of | ||
| 118 | * `seconds' | ||
| 119 | * | ||
| 120 | * return in any case | ||
| 121 | */ | ||
| 122 | |||
| 123 | void | ||
| 124 | set_tv (struct timeval *tv, int seconds) | ||
| 125 | { | ||
| 126 | tv->tv_sec = seconds; | ||
| 127 | tv->tv_usec = 0; | ||
| 128 | |||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | /* xstrupper | ||
| 134 | * | ||
| 135 | * uppercase a string `str' | ||
| 136 | * | ||
| 137 | * return in any case | ||
| 138 | */ | ||
| 139 | |||
| 140 | void | ||
| 141 | xstrupper (char *str) | ||
| 142 | { | ||
| 143 | for (; *str != '\0'; ++str) { | ||
| 144 | if (*str >= 'a' && *str <= 'z') { | ||
| 145 | *str -= ('a' - 'A'); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | /* concating snprintf | ||
| 154 | * | ||
| 155 | * determines the length of the string pointed to by `os', appending formatted | ||
| 156 | * string to a maximium length of `len'. | ||
| 157 | * | ||
| 158 | */ | ||
| 159 | |||
| 160 | void | ||
| 161 | scnprintf (char *os, size_t len, const char *str, ...) | ||
| 162 | { | ||
| 163 | va_list vl; | ||
| 164 | char *ostmp = os + strlen (os); | ||
| 165 | |||
| 166 | va_start (vl, str); | ||
| 167 | vsnprintf (ostmp, len - strlen (os) - 1, str, vl); | ||
| 168 | va_end (vl); | ||
| 169 | |||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | unsigned long int | ||
| 175 | t_passed (struct timeval *old) | ||
| 176 | { | ||
| 177 | unsigned long int tp; | ||
| 178 | struct timeval tv; | ||
| 179 | |||
| 180 | if (old == NULL) | ||
| 181 | return (0); | ||
| 182 | if (old->tv_sec == 0 && old->tv_usec == 0) | ||
| 183 | return (0); | ||
| 184 | |||
| 185 | gettimeofday (&tv, NULL); | ||
| 186 | |||
| 187 | tp = ((tv.tv_sec * 1000000) + tv.tv_usec) - | ||
| 188 | ((old->tv_sec * 1000000) + old->tv_usec); | ||
| 189 | |||
| 190 | return (tp); | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | unsigned long int | ||
| 195 | tdiff (struct timeval *old, struct timeval *new) | ||
| 196 | { | ||
| 197 | unsigned long int time1; | ||
| 198 | |||
| 199 | if (new->tv_sec >= old->tv_sec) { | ||
| 200 | time1 = new->tv_sec - old->tv_sec; | ||
| 201 | if ((new->tv_usec - 500000) >= old->tv_usec) | ||
| 202 | time1++; | ||
| 203 | } else { | ||
| 204 | time1 = old->tv_sec - new->tv_sec; | ||
| 205 | if ((old->tv_usec - 500000) >= new->tv_usec) | ||
| 206 | time1++; | ||
| 207 | } | ||
| 208 | |||
| 209 | return (time1); | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | /* ipv4_print | ||
| 214 | * | ||
| 215 | * padding = 0 -> don't padd | ||
| 216 | * padding = 1 -> padd with zeros | ||
| 217 | * padding = 2 -> padd with spaces | ||
| 218 | */ | ||
| 219 | |||
| 220 | char * | ||
| 221 | ipv4_print (char *dest, struct in_addr in, int padding) | ||
| 222 | { | ||
| 223 | unsigned char *ipp; | ||
| 224 | |||
| 225 | ipp = (unsigned char *) &in.s_addr; | ||
| 226 | |||
| 227 | strcpy (dest, ""); | ||
| 228 | |||
| 229 | switch (padding) { | ||
| 230 | case (0): | ||
| 231 | sprintf (dest, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 232 | break; | ||
| 233 | case (1): | ||
| 234 | sprintf (dest, "%03d.%03d.%03d.%03d", ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 235 | break; | ||
| 236 | case (2): | ||
| 237 | sprintf (dest, "%3d.%3d.%3d.%3d", ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 238 | break; | ||
| 239 | default: | ||
| 240 | break; | ||
| 241 | } | ||
| 242 | |||
| 243 | return (dest); | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | void * | ||
| 248 | xrealloc (void *m_ptr, size_t newsize) | ||
| 249 | { | ||
| 250 | void *n_ptr; | ||
| 251 | |||
| 252 | n_ptr = realloc (m_ptr, newsize); | ||
| 253 | if (n_ptr == NULL) { | ||
| 254 | fprintf (stderr, "realloc failed\n"); | ||
| 255 | exit (EXIT_FAILURE); | ||
| 256 | } | ||
| 257 | |||
| 258 | return (n_ptr); | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | char * | ||
| 263 | xstrdup (char *str) | ||
| 264 | { | ||
| 265 | char *b; | ||
| 266 | |||
| 267 | b = strdup (str); | ||
| 268 | if (b == NULL) { | ||
| 269 | fprintf (stderr, "strdup failed\n"); | ||
| 270 | exit (EXIT_FAILURE); | ||
| 271 | } | ||
| 272 | |||
| 273 | return (b); | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | void * | ||
| 278 | xcalloc (int factor, size_t size) | ||
| 279 | { | ||
| 280 | void *bla; | ||
| 281 | |||
| 282 | bla = calloc (factor, size); | ||
| 283 | |||
| 284 | if (bla == NULL) { | ||
| 285 | fprintf (stderr, "no memory left\n"); | ||
| 286 | exit (EXIT_FAILURE); | ||
| 287 | } | ||
| 288 | |||
| 289 | return (bla); | ||
| 290 | } | ||
diff --git a/other/zylyx/src/common.h b/other/zylyx/src/common.h new file mode 100644 index 0000000..273a83b --- /dev/null +++ b/other/zylyx/src/common.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | |||
| 2 | #include <sys/time.h> | ||
| 3 | #include <netinet/in.h> | ||
| 4 | #include <unistd.h> | ||
| 5 | |||
| 6 | #ifndef Z_COMMON_H | ||
| 7 | #define Z_COMMON_H | ||
| 8 | |||
| 9 | #ifdef DEBUG | ||
| 10 | void debugp (char *filename, const char *str, ...); | ||
| 11 | void hexdump (char *filename, unsigned char *data, unsigned int amount); | ||
| 12 | #endif | ||
| 13 | int m_random (int lowmark, int highmark); | ||
| 14 | void set_tv (struct timeval *tv, int seconds); | ||
| 15 | void xstrupper (char *str); | ||
| 16 | void scnprintf (char *os, size_t len, const char *str, ...); | ||
| 17 | unsigned long int t_passed (struct timeval *old); | ||
| 18 | unsigned long int tdiff (struct timeval *old, struct timeval *new); | ||
| 19 | char *ipv4_print (char *dest, struct in_addr in, int padding); | ||
| 20 | void *xrealloc (void *m_ptr, size_t newsize); | ||
| 21 | char *xstrdup (char *str); | ||
| 22 | void *xcalloc (int factor, size_t size); | ||
| 23 | |||
| 24 | #endif | ||
| 25 | |||
diff --git a/other/zylyx/src/network.c b/other/zylyx/src/network.c new file mode 100644 index 0000000..ab0f0f7 --- /dev/null +++ b/other/zylyx/src/network.c | |||
| @@ -0,0 +1,861 @@ | |||
| 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/types.h> | ||
| 16 | #include <sys/ioctl.h> | ||
| 17 | #include <sys/socket.h> | ||
| 18 | #include <sys/time.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 | |||
| 37 | int | ||
| 38 | net_socks_connect (char *socks, unsigned short int sport, char *server, unsigned short int port, int sec) | ||
| 39 | { | ||
| 40 | int s5s; | ||
| 41 | struct sockaddr_in cs; | ||
| 42 | |||
| 43 | s5s = net_connect (&cs, socks, sport, NULL, 0, sec); | ||
| 44 | if (s5s == -1) | ||
| 45 | return (-1); | ||
| 46 | |||
| 47 | if (net_socks_put_s5info (s5s, server, port, sec) == -1) { | ||
| 48 | close (s5s); | ||
| 49 | return (-1); | ||
| 50 | } | ||
| 51 | return (s5s); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | int | ||
| 56 | net_socks_put_s5info (int s5s, char *server, unsigned short int port, int sec) | ||
| 57 | { | ||
| 58 | int n; | ||
| 59 | char buff[1024]; | ||
| 60 | |||
| 61 | /* v5 + noauth */ | ||
| 62 | net_write (s5s, "\x05\x01%c", 0); | ||
| 63 | if (net_rtimeout (s5s, sec) == -1) | ||
| 64 | return (-1); | ||
| 65 | recv (s5s, buff, sizeof (buff), 0); | ||
| 66 | |||
| 67 | /* chain us =) */ | ||
| 68 | net_write (s5s, "\x05\x01%c\x03%c%s%c%c", 0, strlen (server), server, (port >> 8) & 0xff, port & 0xff); | ||
| 69 | if (net_rtimeout (s5s, sec) == -1) | ||
| 70 | return (-1); | ||
| 71 | n = recv (s5s, buff, sizeof (buff), 0); | ||
| 72 | if (buff[1] != 0x00) { | ||
| 73 | return (-1); | ||
| 74 | } | ||
| 75 | return (1); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | int | ||
| 80 | net_parseip (char *inp, char **ip, unsigned short int *port) | ||
| 81 | { | ||
| 82 | int n; | ||
| 83 | |||
| 84 | if (inp == NULL) | ||
| 85 | return (0); | ||
| 86 | if (strchr (inp, ':') == NULL) | ||
| 87 | return (0); | ||
| 88 | |||
| 89 | *ip = calloc (1, 256); | ||
| 90 | if (*ip == NULL) | ||
| 91 | return (0); | ||
| 92 | |||
| 93 | n = sscanf (inp, "%[^:]:%hu", *ip, port); | ||
| 94 | if (n != 2) | ||
| 95 | return (0); | ||
| 96 | |||
| 97 | *ip = realloc (*ip, strlen (*ip) + 1); | ||
| 98 | if (*ip == NULL || (*port < 1 || *port > 65535)) | ||
| 99 | return (0); | ||
| 100 | |||
| 101 | return (1); | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | char * | ||
| 106 | net_getlocalip (void) | ||
| 107 | { | ||
| 108 | struct sockaddr_in pf; | ||
| 109 | char name[255]; | ||
| 110 | |||
| 111 | memset (name, '\0', sizeof (name)); | ||
| 112 | |||
| 113 | if (gethostname (name, sizeof (name) - 1) == -1) { | ||
| 114 | return (NULL); | ||
| 115 | } | ||
| 116 | |||
| 117 | pf.sin_addr.s_addr = net_resolve (name); | ||
| 118 | |||
| 119 | return (strdup (inet_ntoa (pf.sin_addr)));; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | char * | ||
| 124 | net_peername (int socket) | ||
| 125 | { | ||
| 126 | struct sockaddr_in peeraddr; | ||
| 127 | struct hostent he, *hep; | ||
| 128 | size_t size = sizeof (struct sockaddr_in); | ||
| 129 | int n, h_errno; | ||
| 130 | unsigned char h_buf[8192]; | ||
| 131 | |||
| 132 | if (getpeername (socket, (struct sockaddr *) &peeraddr, &size) == -1) | ||
| 133 | return (NULL); | ||
| 134 | |||
| 135 | /* digital unix / hp-ux freaks mod here =) | ||
| 136 | */ | ||
| 137 | n = gethostbyaddr_r ((char *) &peeraddr.sin_addr, sizeof (struct in_addr), | ||
| 138 | AF_INET, &he, h_buf, sizeof (h_buf), &hep, &h_errno); | ||
| 139 | |||
| 140 | if (hep == NULL) { | ||
| 141 | char *ip_str = NULL; | ||
| 142 | |||
| 143 | net_printipa (&peeraddr.sin_addr, &ip_str); | ||
| 144 | return (ip_str); | ||
| 145 | } | ||
| 146 | |||
| 147 | return (strdup (he.h_name)); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | FILE * | ||
| 152 | net_descriptify (int socket) | ||
| 153 | { | ||
| 154 | FILE *fp; | ||
| 155 | |||
| 156 | fp = fdopen (socket, "r+"); | ||
| 157 | return ((fp == NULL) ? (NULL) : (fp)); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | /* loosely based on rfc931.c */ | ||
| 162 | |||
| 163 | int | ||
| 164 | net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport, | ||
| 165 | struct sockaddr_in *remotes, unsigned short int remoteport) | ||
| 166 | { | ||
| 167 | int is; /* ident socket */ | ||
| 168 | struct sockaddr_in isa; | ||
| 169 | int n; | ||
| 170 | char identreply[512], *cp; | ||
| 171 | unsigned int rmt_port, our_port; | ||
| 172 | |||
| 173 | |||
| 174 | *ident = NULL; | ||
| 175 | |||
| 176 | is = net_connect (&isa, inet_ntoa (remotes->sin_addr), 113, NULL, 0, net_identtimeout); | ||
| 177 | if (is == -1) | ||
| 178 | return (-1); | ||
| 179 | |||
| 180 | /* ident request */ | ||
| 181 | net_write (is, "%u,%u\r\n", remoteport, localport); | ||
| 182 | memset (identreply, '\0', sizeof (identreply)); | ||
| 183 | |||
| 184 | n = net_rlinet (is, identreply, sizeof(identreply) -1, net_identtimeout); | ||
| 185 | if (n == -1) { | ||
| 186 | close (is); | ||
| 187 | return (-1); | ||
| 188 | } | ||
| 189 | close (is); | ||
| 190 | |||
| 191 | *ident = calloc (1, 256); | ||
| 192 | #ifdef DEBUG | ||
| 193 | printf("%s\n", identreply); | ||
| 194 | #endif | ||
| 195 | n = sscanf (identreply, "%u , %u : USERID :%*[^:]:%255s", &rmt_port, &our_port, *ident); | ||
| 196 | if (n != 3) { | ||
| 197 | free (*ident); | ||
| 198 | *ident = NULL; | ||
| 199 | return (-1); | ||
| 200 | } | ||
| 201 | |||
| 202 | /* check the ports 'man */ | ||
| 203 | if ((rmt_port != remoteport) || (our_port != localport)) { | ||
| 204 | free (*ident); | ||
| 205 | *ident = NULL; | ||
| 206 | return (-1); | ||
| 207 | } | ||
| 208 | |||
| 209 | /* strip character and save some memory */ | ||
| 210 | if ((cp = strchr (*ident, '\r'))) | ||
| 211 | *cp = '\0'; | ||
| 212 | n = strlen (*ident); | ||
| 213 | *ident = realloc (*ident, n + 1); | ||
| 214 | (*ident)[n] = '\0'; | ||
| 215 | |||
| 216 | #ifdef DEBUG | ||
| 217 | printf("ident-return: %s\n", *ident); | ||
| 218 | #endif | ||
| 219 | return (1); | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | int | ||
| 224 | net_accept (int s, struct sockaddr_in *cs, int maxsec) | ||
| 225 | { | ||
| 226 | int flags, n; | ||
| 227 | fd_set ac_s; | ||
| 228 | int len; | ||
| 229 | struct timeval tval; | ||
| 230 | |||
| 231 | flags = fcntl(s, F_GETFL, 0); | ||
| 232 | if (flags == -1) | ||
| 233 | return (-1); | ||
| 234 | n = fcntl(s, F_SETFL, flags | O_NONBLOCK); | ||
| 235 | if (flags == -1) | ||
| 236 | return (-1); | ||
| 237 | |||
| 238 | FD_ZERO(&ac_s); | ||
| 239 | FD_SET(s, &ac_s); | ||
| 240 | tval.tv_sec = maxsec; | ||
| 241 | tval.tv_usec = 0; | ||
| 242 | |||
| 243 | n = select(s + 1, &ac_s, NULL, NULL, maxsec ? &tval : NULL); | ||
| 244 | if (n == 0) | ||
| 245 | return (0); | ||
| 246 | |||
| 247 | if (FD_ISSET(s, &ac_s)) { | ||
| 248 | len = sizeof(struct sockaddr_in); | ||
| 249 | n = accept(s, (struct sockaddr *) cs, &len); | ||
| 250 | if (n == -1) { | ||
| 251 | switch (errno) { | ||
| 252 | case EWOULDBLOCK: | ||
| 253 | case ECONNABORTED: | ||
| 254 | case EPROTO: | ||
| 255 | case EINTR: if (fcntl(s, F_SETFL, flags) == -1) | ||
| 256 | return (-1); | ||
| 257 | return (0); | ||
| 258 | default: return (-1); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | if (fcntl(s, F_SETFL, flags) == -1) | ||
| 262 | return (-1); | ||
| 263 | return (n); | ||
| 264 | } | ||
| 265 | if (fcntl(s, F_SETFL, flags) == -1) | ||
| 266 | return (-1); | ||
| 267 | return (0); | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | int | ||
| 272 | net_testvip (char *ip) | ||
| 273 | { | ||
| 274 | struct ifi_info *ifi, *ifc; | ||
| 275 | struct in_addr ip_n; | ||
| 276 | |||
| 277 | if (ip == NULL) | ||
| 278 | return (1); | ||
| 279 | if (strcmp(ip, "*") == 0) | ||
| 280 | return (1); | ||
| 281 | |||
| 282 | ip_n.s_addr = net_resolve(ip); | ||
| 283 | if (!(ip_n.s_addr)) | ||
| 284 | return (0); | ||
| 285 | |||
| 286 | ifi = net_ifi_get (AF_INET, 1); | ||
| 287 | if (ifi == NULL) | ||
| 288 | return (0); | ||
| 289 | for (ifc = ifi; ifc != NULL; ifc = ifc->ifi_next) { | ||
| 290 | if (memcmp (&ip_n.s_addr, &ifc->ifi_saddr.s_addr, sizeof (struct in_addr)) == 0) { | ||
| 291 | net_ifi_free(ifi); | ||
| 292 | return (1); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | net_ifi_free(ifi); | ||
| 296 | return (0); | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | void | ||
| 301 | net_ifi_free (struct ifi_info *tf) | ||
| 302 | { | ||
| 303 | struct ifi_info *ifi, *ifil; | ||
| 304 | |||
| 305 | ifil = NULL; | ||
| 306 | for (ifi = tf; ifi != NULL; ifi = ifi->ifi_next) { | ||
| 307 | if (ifil) | ||
| 308 | free (ifil); | ||
| 309 | if (ifi->ifi_addr) | ||
| 310 | free (ifi->ifi_addr); | ||
| 311 | ifil = ifi; | ||
| 312 | } | ||
| 313 | if (ifil) | ||
| 314 | free (ifil); | ||
| 315 | return; | ||
| 316 | } | ||
| 317 | |||
| 318 | |||
| 319 | struct ifi_info * | ||
| 320 | net_ifi_get (int family, int doaliases) | ||
| 321 | { | ||
| 322 | struct ifi_info *ifi, *ifihead, **ifipnext; | ||
| 323 | int sockfd, len, lastlen, flags, myflags; | ||
| 324 | char *ptr, *buf, lastname[IFNAMSIZ], *cptr; | ||
| 325 | struct ifconf ifc; | ||
| 326 | struct ifreq *ifr, ifrcopy; | ||
| 327 | struct sockaddr_in *sinptr; | ||
| 328 | |||
| 329 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 330 | if (sockfd == -1) | ||
| 331 | return (NULL); | ||
| 332 | |||
| 333 | lastlen = 0; | ||
| 334 | len = 100 * sizeof(struct ifreq); | ||
| 335 | for (;;) { | ||
| 336 | buf = malloc(len); | ||
| 337 | if (buf == NULL) | ||
| 338 | return (NULL); | ||
| 339 | ifc.ifc_len = len; | ||
| 340 | ifc.ifc_buf = buf; | ||
| 341 | if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { | ||
| 342 | if (errno != EINVAL || lastlen != 0) | ||
| 343 | return (NULL); | ||
| 344 | } else { | ||
| 345 | if (ifc.ifc_len == lastlen) | ||
| 346 | break; | ||
| 347 | lastlen = ifc.ifc_len; | ||
| 348 | } | ||
| 349 | len += 10 * sizeof(struct ifreq); | ||
| 350 | free (buf); | ||
| 351 | } | ||
| 352 | ifihead = NULL; | ||
| 353 | ifipnext = &ifihead; | ||
| 354 | lastname[0] = 0; | ||
| 355 | |||
| 356 | for (ptr = buf; ptr < buf + ifc.ifc_len;) { | ||
| 357 | ifr = (struct ifreq *) ptr; | ||
| 358 | if (ifr->ifr_addr.sa_family == AF_INET) | ||
| 359 | len = sizeof(struct sockaddr); | ||
| 360 | ptr += sizeof(ifr->ifr_name) + len; | ||
| 361 | |||
| 362 | if (ifr->ifr_addr.sa_family != family) | ||
| 363 | continue; | ||
| 364 | myflags = 0; | ||
| 365 | if ((cptr = strchr(ifr->ifr_name, ':')) != NULL) | ||
| 366 | *cptr = 0; | ||
| 367 | if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) { | ||
| 368 | if (doaliases == 0) | ||
| 369 | continue; | ||
| 370 | myflags = IFI_ALIAS; | ||
| 371 | } | ||
| 372 | memcpy(lastname, ifr->ifr_name, IFNAMSIZ); | ||
| 373 | |||
| 374 | ifrcopy = *ifr; | ||
| 375 | if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0) | ||
| 376 | return (NULL); | ||
| 377 | flags = ifrcopy.ifr_flags; | ||
| 378 | if ((flags & IFF_UP) == 0) | ||
| 379 | continue; | ||
| 380 | |||
| 381 | ifi = calloc(1, sizeof(struct ifi_info)); | ||
| 382 | if (ifi == NULL) | ||
| 383 | return (NULL); | ||
| 384 | *ifipnext = ifi; | ||
| 385 | ifipnext = &ifi->ifi_next; | ||
| 386 | ifi->ifi_flags = flags; | ||
| 387 | ifi->ifi_myflags = myflags; | ||
| 388 | memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME); | ||
| 389 | ifi->ifi_name[IFI_NAME - 1] = '\0'; | ||
| 390 | |||
| 391 | #ifdef DEBUG | ||
| 392 | printf("got: %s\n", ifi->ifi_name); | ||
| 393 | #endif | ||
| 394 | |||
| 395 | switch (ifr->ifr_addr.sa_family) { | ||
| 396 | case AF_INET: | ||
| 397 | sinptr = (struct sockaddr_in *) &ifr->ifr_addr; | ||
| 398 | memcpy(&ifi->ifi_saddr, &sinptr->sin_addr, sizeof(struct in_addr)); | ||
| 399 | if (ifi->ifi_addr == NULL) { | ||
| 400 | ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in)); | ||
| 401 | if (ifi->ifi_addr == NULL) | ||
| 402 | return (NULL); | ||
| 403 | memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in)); | ||
| 404 | } | ||
| 405 | break; | ||
| 406 | default: | ||
| 407 | break; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | free (buf); | ||
| 411 | return (ifihead); | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | void | ||
| 416 | net_boundfree (bound *bf) | ||
| 417 | { | ||
| 418 | close (bf->bs); | ||
| 419 | free(bf); | ||
| 420 | return; | ||
| 421 | } | ||
| 422 | |||
| 423 | |||
| 424 | bound * | ||
| 425 | net_bind (char *ip, unsigned short int port) | ||
| 426 | { | ||
| 427 | bound *b; | ||
| 428 | int br, gsnr, lr; | ||
| 429 | int len, reusetmp; | ||
| 430 | struct sockaddr_in *sap; | ||
| 431 | |||
| 432 | if (port >= 65536) | ||
| 433 | return (NULL); | ||
| 434 | |||
| 435 | b = calloc(1, sizeof (bound)); | ||
| 436 | if (b == NULL) | ||
| 437 | return (NULL); | ||
| 438 | b->bs = socket (AF_INET, SOCK_STREAM, 0); | ||
| 439 | if (b->bs == -1) | ||
| 440 | goto berror; | ||
| 441 | |||
| 442 | reusetmp = 1; | ||
| 443 | #ifdef SO_REUSEPORT | ||
| 444 | if (setsockopt (b->bs, SOL_SOCKET, SO_REUSEPORT, &reusetmp, sizeof (reusetmp)) == -1) | ||
| 445 | goto berror; | ||
| 446 | #else | ||
| 447 | if (setsockopt (b->bs, SOL_SOCKET, SO_REUSEADDR, &reusetmp, sizeof (reusetmp)) == -1) | ||
| 448 | goto berror; | ||
| 449 | #endif | ||
| 450 | |||
| 451 | sap = (struct sockaddr_in *) &b->bsa; | ||
| 452 | sap->sin_family = AF_INET; | ||
| 453 | sap->sin_port = htons (port); /* 0 = ephemeral */ | ||
| 454 | |||
| 455 | if (ip != NULL) { | ||
| 456 | if (strcmp (ip, "*") == 0) { | ||
| 457 | sap->sin_addr.s_addr = htonl (INADDR_ANY); | ||
| 458 | } else { | ||
| 459 | if (!(sap->sin_addr.s_addr = net_resolve (ip))) { | ||
| 460 | goto berror; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | } else { | ||
| 464 | sap->sin_addr.s_addr = htonl (INADDR_ANY); | ||
| 465 | } | ||
| 466 | |||
| 467 | br = bind (b->bs, (struct sockaddr *) &b->bsa, sizeof (struct sockaddr)); | ||
| 468 | if (br == -1) | ||
| 469 | goto berror; | ||
| 470 | |||
| 471 | len = sizeof (struct sockaddr); | ||
| 472 | gsnr = getsockname (b->bs, (struct sockaddr *) &b->bsa, &len); | ||
| 473 | b->port = ntohs (sap->sin_port); | ||
| 474 | if (gsnr == -1) | ||
| 475 | goto berror; | ||
| 476 | |||
| 477 | lr = listen (b->bs, 16); | ||
| 478 | if (lr == -1) { | ||
| 479 | goto berror; | ||
| 480 | } | ||
| 481 | return (b); | ||
| 482 | |||
| 483 | berror: | ||
| 484 | free(b); | ||
| 485 | |||
| 486 | return(NULL); | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | unsigned long int | ||
| 491 | net_resolve (char *host) | ||
| 492 | { | ||
| 493 | long i; | ||
| 494 | struct hostent *he; | ||
| 495 | |||
| 496 | i = inet_addr(host); | ||
| 497 | if (i == -1) { | ||
| 498 | he = gethostbyname(host); | ||
| 499 | if (he == NULL) { | ||
| 500 | return (0); | ||
| 501 | } else { | ||
| 502 | return (*(unsigned long *) he->h_addr); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | return (i); | ||
| 506 | } | ||
| 507 | |||
| 508 | |||
| 509 | int | ||
| 510 | net_assignaddr (int sd, char *sourceip, unsigned short int sourceport) | ||
| 511 | { | ||
| 512 | struct sockaddr_in sourcedef; | ||
| 513 | |||
| 514 | if (sourceip && strcmp (sourceip, "*") == 0) | ||
| 515 | sourceip = NULL; | ||
| 516 | |||
| 517 | if (sourceip == NULL && sourceport == 0) | ||
| 518 | return (1); | ||
| 519 | |||
| 520 | /* is the IP available on the local host ? (not really necessary) */ | ||
| 521 | if (sourceip && !net_testvip (sourceip)) { | ||
| 522 | return (0); | ||
| 523 | } | ||
| 524 | |||
| 525 | memset (&sourcedef, '\0', sizeof (struct sockaddr_in)); | ||
| 526 | |||
| 527 | /* if sourceip is specified, set it */ | ||
| 528 | if (sourceip) { | ||
| 529 | sourcedef.sin_addr.s_addr = net_resolve (sourceip); | ||
| 530 | } else { | ||
| 531 | sourcedef.sin_addr.s_addr = htonl (INADDR_ANY); | ||
| 532 | } | ||
| 533 | if (sourceport) | ||
| 534 | sourcedef.sin_port = htons (sourceport); | ||
| 535 | |||
| 536 | /* now set the source on the socket by binding it */ | ||
| 537 | if (bind (sd, (struct sockaddr *) &sourcedef, sizeof (struct sockaddr_in)) == -1) { | ||
| 538 | return (0); | ||
| 539 | } | ||
| 540 | |||
| 541 | return (1); | ||
| 542 | } | ||
| 543 | |||
| 544 | |||
| 545 | int | ||
| 546 | net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, char *sourceip, | ||
| 547 | unsigned short int sourceport, int sec) | ||
| 548 | { | ||
| 549 | int n, len, error, flags; | ||
| 550 | int fd; | ||
| 551 | struct timeval tv; | ||
| 552 | fd_set rset, wset; | ||
| 553 | |||
| 554 | /* first allocate a socket */ | ||
| 555 | cs->sin_family = AF_INET; | ||
| 556 | cs->sin_port = htons (port); | ||
| 557 | fd = socket (cs->sin_family, SOCK_STREAM, 0); | ||
| 558 | if (fd == -1) | ||
| 559 | return (-1); | ||
| 560 | |||
| 561 | /* check wether we should change the defaults */ | ||
| 562 | if (net_assignaddr (fd, sourceip, sourceport) == 0) { | ||
| 563 | close (fd); | ||
| 564 | return (-1); | ||
| 565 | } | ||
| 566 | |||
| 567 | if (!(cs->sin_addr.s_addr = net_resolve (server))) { | ||
| 568 | close (fd); | ||
| 569 | return (-1); | ||
| 570 | } | ||
| 571 | |||
| 572 | flags = fcntl (fd, F_GETFL, 0); | ||
| 573 | if (flags == -1) { | ||
| 574 | close (fd); | ||
| 575 | return (-1); | ||
| 576 | } | ||
| 577 | n = fcntl (fd, F_SETFL, flags | O_NONBLOCK); | ||
| 578 | if (n == -1) { | ||
| 579 | close (fd); | ||
| 580 | return (-1); | ||
| 581 | } | ||
| 582 | |||
| 583 | error = 0; | ||
| 584 | |||
| 585 | n = connect (fd, (struct sockaddr *) cs, sizeof (struct sockaddr_in)); | ||
| 586 | if (n < 0) { | ||
| 587 | if (errno != EINPROGRESS) { | ||
| 588 | close (fd); | ||
| 589 | return (-1); | ||
| 590 | } | ||
| 591 | } | ||
| 592 | if (n == 0) | ||
| 593 | goto done; | ||
| 594 | |||
| 595 | FD_ZERO(&rset); | ||
| 596 | FD_ZERO(&wset); | ||
| 597 | FD_SET(fd, &rset); | ||
| 598 | FD_SET(fd, &wset); | ||
| 599 | tv.tv_sec = sec; | ||
| 600 | tv.tv_usec = 0; | ||
| 601 | |||
| 602 | n = select(fd + 1, &rset, &wset, NULL, &tv); | ||
| 603 | if (n == 0) { | ||
| 604 | close(fd); | ||
| 605 | errno = ETIMEDOUT; | ||
| 606 | return (-1); | ||
| 607 | } | ||
| 608 | if (n == -1) | ||
| 609 | return (-1); | ||
| 610 | |||
| 611 | if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) { | ||
| 612 | if (FD_ISSET(fd, &rset) && FD_ISSET(fd, &wset)) { | ||
| 613 | len = sizeof(error); | ||
| 614 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { | ||
| 615 | errno = ETIMEDOUT; | ||
| 616 | return (-1); | ||
| 617 | } | ||
| 618 | if (error == 0) { | ||
| 619 | goto done; | ||
| 620 | } else { | ||
| 621 | errno = error; | ||
| 622 | return (-1); | ||
| 623 | } | ||
| 624 | } | ||
| 625 | } else | ||
| 626 | return (-1); | ||
| 627 | |||
| 628 | done: | ||
| 629 | n = fcntl(fd, F_SETFL, flags); | ||
| 630 | if (n == -1) | ||
| 631 | return (-1); | ||
| 632 | return (fd); | ||
| 633 | } | ||
| 634 | |||
| 635 | |||
| 636 | int | ||
| 637 | net_tline (char *buf, int bufsize) | ||
| 638 | { | ||
| 639 | int p; | ||
| 640 | |||
| 641 | for (p = 0; p < bufsize; p++) { | ||
| 642 | if (buf[p] == '\n') | ||
| 643 | return (p + 1); | ||
| 644 | } | ||
| 645 | return (-1); | ||
| 646 | } | ||
| 647 | |||
| 648 | #define LINET_A 1024 | ||
| 649 | |||
| 650 | |||
| 651 | int | ||
| 652 | net_rlineta (int fd, char **buf, int sec) | ||
| 653 | { | ||
| 654 | int n; /* return value */ | ||
| 655 | int bufsize = 0; | ||
| 656 | |||
| 657 | *buf = NULL; | ||
| 658 | |||
| 659 | do { | ||
| 660 | bufsize += LINET_A; | ||
| 661 | *buf = realloc (*buf, bufsize); | ||
| 662 | if (*buf == NULL) | ||
| 663 | return (-1); | ||
| 664 | |||
| 665 | n = net_rlinet (fd, *buf + bufsize - LINET_A, LINET_A, sec); | ||
| 666 | |||
| 667 | if (n == -1) | ||
| 668 | goto rlinetaerr; | ||
| 669 | if (n >= 0) | ||
| 670 | goto rlinetastrip; | ||
| 671 | } while (n == -2); | ||
| 672 | |||
| 673 | rlinetastrip: | ||
| 674 | *buf = realloc (*buf, strlen (*buf) + 1); | ||
| 675 | return (strlen (*buf)); | ||
| 676 | |||
| 677 | rlinetaerr: | ||
| 678 | free (*buf); | ||
| 679 | return (-1); | ||
| 680 | } | ||
| 681 | |||
| 682 | |||
| 683 | int | ||
| 684 | net_rlinet (int fd, char *buf, int bufsize, int sec) | ||
| 685 | { | ||
| 686 | int n; | ||
| 687 | unsigned long int rb = 0; | ||
| 688 | struct timeval tv_start, tv_cur; | ||
| 689 | |||
| 690 | memset(buf, '\0', bufsize); | ||
| 691 | (void) gettimeofday(&tv_start, NULL); | ||
| 692 | |||
| 693 | do { | ||
| 694 | (void) gettimeofday(&tv_cur, NULL); | ||
| 695 | if (sec > 0) { | ||
| 696 | if ((((tv_cur.tv_sec * 1000000) + (tv_cur.tv_usec)) - | ||
| 697 | ((tv_start.tv_sec * 1000000) + (tv_start.tv_usec))) > (sec * 1000000)) { | ||
| 698 | return (-1); | ||
| 699 | } | ||
| 700 | } | ||
| 701 | n = net_rtimeout(fd, net_readtimeout); | ||
| 702 | if (n <= 0) { | ||
| 703 | return (-1); | ||
| 704 | } | ||
| 705 | n = read(fd, buf, 1); | ||
| 706 | if (n <= 0) { | ||
| 707 | return (n); | ||
| 708 | } | ||
| 709 | rb++; | ||
| 710 | if (*buf == '\n') | ||
| 711 | return (rb); | ||
| 712 | buf++; | ||
| 713 | if (rb >= bufsize) | ||
| 714 | return (-2); /* buffer full */ | ||
| 715 | } while (1); | ||
| 716 | } | ||
| 717 | |||
| 718 | |||
| 719 | long int | ||
| 720 | net_rbuf (int fd, char **dst) | ||
| 721 | { | ||
| 722 | long int ml = 0; | ||
| 723 | long int read_bytes; | ||
| 724 | int p; | ||
| 725 | |||
| 726 | while ((p = net_rtimeout(fd, net_readtimeout)) == 1) { | ||
| 727 | *dst = (char *) realloc(*dst, ml + NET_BSIZE); | ||
| 728 | if (*dst == NULL) | ||
| 729 | return (-1); | ||
| 730 | ml += read_bytes = read(fd, *dst + ml, NET_BSIZE); | ||
| 731 | if (read_bytes == 0) { | ||
| 732 | *dst = (char *) realloc(*dst, ml); | ||
| 733 | if ((*dst == NULL) && (ml == 0)) { | ||
| 734 | return (1); | ||
| 735 | } else if (*dst == NULL) { | ||
| 736 | return (-1); | ||
| 737 | } else { | ||
| 738 | return (ml); | ||
| 739 | } | ||
| 740 | } | ||
| 741 | } | ||
| 742 | return (-1); | ||
| 743 | } | ||
| 744 | |||
| 745 | |||
| 746 | int | ||
| 747 | net_rbuft (int fd, char *dst, unsigned long int dsize) | ||
| 748 | { | ||
| 749 | unsigned long int bl = 0, m; | ||
| 750 | int p; | ||
| 751 | |||
| 752 | while (bl < dsize) { | ||
| 753 | p = net_rtimeout(fd, net_readtimeout); | ||
| 754 | if ((p == 0) || (p == -1)) { | ||
| 755 | return (-1); | ||
| 756 | } | ||
| 757 | |||
| 758 | m = read(fd, dst + bl, (dsize - bl)); | ||
| 759 | if ((m == 0) || (m == -1)) { | ||
| 760 | return (-1); | ||
| 761 | } | ||
| 762 | bl += m; | ||
| 763 | } | ||
| 764 | return (1); | ||
| 765 | } | ||
| 766 | |||
| 767 | |||
| 768 | int | ||
| 769 | net_rtimeout (int fd, int sec) | ||
| 770 | { | ||
| 771 | fd_set rset; | ||
| 772 | struct timeval tv; | ||
| 773 | int n, error, flags; | ||
| 774 | |||
| 775 | error = 0; | ||
| 776 | flags = fcntl(fd, F_GETFL, 0); | ||
| 777 | n = fcntl(fd, F_SETFL, flags | O_NONBLOCK); | ||
| 778 | if (n == -1) | ||
| 779 | return (-1); | ||
| 780 | |||
| 781 | FD_ZERO(&rset); | ||
| 782 | FD_SET(fd, &rset); | ||
| 783 | tv.tv_sec = sec; | ||
| 784 | tv.tv_usec = 0; | ||
| 785 | |||
| 786 | /* now we wait until more data is received then the tcp low level watermark, | ||
| 787 | * which should be setted to 1 in this case (1 is default) | ||
| 788 | */ | ||
| 789 | |||
| 790 | n = select(fd + 1, &rset, NULL, NULL, &tv); | ||
| 791 | if (n == 0) { | ||
| 792 | n = fcntl(fd, F_SETFL, flags); | ||
| 793 | if (n == -1) | ||
| 794 | return (-1); | ||
| 795 | errno = ETIMEDOUT; | ||
| 796 | return (-1); | ||
| 797 | } | ||
| 798 | if (n == -1) { | ||
| 799 | return (-1); | ||
| 800 | } | ||
| 801 | /* socket readable ? */ | ||
| 802 | if (FD_ISSET(fd, &rset)) { | ||
| 803 | n = fcntl(fd, F_SETFL, flags); | ||
| 804 | if (n == -1) | ||
| 805 | return (-1); | ||
| 806 | return (1); | ||
| 807 | } else { | ||
| 808 | n = fcntl(fd, F_SETFL, flags); | ||
| 809 | if (n == -1) | ||
| 810 | return (-1); | ||
| 811 | errno = ETIMEDOUT; | ||
| 812 | return (-1); | ||
| 813 | } | ||
| 814 | } | ||
| 815 | |||
| 816 | |||
| 817 | void | ||
| 818 | net_write (int fd, const char *str, ...) | ||
| 819 | { | ||
| 820 | char tmp[1025]; | ||
| 821 | va_list vl; | ||
| 822 | int i; | ||
| 823 | |||
| 824 | va_start(vl, str); | ||
| 825 | memset(tmp, 0, sizeof(tmp)); | ||
| 826 | i = vsnprintf(tmp, sizeof(tmp), str, vl); | ||
| 827 | va_end(vl); | ||
| 828 | |||
| 829 | send(fd, tmp, i, 0); | ||
| 830 | return; | ||
| 831 | } | ||
| 832 | |||
| 833 | |||
| 834 | int | ||
| 835 | net_printip (struct in_addr *ia, char *str, size_t len) | ||
| 836 | { | ||
| 837 | unsigned char *ipp; | ||
| 838 | |||
| 839 | ipp = (unsigned char *) &ia->s_addr; | ||
| 840 | snprintf (str, len - 1, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 841 | |||
| 842 | return (0); | ||
| 843 | } | ||
| 844 | |||
| 845 | |||
| 846 | int | ||
| 847 | net_printipa (struct in_addr *ia, char **str) | ||
| 848 | { | ||
| 849 | unsigned char *ipp; | ||
| 850 | |||
| 851 | ipp = (unsigned char *) &ia->s_addr; | ||
| 852 | *str = calloc (1, 256); | ||
| 853 | if (*str == NULL) | ||
| 854 | return (1); | ||
| 855 | |||
| 856 | snprintf (*str, 255, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 857 | *str = realloc (*str, strlen (*str) + 1); | ||
| 858 | |||
| 859 | return ((*str == NULL) ? 1 : 0); | ||
| 860 | } | ||
| 861 | |||
diff --git a/other/zylyx/src/network.h b/other/zylyx/src/network.h new file mode 100644 index 0000000..46a8b54 --- /dev/null +++ b/other/zylyx/src/network.h | |||
| @@ -0,0 +1,342 @@ | |||
| 1 | /* scut's leet network library ;) | ||
| 2 | * 1999 (c) scut | ||
| 3 | * | ||
| 4 | * networking code | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef SCUT_NETWORK_H | ||
| 8 | #define SCUT_NETWORK_H | ||
| 9 | |||
| 10 | #include <sys/socket.h> | ||
| 11 | #include <net/if.h> | ||
| 12 | #include <netinet/in.h> | ||
| 13 | #include <stdio.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 | /* struct ifi_info | ||
| 23 | * | ||
| 24 | * a linked list giving information about all the network interfaces available | ||
| 25 | * a pointer to this struct list is returned by net_get_ifi. | ||
| 26 | */ | ||
| 27 | |||
| 28 | struct ifi_info { | ||
| 29 | char ifi_name[IFI_NAME]; | ||
| 30 | u_char ifi_haddr[IFI_HADDR]; | ||
| 31 | u_short ifi_hlen; | ||
| 32 | short ifi_flags; | ||
| 33 | short ifi_myflags; | ||
| 34 | struct sockaddr *ifi_addr; | ||
| 35 | struct in_addr ifi_saddr; | ||
| 36 | struct ifi_info *ifi_next; | ||
| 37 | }; | ||
| 38 | |||
| 39 | #define IFI_ALIAS 1 | ||
| 40 | |||
| 41 | typedef struct bound { | ||
| 42 | int bs; /* bound socket */ | ||
| 43 | unsigned short port; /* port we bound to */ | ||
| 44 | struct sockaddr bsa; /* bs_in */ | ||
| 45 | } bound; | ||
| 46 | |||
| 47 | extern int net_readtimeout; | ||
| 48 | extern int net_conntimeout; | ||
| 49 | extern int net_identtimeout; | ||
| 50 | |||
| 51 | |||
| 52 | /* net_socks_connect | ||
| 53 | * | ||
| 54 | * relays through an open socks 5 server (NO AUTH type) | ||
| 55 | * returns a socket descriptor which is already connected | ||
| 56 | */ | ||
| 57 | |||
| 58 | int net_socks_connect (char *socks, unsigned short int sport, | ||
| 59 | char *server, unsigned short int port, int sec); | ||
| 60 | |||
| 61 | |||
| 62 | /* net_socks_put_s5info | ||
| 63 | * | ||
| 64 | * insert socks 5 compatible relay information into socket s5s, | ||
| 65 | * used to relay over more then just one socks server | ||
| 66 | */ | ||
| 67 | |||
| 68 | int net_socks_put_s5info (int s5s, char *server, | ||
| 69 | unsigned short int port, int sec); | ||
| 70 | |||
| 71 | |||
| 72 | /* net_parseip | ||
| 73 | * | ||
| 74 | * read an ip in the format "1.1.1.1:299" or "blabla:481" into | ||
| 75 | * the char pointer *ip and into the port *port | ||
| 76 | * | ||
| 77 | * return 0 on failure | ||
| 78 | * return 1 on success | ||
| 79 | */ | ||
| 80 | |||
| 81 | int net_parseip (char *inp, char **ip, unsigned short int *port); | ||
| 82 | |||
| 83 | |||
| 84 | /* net_getlocalip | ||
| 85 | * | ||
| 86 | * give back the main IP of the local machine | ||
| 87 | * | ||
| 88 | * return the local IP address as string on success | ||
| 89 | * return NULL on failure | ||
| 90 | */ | ||
| 91 | |||
| 92 | char *net_getlocalip (void); | ||
| 93 | |||
| 94 | |||
| 95 | /* net_peername | ||
| 96 | * | ||
| 97 | * return a pointer that points to an alloced character array that contains | ||
| 98 | * the fully qualified hostname for the remote host that is connected using | ||
| 99 | * the socket descriptor `socket'. | ||
| 100 | + | ||
| 101 | * return NULL on failure | ||
| 102 | * return pointer to hostname or quad-dotted IP address | ||
| 103 | */ | ||
| 104 | |||
| 105 | char *net_peername (int socket); | ||
| 106 | |||
| 107 | |||
| 108 | /* net_descriptify | ||
| 109 | * | ||
| 110 | * descriptify a socket `socket' ;) | ||
| 111 | * | ||
| 112 | * return -1 on failure | ||
| 113 | * return file descriptor on success | ||
| 114 | */ | ||
| 115 | |||
| 116 | FILE *net_descriptify (int socket); | ||
| 117 | |||
| 118 | |||
| 119 | /* net_ident | ||
| 120 | * | ||
| 121 | * ident a connection identified by the host:port pairs on both sides, | ||
| 122 | * returning the ident in *ident | ||
| 123 | * | ||
| 124 | * return 1 on success | ||
| 125 | * return -1 on failure | ||
| 126 | */ | ||
| 127 | |||
| 128 | int net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport, | ||
| 129 | struct sockaddr_in *remotes, unsigned short int remoteport); | ||
| 130 | |||
| 131 | |||
| 132 | /* net_accept | ||
| 133 | * | ||
| 134 | * accept a connection from socket s, and stores the connection | ||
| 135 | * into cs. | ||
| 136 | * wait a maximum amount of maxsec seconds for connections | ||
| 137 | * maxsec can also be zero (infinite wait, until connection) | ||
| 138 | * | ||
| 139 | * return 0 if no connection has been made within maxsec seconds | ||
| 140 | * return -1 if an error appears | ||
| 141 | * return the socket number if a connection has been made | ||
| 142 | */ | ||
| 143 | |||
| 144 | int net_accept (int s, struct sockaddr_in *cs, int maxsec); | ||
| 145 | |||
| 146 | |||
| 147 | /* net_get_ifi | ||
| 148 | * | ||
| 149 | * get network interface information | ||
| 150 | * | ||
| 151 | * return NULL on failure | ||
| 152 | * return a pointer to a linked list structure ifi_info (see above) | ||
| 153 | */ | ||
| 154 | |||
| 155 | struct ifi_info *net_ifi_get (int family, int doaliases); | ||
| 156 | |||
| 157 | |||
| 158 | /* net_ifi_free | ||
| 159 | * | ||
| 160 | * free the linked list associated with `tf'. | ||
| 161 | * | ||
| 162 | * return in any case | ||
| 163 | */ | ||
| 164 | |||
| 165 | void net_ifi_free (struct ifi_info *tf); | ||
| 166 | |||
| 167 | |||
| 168 | /* net_testvip | ||
| 169 | * | ||
| 170 | * test if virtual ip/hostname is available for use on the local machine, | ||
| 171 | * | ||
| 172 | * return 1 if the ip can be used | ||
| 173 | * return 0 if the ip/host is not available | ||
| 174 | */ | ||
| 175 | |||
| 176 | int net_testvip (char *ip); | ||
| 177 | |||
| 178 | |||
| 179 | /* net_bind | ||
| 180 | * | ||
| 181 | * bind a socket to an ip:port on the local machine, | ||
| 182 | * `ip' can be either NULL (bind to all IP's on the host), or a pointer | ||
| 183 | * to a virtual host name, or a real IP, or "*" for any. | ||
| 184 | * `port' can be either 0 (ephemeral port), or any free port. | ||
| 185 | * | ||
| 186 | * return NULL on failure | ||
| 187 | * return pointer to bound structure on success | ||
| 188 | */ | ||
| 189 | |||
| 190 | bound *net_bind (char *ip, unsigned short int port); | ||
| 191 | |||
| 192 | |||
| 193 | /* net_boundfree | ||
| 194 | * | ||
| 195 | * free the bound structure pointed to by `bf' | ||
| 196 | * | ||
| 197 | * return in any case | ||
| 198 | */ | ||
| 199 | |||
| 200 | void net_boundfree (bound *bf); | ||
| 201 | |||
| 202 | |||
| 203 | /* net_resolve | ||
| 204 | * | ||
| 205 | * resolve a hostname pointed to by `host' into a s_addr return value | ||
| 206 | * | ||
| 207 | * return the correct formatted `s_addr' for this host on success | ||
| 208 | * return 0 on failure | ||
| 209 | */ | ||
| 210 | |||
| 211 | unsigned long int net_resolve (char *host); | ||
| 212 | |||
| 213 | |||
| 214 | /* net_assignaddr | ||
| 215 | * | ||
| 216 | * assign an IP address and port to a socket | ||
| 217 | * sourceip can be an IP or hostname that is available on the local host, | ||
| 218 | * or NULL/"*" to let the kernel choose one, same applies to sourceport, | ||
| 219 | * it can either be zero (ephemeral port choosen by the kernel) or a | ||
| 220 | * valid port number | ||
| 221 | * | ||
| 222 | * return 1 on success | ||
| 223 | * return 0 on failure | ||
| 224 | */ | ||
| 225 | |||
| 226 | int net_assignaddr (int sd, char *sourceip, unsigned short int sourceport); | ||
| 227 | |||
| 228 | |||
| 229 | /* net_connect | ||
| 230 | * | ||
| 231 | * connect to the given `server' and `port' with a max timeout of `sec'. | ||
| 232 | * initialize the sockaddr_in struct `cs' correctly (ipv4), accept any | ||
| 233 | * ip "123.123.123.123" or hostname "localhost", "www.yahoo.de" as hostname. | ||
| 234 | * create a new socket and return either -1 if failed or | ||
| 235 | * the connected socket if connection has been established within the | ||
| 236 | * timeout limit. | ||
| 237 | * | ||
| 238 | * the routine is still IPv4 biased :-/ | ||
| 239 | * with `sourceip'/`sourceportÄ you MAY specify the source IP and source port | ||
| 240 | * to use for the connection, but you can set the ip or port to NULL/0, | ||
| 241 | * to choose the default IP and an ephemeral port. this was added later in | ||
| 242 | * this library, so please update your sources. | ||
| 243 | * | ||
| 244 | * return -1 on failure | ||
| 245 | * return socket if success | ||
| 246 | */ | ||
| 247 | |||
| 248 | int net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, char *sourceip, | ||
| 249 | unsigned short int sourceport, int sec); | ||
| 250 | |||
| 251 | |||
| 252 | /* net_rtimeout | ||
| 253 | * | ||
| 254 | * waits max `sec' seconds for fd to become readable | ||
| 255 | * | ||
| 256 | * return -1 on error (errno set) | ||
| 257 | * return 1 on readability | ||
| 258 | */ | ||
| 259 | |||
| 260 | int net_rtimeout (int fd, int sec); | ||
| 261 | |||
| 262 | |||
| 263 | /* net_rbuf | ||
| 264 | * | ||
| 265 | * allocate memory and read socket data to `dst' until the connection | ||
| 266 | * gets closed. | ||
| 267 | * | ||
| 268 | * return n if success (n = number of bytes read) | ||
| 269 | * return -1 if failed | ||
| 270 | */ | ||
| 271 | |||
| 272 | long int net_rbuf (int fd, char **dst); | ||
| 273 | #define NET_BSIZE 4096 /* blocksize for pre-allocation */ | ||
| 274 | |||
| 275 | |||
| 276 | /* net_rbuft | ||
| 277 | * | ||
| 278 | * read `dsize' bytes into `dst' from `fd', with timeout | ||
| 279 | * | ||
| 280 | * return 1 on success | ||
| 281 | * return -1 on failure | ||
| 282 | */ | ||
| 283 | int net_rbuft (int fd, char *dst, unsigned long int dsize); | ||
| 284 | |||
| 285 | |||
| 286 | /* net_rlinet | ||
| 287 | * | ||
| 288 | * read a line from socket descriptor with timeout to buffer | ||
| 289 | * if sec = 0, then only a continuous stream of data is required, not | ||
| 290 | * an overall timeout. | ||
| 291 | * | ||
| 292 | * return -1 on timeout | ||
| 293 | * return 0 on connection close | ||
| 294 | * return length of readen line (including '\n') | ||
| 295 | * | ||
| 296 | * net_rlineta | ||
| 297 | * same as net_rlinet, but allocs the memory itself | ||
| 298 | */ | ||
| 299 | |||
| 300 | int net_rlineta (int fd, char **buf, int sec); | ||
| 301 | int net_rlinet (int fd, char *buf, int bufsize, int sec); | ||
| 302 | |||
| 303 | |||
| 304 | /* net_tline | ||
| 305 | * | ||
| 306 | * return length if string `buf' with a maximum length of `bufsize' | ||
| 307 | * contains '\n' | ||
| 308 | * | ||
| 309 | * return -1 if no '\n' in string | ||
| 310 | */ | ||
| 311 | |||
| 312 | int net_tline (char *buf, int bufsize); | ||
| 313 | |||
| 314 | |||
| 315 | /* net_write | ||
| 316 | * | ||
| 317 | * print a formatted string to a socket, see syntax of printf | ||
| 318 | * | ||
| 319 | * return in any case | ||
| 320 | */ | ||
| 321 | |||
| 322 | void net_write (int fd, const char *str, ...); | ||
| 323 | |||
| 324 | |||
| 325 | /* net_printip | ||
| 326 | * | ||
| 327 | * print an IP address stored in the struct in_addr pointed to by `ia' to a | ||
| 328 | * string `str' with a maximum length of `len'. | ||
| 329 | * | ||
| 330 | * return 0 on success | ||
| 331 | *Üreturn 1 on failure | ||
| 332 | * | ||
| 333 | * net_printipa behaves the same way, except it allocates memory and let | ||
| 334 | * `*str' point to the string | ||
| 335 | */ | ||
| 336 | |||
| 337 | int net_printip (struct in_addr *ia, char *str, size_t len); | ||
| 338 | int net_printipa (struct in_addr *ia, char **str); | ||
| 339 | |||
| 340 | |||
| 341 | #endif | ||
| 342 | |||
diff --git a/other/zylyx/src/proxy.c b/other/zylyx/src/proxy.c new file mode 100644 index 0000000..5f2fde1 --- /dev/null +++ b/other/zylyx/src/proxy.c | |||
| @@ -0,0 +1,206 @@ | |||
| 1 | /* zylyx - file find | ||
| 2 | * | ||
| 3 | * proxy routines | ||
| 4 | * | ||
| 5 | * by team teso | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <unistd.h> | ||
| 9 | #include <pthread.h> | ||
| 10 | #include <semaphore.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include <stdlib.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include "common.h" | ||
| 15 | #include "network.h" | ||
| 16 | #include "screen.h" | ||
| 17 | #include "proxy.h" | ||
| 18 | #include "zylyx.h" | ||
| 19 | |||
| 20 | |||
| 21 | void | ||
| 22 | prx_fire (proxy *mp, result *result_r, pthread_mutex_t *result_m, | ||
| 23 | sem_t *permit_action, sem_t *client_action) | ||
| 24 | { | ||
| 25 | pthread_t tid; | ||
| 26 | scan_t *new = xcalloc (1, sizeof (scan_t)); | ||
| 27 | int n; | ||
| 28 | |||
| 29 | new->proxy = mp; | ||
| 30 | new->result_r = result_r; | ||
| 31 | new->result_m = result_m; | ||
| 32 | new->permit_action = permit_action; | ||
| 33 | new->client_action = client_action; | ||
| 34 | |||
| 35 | n = pthread_create (&tid, NULL, (void *) prx_scan, (void *) new); | ||
| 36 | |||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | int | ||
| 42 | prx_findfile (scan_t *sc) | ||
| 43 | { | ||
| 44 | int n, m, linecount; | ||
| 45 | int prx_fd; | ||
| 46 | struct sockaddr_in csa; | ||
| 47 | char *readline; | ||
| 48 | |||
| 49 | prx_fd = net_connect (&csa, sc->proxy->host, sc->proxy->port, NULL, 0, 20); | ||
| 50 | if (prx_fd == -1) { | ||
| 51 | scr_rprint (sc->proxy->x, sc->proxy->y, ":04-"); | ||
| 52 | return (-1); | ||
| 53 | } | ||
| 54 | |||
| 55 | scr_rprint (sc->proxy->x, sc->proxy->y, ":02c"); | ||
| 56 | net_write (prx_fd, "GET %s HTTP/1.0\n\n", sc->proxy->file); | ||
| 57 | |||
| 58 | scr_rprint (sc->proxy->x, sc->proxy->y, ":02g"); | ||
| 59 | |||
| 60 | for (linecount = 0 ; | ||
| 61 | (((n = net_rlineta (prx_fd, &readline, 30)) > 0) && | ||
| 62 | linecount < 10) ; | ||
| 63 | linecount++) | ||
| 64 | { | ||
| 65 | int p; | ||
| 66 | |||
| 67 | scr_rprint (sc->proxy->x, sc->proxy->y, ":02r"); | ||
| 68 | |||
| 69 | m = sscanf (readline, "HTTP/1.0 %d", &p); | ||
| 70 | free (readline); | ||
| 71 | if (m != 1) { | ||
| 72 | scr_rprint (sc->proxy->x, sc->proxy->y, ":03j"); | ||
| 73 | } else if (p < 200 || p >= 300) { | ||
| 74 | scr_rprint (sc->proxy->x, sc->proxy->y, ":04f"); | ||
| 75 | close (prx_fd); | ||
| 76 | } else { | ||
| 77 | close (prx_fd); | ||
| 78 | return (1); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | if (n <= 0) | ||
| 83 | scr_rprint (sc->proxy->x, sc->proxy->y, ":04t"); | ||
| 84 | return (0); | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | void * | ||
| 89 | prx_scan (scan_t *sc) | ||
| 90 | { | ||
| 91 | int n; | ||
| 92 | |||
| 93 | /* don't mess with the system resources, else we get stuck after like | ||
| 94 | * 1050's proxy fires =) | ||
| 95 | */ | ||
| 96 | pthread_detach (pthread_self ()); | ||
| 97 | scr_rprint (sc->proxy->x, sc->proxy->y, ":05o"); | ||
| 98 | |||
| 99 | /* scan the proxy for the file, oh yeah | ||
| 100 | */ | ||
| 101 | n = prx_findfile (sc); | ||
| 102 | |||
| 103 | /* post the result to the main thread using shared process memory | ||
| 104 | * and semaphores for activation | ||
| 105 | */ | ||
| 106 | sem_wait (sc->permit_action); | ||
| 107 | pthread_mutex_lock (sc->result_m); | ||
| 108 | switch (n) { | ||
| 109 | case (-1): | ||
| 110 | case (0): | ||
| 111 | /* file not found or connection / proxy error | ||
| 112 | */ | ||
| 113 | |||
| 114 | sc->result_r->found = 0; | ||
| 115 | break; | ||
| 116 | case (1): | ||
| 117 | /* file successfully found | ||
| 118 | */ | ||
| 119 | |||
| 120 | sc->result_r->found = 1; | ||
| 121 | sc->result_r->proxy_host = sc->proxy->host; | ||
| 122 | sc->result_r->proxy_port = sc->proxy->port; | ||
| 123 | sc->result_r->file = sc->proxy->file; | ||
| 124 | scr_rprint (sc->proxy->x, sc->proxy->y, ":01!"); | ||
| 125 | break; | ||
| 126 | |||
| 127 | default: | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | sem_post (sc->client_action); | ||
| 131 | pthread_mutex_unlock (sc->result_m); | ||
| 132 | |||
| 133 | free (sc); | ||
| 134 | |||
| 135 | pthread_exit (NULL); | ||
| 136 | |||
| 137 | return (NULL); /* gcc eat that ;*/ | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | proxy ** | ||
| 142 | prx_load (char *filename, int *pc) | ||
| 143 | { | ||
| 144 | FILE *fp; | ||
| 145 | proxy **pl; | ||
| 146 | long int n, c; | ||
| 147 | |||
| 148 | *pc = n = prx_count (filename); | ||
| 149 | pl = xcalloc (n + 2, sizeof (proxy *)); | ||
| 150 | pl[n] = NULL; /* EOA */ | ||
| 151 | |||
| 152 | fp = fopen (filename, "r"); | ||
| 153 | if (fp == NULL) | ||
| 154 | exit (EXIT_FAILURE); | ||
| 155 | |||
| 156 | for (c = 0; c < n; ++c) { | ||
| 157 | pl[c] = prx_read (fp); | ||
| 158 | if (pl[c] == NULL) { | ||
| 159 | c--; | ||
| 160 | n--; | ||
| 161 | } | ||
| 162 | // printf ("%s:%hu\n", pl[c]->host, pl[c]->port); | ||
| 163 | } | ||
| 164 | |||
| 165 | return (pl); | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | proxy * | ||
| 170 | prx_read (FILE *fp) | ||
| 171 | { | ||
| 172 | proxy *prx = xcalloc (1, sizeof (proxy)); | ||
| 173 | char buf[1024]; | ||
| 174 | int n; | ||
| 175 | |||
| 176 | fgets (buf, sizeof (buf) - 1, fp); | ||
| 177 | n = net_parseip (buf, &prx->host, &prx->port); | ||
| 178 | if (n == 0) { | ||
| 179 | free (prx); | ||
| 180 | prx = NULL; | ||
| 181 | } | ||
| 182 | |||
| 183 | return (prx); | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | long int | ||
| 188 | prx_count (char *filename) | ||
| 189 | { | ||
| 190 | FILE *fp; | ||
| 191 | long int n; | ||
| 192 | char buf[1024]; | ||
| 193 | |||
| 194 | fp = fopen (filename, "r"); | ||
| 195 | if (fp == NULL) | ||
| 196 | exit (EXIT_FAILURE); | ||
| 197 | |||
| 198 | for (n = 0; fgets (buf, sizeof (buf), fp) != NULL; ++n) | ||
| 199 | ; | ||
| 200 | |||
| 201 | fclose (fp); | ||
| 202 | |||
| 203 | return (n); | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
diff --git a/other/zylyx/src/proxy.h b/other/zylyx/src/proxy.h new file mode 100644 index 0000000..bfb26a8 --- /dev/null +++ b/other/zylyx/src/proxy.h | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | /* zylyx - file find | ||
| 2 | * | ||
| 3 | * proxy routines include file | ||
| 4 | * | ||
| 5 | * by team teso | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef _ZYL_PROXY_H | ||
| 9 | #define _ZYL_PROXY_H | ||
| 10 | |||
| 11 | #include <pthread.h> | ||
| 12 | #include <semaphore.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include "zylyx.h" | ||
| 15 | |||
| 16 | typedef struct scan_t { | ||
| 17 | proxy *proxy; | ||
| 18 | result *result_r; | ||
| 19 | pthread_mutex_t *result_m; | ||
| 20 | sem_t *permit_action; | ||
| 21 | sem_t *client_action; | ||
| 22 | } scan_t; | ||
| 23 | |||
| 24 | proxy **prx_load (char *filename, int *pc); | ||
| 25 | void prx_fire (proxy *mp, result *result_r, pthread_mutex_t *result_m, | ||
| 26 | sem_t *permit_action, sem_t *client_action); | ||
| 27 | void *prx_scan (scan_t *sc); | ||
| 28 | proxy *prx_read (FILE *fp); | ||
| 29 | long int prx_count (char *filename); | ||
| 30 | |||
| 31 | #endif | ||
| 32 | |||
diff --git a/other/zylyx/src/screen.c b/other/zylyx/src/screen.c new file mode 100644 index 0000000..a4c707c --- /dev/null +++ b/other/zylyx/src/screen.c | |||
| @@ -0,0 +1,311 @@ | |||
| 1 | /* zylyx - file find | ||
| 2 | * | ||
| 3 | * screen and output module | ||
| 4 | * | ||
| 5 | * by team teso | ||
| 6 | */ | ||
| 7 | |||
| 8 | #define _ZYL_SCR_MAIN | ||
| 9 | |||
| 10 | #include <pthread.h> | ||
| 11 | #include <stdarg.h> | ||
| 12 | #include <stdlib.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <string.h> | ||
| 15 | #include <slang.h> | ||
| 16 | #include "screen.h" | ||
| 17 | #include "zylyx.h" | ||
| 18 | |||
| 19 | |||
| 20 | pthread_mutex_t screen_mutex; | ||
| 21 | |||
| 22 | void | ||
| 23 | scr_init (void) | ||
| 24 | { | ||
| 25 | int n; | ||
| 26 | |||
| 27 | pthread_mutex_init (&screen_mutex, NULL); | ||
| 28 | SLtt_get_terminfo (); | ||
| 29 | SLang_init_tty (-1, 0, 0); | ||
| 30 | n = SLsmg_init_smg (); | ||
| 31 | if (n == -1) { | ||
| 32 | fprintf (stderr, "SLsmg_init_smg failed\n"); | ||
| 33 | exit (EXIT_FAILURE); | ||
| 34 | } | ||
| 35 | SLsmg_cls (); | ||
| 36 | scr_init_col (); | ||
| 37 | SLsmg_refresh (); | ||
| 38 | |||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | void | ||
| 44 | scr_init_col (void) | ||
| 45 | { | ||
| 46 | /* give us some sneezy colors, oh yeah | ||
| 47 | */ | ||
| 48 | |||
| 49 | SLtt_set_color (COL_SPICY, NULL, "white", "black"); | ||
| 50 | SLtt_set_color (COL_MSPICY, NULL, "brightblue", "black"); | ||
| 51 | SLtt_set_color (COL_LSPICY, NULL, "blue", "black"); | ||
| 52 | SLtt_set_color (COL_TEXT, NULL, "gray", "black"); | ||
| 53 | SLtt_set_color (COL_STEXT, NULL, "brightgreen", "black"); | ||
| 54 | SLtt_set_color (COL_PRX_INACT, NULL, "green", "black"); | ||
| 55 | |||
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | void | ||
| 61 | scr_prg_init (void) | ||
| 62 | { | ||
| 63 | scr_build_box (0, 0, SLtt_Screen_Cols - 1, 6); | ||
| 64 | scr_build_box (0, 6, SLtt_Screen_Cols - 1, SLtt_Screen_Rows - 1); | ||
| 65 | |||
| 66 | scr_banner (); | ||
| 67 | |||
| 68 | SLsmg_refresh (); | ||
| 69 | |||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | void | ||
| 75 | scr_banner (void) | ||
| 76 | { | ||
| 77 | scr_cprint (2, 1, ":05zylyx v"VERSION":01 - :05file find:01 - :02"AUTHORS"\n"); | ||
| 78 | |||
| 79 | scr_cprint (2, 3, ":05o :02opening connection :02c connected :04- :02connection failed\n"); | ||
| 80 | scr_cprint (2, 4, ":02g :02try to get file :02r receiving :03j :02junk :04f :02file not found\n"); | ||
| 81 | scr_cprint (2, 5, ":04t :02timeouted :01! :02file found\n"); | ||
| 82 | |||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | int | ||
| 88 | scr_rprintf (int x, int y, const char *str, ...) | ||
| 89 | { | ||
| 90 | char tmp[1025]; | ||
| 91 | va_list vl; | ||
| 92 | int i; | ||
| 93 | |||
| 94 | va_start (vl, str); | ||
| 95 | memset (tmp, '\0', sizeof (tmp)); | ||
| 96 | i = vsnprintf (tmp, sizeof (tmp) - 1, str, vl); | ||
| 97 | va_end (vl); | ||
| 98 | |||
| 99 | scr_rprint (x, y, tmp); | ||
| 100 | SLsmg_refresh (); | ||
| 101 | |||
| 102 | return (i); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | void | ||
| 107 | scr_rprint (int x, int y, char *str) | ||
| 108 | { | ||
| 109 | scr_cprint (x, y, str); | ||
| 110 | SLsmg_refresh (); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | void | ||
| 115 | scr_cprint (int x, int y, char *str) | ||
| 116 | { | ||
| 117 | pthread_mutex_lock (&screen_mutex); | ||
| 118 | SLsmg_gotorc (y, x); | ||
| 119 | scr_print (str); | ||
| 120 | pthread_mutex_unlock (&screen_mutex); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | void | ||
| 125 | scr_print (char *str1) | ||
| 126 | { | ||
| 127 | char *str = strdup (str1); | ||
| 128 | char *prc = str; /* process pointer */ | ||
| 129 | char *print_str; | ||
| 130 | int color; | ||
| 131 | |||
| 132 | if (str[0] == ':') { | ||
| 133 | if (sscanf (str + 1, "%d", &color) != 1) | ||
| 134 | goto p_fail; | ||
| 135 | SLsmg_set_color (color); | ||
| 136 | prc += 3; | ||
| 137 | } | ||
| 138 | |||
| 139 | while ((print_str = strsep (&prc, ":")) != NULL) { | ||
| 140 | SLsmg_write_string (print_str); | ||
| 141 | if (prc == NULL) | ||
| 142 | goto p_fail; | ||
| 143 | if (sscanf (prc, "%d", &color) != 1) | ||
| 144 | goto p_fail; | ||
| 145 | SLsmg_set_color (color); | ||
| 146 | prc += 2; | ||
| 147 | } | ||
| 148 | |||
| 149 | p_fail: | ||
| 150 | free (str); | ||
| 151 | |||
| 152 | return; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | void | ||
| 157 | scr_build_box (int x1, int y1, int x2, int y2) | ||
| 158 | { | ||
| 159 | pthread_mutex_lock (&screen_mutex); | ||
| 160 | |||
| 161 | scr_build_sline_v (x1, y1, ((y2 - y1) / 2) + y1, '+', '|', '+', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 162 | scr_build_sline_v (x2, y1, ((y2 - y1) / 2) + y1, '+', '|', '+', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 163 | scr_build_sline_h (y1, x1 + 1, ((x2 - x1 - 2) / 2) + x1, '-', '-', '-', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 164 | scr_build_sline_h (y2, x1 + 1, ((x2 - x1 - 2) / 2) + x1, '-', '-', '-', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 165 | |||
| 166 | scr_build_sline_v (x1, y2, ((y2 - y1) / 2) + y1, '|', '|', '+', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 167 | scr_build_sline_v (x2, y2, ((y2 - y1) / 2) + y1, '|', '|', '+', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 168 | scr_build_sline_h (y1, x2 - 1, ((x2 - x1) / 2) + x1, '-', '-', '-', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 169 | scr_build_sline_h (y2, x2 - 1, ((x2 - x1) / 2) + x1, '-', '-', '-', COL_SPICY, COL_MSPICY, COL_LSPICY); | ||
| 170 | |||
| 171 | pthread_mutex_unlock (&screen_mutex); | ||
| 172 | |||
| 173 | return; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | void | ||
| 178 | scr_build_sline_v (int x, int y1, int y2, char start, char fill, char end, | ||
| 179 | int objhigh, int objmed, int objlow) | ||
| 180 | { | ||
| 181 | int ly2, ly3; | ||
| 182 | |||
| 183 | if (y1 > y2) { | ||
| 184 | int obj_tmp; | ||
| 185 | |||
| 186 | y2 ^= y1; | ||
| 187 | y1 ^= y2; | ||
| 188 | y2 ^= y1; | ||
| 189 | |||
| 190 | obj_tmp = objhigh; | ||
| 191 | objhigh = objlow; | ||
| 192 | objlow = objmed; | ||
| 193 | objmed = obj_tmp; | ||
| 194 | |||
| 195 | ly2 = ((y2 - y1) - ((y2 - y1) / 8)) + y1; | ||
| 196 | ly3 = ((y2 - y1) - ((y2 - y1) / 4)) + y1; | ||
| 197 | } else { | ||
| 198 | ly2 = ((y2 - y1) / 8) + y1; | ||
| 199 | ly3 = ((y2 - y1) / 4) + y1; | ||
| 200 | } | ||
| 201 | |||
| 202 | SLsmg_set_color (objhigh); | ||
| 203 | SLsmg_gotorc (y1, x); | ||
| 204 | SLsmg_write_char (start); | ||
| 205 | |||
| 206 | for (++y1 ; y1 < y2 ; ++y1) { | ||
| 207 | if (y1 == ly2) { | ||
| 208 | SLsmg_set_color (objmed); | ||
| 209 | } else if (y1 == ly3) { | ||
| 210 | SLsmg_set_color (objlow); | ||
| 211 | } | ||
| 212 | SLsmg_gotorc (y1, x); | ||
| 213 | SLsmg_write_char (fill); | ||
| 214 | } | ||
| 215 | SLsmg_gotorc (y2, x); | ||
| 216 | SLsmg_write_char (end); | ||
| 217 | |||
| 218 | return; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | void | ||
| 223 | scr_build_sline_h (int y, int x1, int x2, char start, char fill, char end, | ||
| 224 | int objhigh, int objmed, int objlow) | ||
| 225 | { | ||
| 226 | int lx2, lx3; | ||
| 227 | |||
| 228 | if (x1 > x2) { | ||
| 229 | int obj_tmp; | ||
| 230 | |||
| 231 | x2 ^= x1; | ||
| 232 | x1 ^= x2; | ||
| 233 | x2 ^= x1; | ||
| 234 | obj_tmp = objhigh; | ||
| 235 | objhigh = objlow; | ||
| 236 | objlow = objmed; | ||
| 237 | objmed = obj_tmp; | ||
| 238 | lx2 = ((x2 - x1) - ((x2 - x1) / 8)) + x1; | ||
| 239 | lx3 = ((x2 - x1) - ((x2 - x1) / 4)) + x1; | ||
| 240 | } else { | ||
| 241 | lx2 = ((x2 - x1) / 8) + x1; | ||
| 242 | lx3 = ((x2 - x1) / 4) + x1; | ||
| 243 | } | ||
| 244 | |||
| 245 | SLsmg_set_color (objhigh); | ||
| 246 | SLsmg_gotorc (y, x1); | ||
| 247 | SLsmg_write_char (start); | ||
| 248 | |||
| 249 | for (++x1 ; x1 < x2 ; ++x1) { | ||
| 250 | if (x1 == lx2) { | ||
| 251 | SLsmg_set_color (objmed); | ||
| 252 | } else if (x1 == lx3) { | ||
| 253 | SLsmg_set_color (objlow); | ||
| 254 | } | ||
| 255 | SLsmg_gotorc (y, x1); | ||
| 256 | SLsmg_write_char (fill); | ||
| 257 | } | ||
| 258 | SLsmg_gotorc (y, x2); | ||
| 259 | SLsmg_write_char (end); | ||
| 260 | |||
| 261 | return; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | void | ||
| 266 | scr_build_line_v (int obj, int x, int y1, int y2, char start, char fill, char end) | ||
| 267 | { | ||
| 268 | SLsmg_set_color (obj); | ||
| 269 | SLsmg_gotorc (y1, x); | ||
| 270 | SLsmg_write_char (start); | ||
| 271 | |||
| 272 | for (++y1 ; y1 < y2 ; ++y1) { | ||
| 273 | SLsmg_gotorc (y1, x); | ||
| 274 | SLsmg_write_char (fill); | ||
| 275 | } | ||
| 276 | |||
| 277 | SLsmg_gotorc (y2, x); | ||
| 278 | SLsmg_write_char (end); | ||
| 279 | |||
| 280 | return; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | void | ||
| 285 | scr_build_line_h (int obj, int y, int x1, int x2, char start, char fill, char end) | ||
| 286 | { | ||
| 287 | SLsmg_set_color (obj); | ||
| 288 | SLsmg_gotorc (y, x1); | ||
| 289 | SLsmg_write_char (start); | ||
| 290 | |||
| 291 | for (++x1 ; x1 < x2 ; ++x1) { | ||
| 292 | SLsmg_gotorc (y, x1); | ||
| 293 | SLsmg_write_char (fill); | ||
| 294 | } | ||
| 295 | |||
| 296 | SLsmg_gotorc (y, x2); | ||
| 297 | SLsmg_write_char (end); | ||
| 298 | |||
| 299 | return; | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | void | ||
| 304 | scr_exit (void) | ||
| 305 | { | ||
| 306 | SLsmg_reset_smg (); | ||
| 307 | SLang_reset_tty (); | ||
| 308 | |||
| 309 | return; | ||
| 310 | } | ||
| 311 | |||
diff --git a/other/zylyx/src/screen.h b/other/zylyx/src/screen.h new file mode 100644 index 0000000..2af9c5b --- /dev/null +++ b/other/zylyx/src/screen.h | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | /* zylyx - file find | ||
| 2 | * | ||
| 3 | * screen and output routines | ||
| 4 | * header file | ||
| 5 | * | ||
| 6 | * by team teso | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _ZYL_SCREEN | ||
| 10 | #define _ZYL_SCREEN | ||
| 11 | |||
| 12 | #include <pthread.h> | ||
| 13 | |||
| 14 | #ifndef _ZYL_SCR_MAIN | ||
| 15 | extern pthread_mutex_t screen_mutex; | ||
| 16 | #endif | ||
| 17 | |||
| 18 | |||
| 19 | #define COL_SPICY 0x01 | ||
| 20 | #define COL_MSPICY 0x02 | ||
| 21 | #define COL_LSPICY 0x03 | ||
| 22 | #define COL_TEXT 0x04 | ||
| 23 | #define COL_STEXT 0x05 | ||
| 24 | #define COL_PRX_INACT 0x10 | ||
| 25 | |||
| 26 | void scr_init (void); | ||
| 27 | void scr_prg_init (void); | ||
| 28 | void scr_init_col (void); | ||
| 29 | void scr_banner (void); | ||
| 30 | int scr_rprintf (int x, int y, const char *str, ...); | ||
| 31 | void scr_rprint (int x, int y, char *str); | ||
| 32 | void scr_cprint (int x, int y, char *str); | ||
| 33 | void scr_print (char *str1); | ||
| 34 | void scr_build_box (int x1, int y1, int x2, int y2); | ||
| 35 | void scr_build_sline_v (int x, int y1, int y2, char start, char fill, char end, | ||
| 36 | int objhigh, int objmed, int objlow); | ||
| 37 | void scr_build_sline_h (int y, int x1, int x2, char start, char fill, char end, | ||
| 38 | int objhigh, int objmed, int objlow); | ||
| 39 | void scr_build_line_v (int obj, int x, int y1, int y2, char start, char fill, char end); | ||
| 40 | void scr_build_line_h (int obj, int y, int x1, int x2, char start, char fill, char end); | ||
| 41 | void scr_exit (void); | ||
| 42 | void scr_init_col (void); | ||
| 43 | |||
| 44 | #endif | ||
| 45 | |||
diff --git a/other/zylyx/src/zylyx.c b/other/zylyx/src/zylyx.c new file mode 100644 index 0000000..5196c0f --- /dev/null +++ b/other/zylyx/src/zylyx.c | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | /* zylyx - file find ;-) | ||
| 2 | * | ||
| 3 | * by team teso | ||
| 4 | */ | ||
| 5 | |||
| 6 | #include <sys/time.h> | ||
| 7 | #include <unistd.h> | ||
| 8 | #include <pthread.h> | ||
| 9 | #include <semaphore.h> | ||
| 10 | #include <stdlib.h> | ||
| 11 | #include <stdio.h> | ||
| 12 | #include <slang.h> | ||
| 13 | #include "common.h" | ||
| 14 | #include "screen.h" | ||
| 15 | #include "proxy.h" | ||
| 16 | #include "zylyx.h" | ||
| 17 | |||
| 18 | #define MAX_PROC 16 | ||
| 19 | |||
| 20 | int win_y; | ||
| 21 | |||
| 22 | int | ||
| 23 | main (int argc, char **argv) | ||
| 24 | { | ||
| 25 | proxy **prx_list; | ||
| 26 | int proxy_count; | ||
| 27 | |||
| 28 | if (argc != 2) { | ||
| 29 | printf ("usage: %s <url>\n", argv[0]); | ||
| 30 | exit (EXIT_FAILURE); | ||
| 31 | } | ||
| 32 | |||
| 33 | scr_init (); | ||
| 34 | scr_prg_init (); | ||
| 35 | |||
| 36 | prx_list = prx_load ("proxy-list", &proxy_count); | ||
| 37 | if (prx_list == NULL) { | ||
| 38 | scr_exit (); | ||
| 39 | exit (EXIT_FAILURE); | ||
| 40 | } | ||
| 41 | |||
| 42 | zyl_assign_prx (prx_list); | ||
| 43 | zyl_main (prx_list, proxy_count, argv[1]); | ||
| 44 | |||
| 45 | scr_exit (); | ||
| 46 | |||
| 47 | exit (EXIT_SUCCESS); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | void | ||
| 52 | zyl_main (proxy **pl, int proxycount, char *file) | ||
| 53 | { | ||
| 54 | int n; | ||
| 55 | struct timeval last_conn = { 0, 0 }; | ||
| 56 | sem_t permit_action, /* permit a client response */ | ||
| 57 | client_action; /* client responded */ | ||
| 58 | pthread_mutex_t subcount_m; /* subcounter mutex */ | ||
| 59 | int subcount = 0; | ||
| 60 | int proxy_ptr = 0; | ||
| 61 | pthread_mutex_t result_m; /* result data is locked hehe */ | ||
| 62 | result result_r; /* result data =) */ | ||
| 63 | |||
| 64 | sem_init (&permit_action, 0, 0); | ||
| 65 | sem_init (&client_action, 0, 0); | ||
| 66 | pthread_mutex_init (&subcount_m, NULL); | ||
| 67 | pthread_mutex_init (&result_m, NULL); | ||
| 68 | |||
| 69 | sem_post (&permit_action); | ||
| 70 | |||
| 71 | /* fire clients and take events (event loop) | ||
| 72 | */ | ||
| 73 | |||
| 74 | while (proxy_ptr < proxycount) { | ||
| 75 | pthread_mutex_lock (&subcount_m); | ||
| 76 | if (subcount < MAX_PROC) { | ||
| 77 | unsigned long int tp; | ||
| 78 | |||
| 79 | pl[proxy_ptr]->file = file; | ||
| 80 | |||
| 81 | /* anti flood mechanism :) | ||
| 82 | */ | ||
| 83 | tp = t_passed (&last_conn); | ||
| 84 | if (tp > 0 && tp < 500000) { | ||
| 85 | /* race condition here (find it and you'll get a hug ;) | ||
| 86 | */ | ||
| 87 | usleep (500000 - tp); | ||
| 88 | } | ||
| 89 | gettimeofday (&last_conn, NULL); | ||
| 90 | prx_fire (pl[proxy_ptr++], &result_r, &result_m, &permit_action, &client_action); | ||
| 91 | subcount++; | ||
| 92 | } | ||
| 93 | pthread_mutex_unlock (&subcount_m); | ||
| 94 | |||
| 95 | #ifdef IPC_DEBUG | ||
| 96 | printf ("zylyx.c:%d # sem_trywait (&client_action) = %d\n", __LINE__, sem_trywait (&client_action)); | ||
| 97 | printf ("zylyx.c:%d # subcount = %d\n", __LINE__, subcount); | ||
| 98 | #endif | ||
| 99 | |||
| 100 | /* if we cannot fire out new clients because we reached the maximum number, | ||
| 101 | * or if a client wants our attention we enter the result processing | ||
| 102 | */ | ||
| 103 | |||
| 104 | n = 1; | ||
| 105 | if (subcount >= MAX_PROC || | ||
| 106 | ((n = sem_trywait (&client_action)) == 0)) | ||
| 107 | { | ||
| 108 | |||
| 109 | /* wait for client action (queued) | ||
| 110 | */ | ||
| 111 | if (n == 1) | ||
| 112 | sem_wait (&client_action); | ||
| 113 | |||
| 114 | /* lock result data | ||
| 115 | */ | ||
| 116 | pthread_mutex_lock (&result_m); | ||
| 117 | |||
| 118 | /* only be verbose if the file was found | ||
| 119 | */ | ||
| 120 | if (result_r.found == 1) { | ||
| 121 | |||
| 122 | /* yeah, zylyx found the file, now tell the user <g> | ||
| 123 | */ | ||
| 124 | |||
| 125 | scr_rprint (1, win_y, ":01! - "); | ||
| 126 | scr_rprintf (5, win_y++, "%s %hu\n", | ||
| 127 | result_r.proxy_host, result_r.proxy_port); | ||
| 128 | } | ||
| 129 | |||
| 130 | /* unlock the result data for the clients to modify, | ||
| 131 | * then post the permission to act ;-) | ||
| 132 | */ | ||
| 133 | pthread_mutex_unlock (&result_m); | ||
| 134 | sem_post (&permit_action); | ||
| 135 | |||
| 136 | subcount--; /* one client quitted */ | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | while (subcount > 0) { | ||
| 141 | sem_wait (&client_action); | ||
| 142 | pthread_mutex_lock (&result_m); | ||
| 143 | |||
| 144 | if (result_r.found == 1) { | ||
| 145 | /* yeah, zylyx found the file <g> | ||
| 146 | */ | ||
| 147 | |||
| 148 | scr_rprint (1, win_y, ":01! - "); | ||
| 149 | scr_rprintf (5, win_y++, "%s %hu\n", | ||
| 150 | result_r.proxy_host, result_r.proxy_port); | ||
| 151 | } | ||
| 152 | pthread_mutex_unlock (&result_m); | ||
| 153 | sem_post (&permit_action); | ||
| 154 | subcount--; /* one client quitted */ | ||
| 155 | } | ||
| 156 | |||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | void | ||
| 162 | zyl_assign_prx (proxy **pl) | ||
| 163 | { | ||
| 164 | int x, y; | ||
| 165 | int n = 0; | ||
| 166 | |||
| 167 | x = 1; | ||
| 168 | y = 7; | ||
| 169 | |||
| 170 | for (y = 7; y < SLtt_Screen_Rows - 2; ++y) { | ||
| 171 | for (x = 1; x < SLtt_Screen_Cols - 1; ++x) { | ||
| 172 | if (pl[n] == NULL) { | ||
| 173 | scr_build_box (0, 6, SLtt_Screen_Cols - 1, y + 1); | ||
| 174 | scr_build_box (0, y + 1, SLtt_Screen_Cols - 1, SLtt_Screen_Rows - 1); | ||
| 175 | win_y = y + 2; | ||
| 176 | SLsmg_refresh (); | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | pl[n]->x = x; | ||
| 180 | pl[n]->y = y; | ||
| 181 | scr_rprint (x, y, ":16."); | ||
| 182 | n++; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | return; | ||
| 187 | } | ||
diff --git a/other/zylyx/src/zylyx.h b/other/zylyx/src/zylyx.h new file mode 100644 index 0000000..bd2c30a --- /dev/null +++ b/other/zylyx/src/zylyx.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* zylyx - file find | ||
| 2 | * | ||
| 3 | * by scut of teso | ||
| 4 | */ | ||
| 5 | |||
| 6 | #ifndef _ZYL_ZYLYX_H | ||
| 7 | #define _ZYL_ZYLYX_H | ||
| 8 | |||
| 9 | #define VERSION "0.1.1" | ||
| 10 | #define AUTHORS "scut of teso" | ||
| 11 | |||
| 12 | typedef struct proxy { | ||
| 13 | char *file; | ||
| 14 | char *host; | ||
| 15 | unsigned short int port; | ||
| 16 | int x, y; /* field with proxy info :) */ | ||
| 17 | } proxy; | ||
| 18 | |||
| 19 | typedef struct result { | ||
| 20 | int found; | ||
| 21 | char *proxy_host; | ||
| 22 | unsigned short int proxy_port; | ||
| 23 | char *file; | ||
| 24 | } result; | ||
| 25 | |||
| 26 | void zyl_assign_prx (proxy **pl); | ||
| 27 | void zyl_main (proxy **pl, int proxycount, char *file); | ||
| 28 | |||
| 29 | #endif | ||
| 30 | |||
