Advertisement
Guest User

Untitled

a guest
Nov 30th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.49 KB | None | 0 0
  1. #ifndef WIN32
  2.     #define _GNU_SOURCE
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9.  
  10. #ifdef WIN32
  11.     #include <winsock.h>
  12.     #include <windows.h>
  13.     typedef int socklen_t;
  14. #else
  15.     #include <unistd.h>
  16.     #include <sys/socket.h>
  17.     #include <sys/types.h>
  18.     #include <arpa/inet.h>
  19.     #include <netinet/in.h>
  20.     #include <netdb.h>
  21.     #include <dlfcn.h>  // -ldl
  22.     #define mysendto sendto
  23.     static ssize_t (*old_sendto)(int s, void *buf, size_t len, int flags, struct sockaddr *to, int tolen) = NULL;
  24. #endif
  25.  
  26.  
  27.  
  28. #define VER         "0.1"
  29. #define PLAYERS     6
  30.  
  31.  
  32.  
  33. u_char *mymemmem(u_char *ptr, int offset, u_char *value, int valsz, int num) {
  34.     int     i;
  35.  
  36.     if(valsz < 0) valsz = strlen(value);
  37.     for(i = offset; i <= (num - valsz); i++) {
  38.         if(!memcmp(ptr + i, value, valsz)) return(ptr + i + valsz);
  39.     }
  40.     return(NULL);
  41. }
  42.  
  43.  
  44.  
  45. int myrand(void) {
  46.     static int  rnd = 0;
  47.     if(!rnd) rnd = time(NULL);
  48.     rnd = ((rnd * 0x343FD) + 0x269EC3) >> 1;
  49.     return(rnd & 0x7fffffff);
  50. }
  51.  
  52.  
  53.  
  54. ssize_t __cdecl mysendto(int s, u_char **retbuf, int len, int flags, const struct sockaddr *to, int tolen) {
  55.     int     i,
  56.             chr  = '\\',    // default delimiter
  57.             num  = 0,       // 0 players in the reply
  58.             lame = 0;
  59.     u_char  *new,           // it's automatically freed by proxocket
  60.             *p;
  61.     u_char  *buf = *retbuf; // do NOT touch this
  62.  
  63. #ifndef WIN32
  64.     if(!old_sendto) old_sendto = dlsym(RTLD_NEXT, "sendto");
  65.     len = old_sendto(s, buf, len, flags, to, tolen);
  66. #endif
  67.  
  68.     if(len < 30) return(len);
  69.  
  70.     // Quake 3 getstatus
  71.     if(!memcmp(buf, "\xff\xff\xff\xff" "statusResponse", 18)) {
  72.         p = mymemmem(buf, 18 + 1, "\n", 1, len);
  73.         if(p) {
  74.             for(i = p - buf; i < len; i++) {
  75.                 if(p[i] == '\n') num++;
  76.             }
  77.         }
  78.         if(num >= PLAYERS) return(len);
  79.  
  80.         new = malloc(len + 1024);
  81.         memcpy(new, buf, len);
  82.         buf = new;
  83.  
  84.         p = buf + len;
  85.         for(; num < PLAYERS; num++) {
  86.             p += sprintf(p, "%d %d \"BOssMan\"\n", myrand() % 50, num);
  87.         }
  88.         len = p - buf;
  89.  
  90.     } else {
  91.         // Quake 3 getinfo
  92.         if(!memcmp(buf, "\xff\xff\xff\xff" "infoResponse", 16)) {
  93.             p = mymemmem(buf, 16, "\\clients\\", -1, len);
  94.             if(!p) p = mymemmem(buf, 16, "\\players\\", -1, len);
  95.             if(!p) p = mymemmem(buf, 16, "\\privateClients\\", -1, len);
  96.  
  97.         // Gamespy 2 and 3
  98.         } else if(!memcmp(buf + 5, "hostname\0", 9)) {
  99.             p = mymemmem(buf, 5, "\0""numplayers""\0", 1+10+1, len);
  100.             chr = 0;
  101.  
  102.         // Half-Life
  103.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "infostringresponse", 22)) {
  104.             p = mymemmem(buf, 14, "\\players\\", -1, len);
  105.  
  106.         // Half-Life (m)
  107.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "m", 5)) {
  108.             p = buf + 5;
  109.             for(i = 0; i < 5; i++) {
  110.                 p += strlen(p) + 1;
  111.                 if(p >= (buf + len)) return(len);
  112.             }
  113.             // p[0] is the number of players, trick
  114.             chr = p[1]; // trick
  115.  
  116.         // Source (I)
  117.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "I", 5)) {
  118.             p = buf + 5 + 1;
  119.             for(i = 0; i < 6; i++) {
  120.                 p += strlen(p) + 1;
  121.                 if(p >= (buf + len)) return(len);
  122.             }
  123.             // p[0] is the number of players, trick
  124.             chr = p[1]; // trick
  125.  
  126.         /*
  127.         // Source (E): no players info in "rules"
  128.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "E", 5)) {
  129.             p = mymemmem(buf, 5, "\0""clients""\0", 1+7+1, len);
  130.             if(!p) p = mymemmem(buf, 5, "\0""players""\0", 1+7+1, len);
  131.             chr = 0;
  132.         */
  133.  
  134.         /*
  135.         // Doom 3: not supported
  136.         } else if(!memcmp(buf, "\xff\xff" "infoResponse", 14)) {
  137.         */
  138.  
  139.         // Gamespy 1
  140.         } else if(buf[0] == '\\') {
  141.             p = mymemmem(buf, 0, "\\numplayers\\", -1, len);
  142.  
  143.         // All seeying eye
  144.         } else if(!memcmp(buf, "EYE1", 4)) {
  145.             p = buf + 4;
  146.             for(i = 0; i < 7; i++) {
  147.                 if(!((p < (buf + len)) && (*p > 1))) return(len);
  148.                 p += *p;
  149.             }
  150.             p++;
  151.             // p[0] is the number of players, trick
  152.             chr = p[*p];    // trick
  153.  
  154.         } else {
  155.             return(len);
  156.         }
  157.         if(!p) return(len); // boring
  158.  
  159.         if(*p < '0') lame = 1;  // it's only a lame trick for non textual players (max 47)
  160.  
  161.         if(lame) {
  162.             num = *p;
  163.         } else {
  164.             num = atoi(p);
  165.         }
  166.         if((num < 0) || (num >= PLAYERS)) return(len);
  167.  
  168.         i = p - buf;
  169.         new = malloc(len + 16);
  170.         memcpy(new, buf, i);                // copy till the players parameter (included)
  171.         p = new + i;
  172.         if(lame) {
  173.             *p++ = PLAYERS;
  174.         } else {
  175.             p += sprintf(p, "%d", PLAYERS); // put the new one
  176.         }
  177.         while(buf[i] != chr) i++;           // skip the old value
  178.         memcpy(p, buf + i, len - i);        // copy the rest
  179.         p += (len - i);
  180.         buf = new;
  181.         len = p - buf;
  182.     }
  183.  
  184.     *retbuf = buf;  // do NOT touch this
  185.     return(len);
  186. }
  187.  
  188.  
  189.  
  190. #ifdef WIN32
  191. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
  192.     if(fdwReason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(hinstDLL);
  193.     return(TRUE);
  194. }
  195. #endif
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement