cielavenir

funopen/fopencookie test

Feb 7th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #ifdef __linux__
  2. #define _GNU_SOURCE
  3. #endif
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9.  
  10. int sread(void *s,char *p,int l){return recv(*(int*)s,p,l,0);}
  11. int swrite(void *s,const char *p,int l){return send(*(int*)s,p,l,0);}
  12. int sclose(void *s){return close(*(int*)s);}
  13. char* myfgets(char *b,int n,FILE *f){
  14.     char *r=fgets(b,n,f);
  15.     if(!r)return NULL;
  16.     if(b[strlen(b)-1]=='\n')b[strlen(b)-1]=0;
  17.     if(b[strlen(b)-1]=='\r')b[strlen(b)-1]=0;
  18.     return r;
  19. }
  20. #ifdef __linux__
  21. FILE *funopen(void *cookie,cookie_read_function_t *R,cookie_write_function_t *W,cookie_seek_function_t *S,cookie_close_function_t *C){
  22.     if(!R&&!W)return NULL;
  23.     cookie_io_functions_t io={R,W,S,C};
  24.     return fopencookie(cookie,R&&W?"r+b":R?"rb":"wb",io);
  25. }
  26. #endif
  27. char z[9999];
  28. int main(){
  29.     struct hostent *host = gethostbyname("prx-common-libraries.googlecode.com");
  30.     unsigned short port = 80;
  31.     int sock;
  32.     struct sockaddr_in addr;
  33.  
  34.     memset(&addr, 0, sizeof(addr));
  35.     addr.sin_port = htons(port);
  36.     addr.sin_family = host->h_addrtype;
  37.     addr.sin_addr.s_addr = *((in_addr_t*)host->h_addr_list[0]);
  38.     sock = socket(host->h_addrtype, SOCK_STREAM, 0);
  39.     connect(sock, (struct sockaddr *) &addr, sizeof(addr));
  40.  
  41.     FILE *f=funopen(&sock,sread,swrite,NULL,sclose);
  42.     fputs("GET /hg/genupdate/upload_script.src HTTP/1.1\r\n",f);
  43.     fputs("Host: prx-common-libraries.googlecode.com\r\n",f);
  44.     fputs("\r\n",f);
  45.     while(myfgets(z,9999,f)&&*z); //strip header
  46.     while(myfgets(z,9999,f)&&memcmp(z,"<GET> ",6));
  47.     puts(z);
  48.     fclose(f);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment