Posted By

bcmoney on 12/10/10


Tagged


Versions (?)

proxy.c


 / Published in: C
 

Useful resources for cross-domain HTTP requests in C (depending on how beefy you want to go): http://www.opensource.apple.com/source/networkcmds/networkcmds-115/identd.tproj/proxy.c http://www.boudet.net/projects/proxy/proxy.c.html http://www.mikegagala.com/2009/12/proxy-c/ https://community.openvpn.net/openvpn/browser/proxy.c?rev=d222fcc587de09da0cb4681526e56b944313b22f http://www.w3.org/Library/src/HTTP.c

  1. /***************************************************************************
  2.  * This program is free software; you can redistribute it and/or modify *
  3.  * it under the terms of the GNU General Public License as published by *
  4.  * the Free Software Foundation; either version 2 of the License, or *
  5.  * (at your option) any later version. *
  6.  *
  7.  * This program is distributed in the hope that it will be useful, *
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  10.  * GNU General Public License for more details. *
  11.  *
  12.  * You should have received a copy of the GNU General Public License *
  13.  * along with this program; if not, write to the *
  14.  * Free Software Foundation, Inc., *
  15.  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  16.  *
  17.  * client.c *
  18.  ***************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <netdb.h>
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <sys/socket.h>
  29.  
  30.  
  31.  
  32. #define MAXDATASIZE 1024 // max buffer size
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36. int sockfd; // main socket file descriptor
  37. unsigned int PORT; // port number
  38. int numbytes; // # of bytes recieved by recv() system call
  39. char buf[MAXDATASIZE]; //buffer which contains data coming in from recv() system call
  40. struct hostent *he; // we don't wanna type ip's all the time now to we :)
  41. struct sockaddr_in their_addr; // connector's address information
  42.  
  43. //if user doesn't provide the clients hostname and port number
  44. if (argc != 3) {
  45. fprintf(stderr,"usage: client hostname and port number\n");
  46. exit(1);
  47. }
  48.  
  49. // get the ip addy for the hostname provided by arg[1]
  50. if ((he=gethostbyname(argv[1])) == NULL) {
  51. perror("gethostbyname");
  52. exit(1);
  53. }
  54.  
  55. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  56. perror("socket");
  57. exit(1);
  58. }
  59.  
  60. PORT = atoi(argv[2]); // turn this into a int value :)
  61.  
  62. host.sin_family = AF_INET;
  63. host.sin_port = htons(PORT); // short, network byte order
  64. host.sin_addr = *((struct in_addr *)he->h_addr);
  65. memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
  66.  
  67.  
  68. //connect to thy host
  69. if (connect(sockfd, (struct sockaddr *)&host, sizeof(struct sockaddr)) < 0)
  70. {
  71. perror("connect");
  72. exit(1);
  73. }
  74.  
  75. printf("Connected to %s\n", inet_ntoa(their_addr));
  76.  
  77. //recieve data from the host
  78. if ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) < 0)
  79. {
  80. perror("recv");
  81. exit(1);
  82. }
  83.  
  84. buf[numbytes-1] = '\0';
  85.  
  86. printf("Received: %s",buf); //print out what you recieved
  87.  
  88. close(sockfd);
  89.  
  90. return 0;
  91. }

Report this snippet  

You need to login to post a comment.