Advertisement
m4ly

FASTCGI FPM

Jun 9th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. /* Dawid Mocek */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <sys/un.h>
  10. #include <fcntl.h>      /* A(10) B(11) C(12) D(13) E(14) F(15) */
  11.  
  12. #define SOH 0x01
  13. #define STX 0x02
  14. #define ETX 0x03
  15. #define CHUNK_SIZE 8192
  16. #define PATH "/tmp/phpmailer.power.local.com.sock"
  17.  
  18. char header[] = { 0x01, 0x06, 0x00, 0x01, 0x02, 0x28, 0x00, 0x00 };
  19. char query[] = {
  20. 0x01, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  21. /*
  22.                                     ]     NULL  SOH   CR    _     /     u     s     r     /
  23. */
  24. 0x01, 0x04, 0x00, 0x01, 0x00, 0x5d, 0x03, 0x00, 0x01, 0x0d, 0x5f, 0x2f, 0x75, 0x73, 0x72, 0x2f,
  25.  
  26. // 0x01, 0x04, 0x00, 0x01, 0x00, 0x5d, 0x03, 0x00, 0x02, 0x0d, 0x5f, 0x2f, 0x75, 0x73, 0x72,
  27. /*
  28. b     i     n     /     p     e     r     l     SO    ETX    R    E      Q    E      S     T
  29. */
  30. 0x62, 0x69, 0x6e, 0x2f, 0x70, 0x65, 0x72, 0x6c, 0x0e, 0x03, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53,
  31. /*
  32. T     _     M     E     T     H     O     D     G     E     T     SI    BEL   S     C     R
  33. */
  34. 0x54, 0x5f, 0x4d, 0x45, 0x54, 0x48, 0x4f, 0x44, 0x47, 0x45, 0x54, 0x0f, 0x07, 0x53, 0x43, 0x52,
  35. /*
  36. I     P     T     _     F     I     L     E     N     A     M     E     /     s     t     a
  37. */
  38. 0x49, 0x50, 0x54, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x4e, 0x41, 0x4d, 0x45, 0x2f, 0x73, 0x74, 0x61,
  39. /*
  40. t     u     s     VT    BELL  S     C     R     I     P     T     _     N     A     M     E
  41. */
  42. 0x74, 0x75, 0x73, 0x0b, 0x07, 0x53, 0x43, 0x52, 0x49, 0x50, 0x54, 0x5f, 0x4e, 0x41, 0x4d, 0x45,
  43. /*
  44. /     s     t     a     t     u     s     FF    NULL  Q     U     E     R     Y     _     S
  45. */
  46. 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x0c, 0x00, 0x51, 0x55, 0x45, 0x52, 0x59, 0x5f, 0x53,
  47. /*
  48. T     R     I     N     G     NULL  NULL  NULL  SOH   EOT   NULL  SOH   NULL  NULL  NULL  NULL
  49. */
  50. 0x54, 0x52, 0x49, 0x4e, 0x47, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  51. /*
  52. SOH   ENQ   NULL  SOH   NULL  NULL  NULL  NULL
  53. */
  54. 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
  55. };
  56. /* R- T 16 znakow */
  57.  
  58. struct twobytes {
  59.     uint8_t cl1;
  60.     uint8_t cl0;
  61. } __attribute__ ((__packed__));
  62.  
  63. struct fcgi_record {
  64.     uint8_t version;
  65.     uint8_t type;
  66.     uint8_t req1;
  67.     uint8_t req0;
  68.     union {
  69.         uint16_t cl;
  70.         struct twobytes cl8;
  71.     };
  72.     uint8_t pad;
  73.     uint8_t reserved;
  74. } __attribute__ ((__packed__));
  75.  
  76. uint16_t fcgi_get_record(int fd, char *buf)
  77. {
  78.     struct fcgi_record fr;
  79.     uint16_t remains = 8;
  80.     char *ptr = (char *) &fr;
  81.     ssize_t len;
  82.  
  83.     while(remains) {
  84.         len = read(fd, ptr, remains);
  85.         printf("%s", ptr);
  86.         if( len <= 0) return 0;
  87.         remains -= len;
  88.         ptr += len;
  89.  
  90.     }
  91.     remains = ntohs(fr.cl) +  fr.pad;
  92.     ptr = buf;
  93.  
  94.     while(remains) {
  95.         len = read(fd, ptr, remains);
  96.         printf("%s", ptr);
  97.         if(len <= 0) return 0;
  98.         remains -= len;
  99.         ptr += len;
  100.     }
  101.  
  102.     if(fr.type != 6) return 0;
  103.     return ntohs(fr.cl);
  104. }
  105.  
  106. int main(void) {
  107.     int s, s2, t, len, size_read=0, total_size =0, size_recv=0;
  108.     size_t n;
  109.     int idx, new_len, cmp;
  110.     struct sockaddr_un remote;
  111.  
  112.     char *buf = NULL;
  113.     char *chunk = (char*)malloc(8192);
  114.  
  115.     if((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  116.         perror("socket()");
  117.         exit(1);
  118.     }
  119.  
  120.     printf("Trying to connect...\n");
  121.     memset(&remote, 0, sizeof(struct sockaddr_un));
  122.     remote.sun_family = AF_UNIX;
  123.     strncpy(remote.sun_path, PATH, sizeof(remote.sun_path)-1);
  124.  
  125.     // connection
  126.     if(connect(s, (struct sockaddr *)&remote, sizeof(struct sockaddr_un)) == -1) {
  127.         perror("connect()");
  128.         exit(1);
  129.     }
  130.  
  131.     printf("Connected\n");
  132.  
  133.     memset(chunk, 0, 8192);
  134.     send(s, query, sizeof(query), 0);
  135. //    uint16_t ret = fcgi_get_record(s, buf);
  136. //    printf("%s\n", buf);
  137.  
  138.     len = read(s, chunk, 8192);
  139.     if(len > 0) {
  140.         cmp = memcmp(chunk, header, sizeof header);
  141.         printf("%d\n", cmp);
  142.         for(t = 0; t < len; t++) {
  143.                 printf("%d = %#010x (%c)\n", t, chunk[t], chunk[t]);
  144.             }
  145.  
  146. /*      if(chunk[7] == NULL) {
  147.             new_len = len - 8;
  148.             buf = (char *)malloc(new_len + 1);
  149.             memset(buf, 0, new_len + 1);
  150.             memcpy(buf, chunk+8, new_len);
  151.             free(chunk);
  152.  
  153.             char *c = strchr(buf, SOH);
  154.             buf = realloc(buf, 5);
  155.             printf("%s\n", buf);
  156.             //new_len =  c - buf - 1 - 1;
  157.  
  158.             //printf("idx = %d\n", tmp1);
  159.             for(t = 0; t < new_len; t++) {
  160.                 printf("%d = %#010x (%c)\n", t, buf[t], buf[t]);
  161. //          }
  162.         }*/
  163.    }
  164.     /*
  165.     printf("LEN = %d\n", len);
  166.  
  167.     for(t = 0; t < len; t++) {
  168.         printf("%d = %#010x (%c)\n", t, chunk[t], chunk[t]);
  169.         }*/
  170.     close(s);
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement