C code problem

C code problem

Post by iRhadagast on Mon Oct 31, 2011 8:43 pm
([msg=62727]see C code problem[/msg])

This is going to seem like a silly, noobish question, mostly because it is, but I can't seem to figure out my problem. I 've been reading Jon Erickson's "Hacking" and have been working with the code examples he gives, but at times encounter errors I can't resolve despite the code appearing to be kosher to my untrained eye. A little help or a gentle nudge in the right direction would be greatly appreciated.

The error I'm getting is this: webserver_id.c:48:1: error: expected declaration or statement at end of input
Which points to the end of the code to my } which made me inspect all my { to ensure I have them all, which I do. I'm at a loss here as to what I'm missing.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>

#include "hacking.h"
#include "hacking-network.h"

int main(int argc, char *argv[]) {
int sockfd;
struct hostent *host_info;
struct sockaddr_in target_addr;
unsigned char buffer[4096];

if(argc <2) {
printf("Usage:%s <hostname>\n", argv[0]);
exit(1);
}

if((host_info = gethostbyname(argv[1])) == NULL)
fatal("looking up hostname");

if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
fatal("In socket");

target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(80);
target_addr.sin_addr = *((struct in_adr *)host_info->h_addr);
memset(&(target_addr.sin_zero), '\0', 8); //Zero the rest of the struct

if (connect(sockfd,(struct sockaddr *)&target_addr, sizeof(struct sockaddr)) == -1)
fatal("connecting to target server");

send_string(sockfd, "HEAD / HTTP/1.0\r\n\r\n");

while(recv_line(sockfd, buffer)) {
if(strncasecmp(buffer, "Server:", 7) == 0) {
printf("The web server for %s is %s\n", argv[1], buffer+8);
exit(0);
}
}
printf("Server line not found\n");
exit(1);
}
iRhadagast
New User
New User
 
Posts: 2
Joined: Mon Oct 31, 2011 8:37 pm
Blog: View Blog (0)


Re: C code problem

Post by ghost107 on Tue Nov 01, 2011 11:17 am
([msg=62728]see Re: C code problem[/msg])

First use the CODE tags its much easier to read.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>

int main(int argc, char *argv[]) {
   int sockfd;
   struct hostent *host_info;
   struct sockaddr_in target_addr;
   unsigned char buffer[4096];

   if(argc <2) {
      printf("Usage:%s <hostname>\n", argv[0]);
      exit(1);
   }

   if((host_info = gethostbyname(argv[1])) == NULL)
      fatal("looking up hostname");

   if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
      fatal("In socket");

   target_addr.sin_family = AF_INET;
   target_addr.sin_port = htons(80);
   target_addr.sin_addr = *((struct in_adr *)host_info->h_addr);
   memset(&(target_addr.sin_zero), '\0', 8); //Zero the rest of the struct

   if (connect(sockfd,(struct sockaddr *)&target_addr, sizeof(struct sockaddr)) == -1)
      fatal("connecting to target server");

   send_string(sockfd, "HEAD / HTTP/1.0\r\n\r\n");

   while(recv_line(sockfd, buffer)) {
      if(strncasecmp(buffer, "Server:", 7) == 0) {
         printf("The web server for %s is %s\n", argv[1], buffer+8);
         exit(0);
      }
   }
   printf("Server line not found\n");
   exit(1);
}


The code is a client that connects to the HTTP server and sends the HEAD option:
This 2 includes you don't use them in your code, its pointless to call them:
#include "hacking.h"
#include "hacking-network.h"

The rest of the code i see is ok;
The second thing I saw was:
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(80);
target_addr.sin_addr = *((struct in_adr *)host_info->h_addr);
memset(&(target_addr.sin_zero), '\0', 8); //Zero the rest of the struct

You can use something like this:
memset(&target_addr, 0, sizeof(target_addr)); /* Clear struct */
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(80);
target_addr.sin_addr = *((struct in_adr *)host_info->h_addr);


For comunication i Never used send_string, I usually use send and recv.
http://linux.die.net/man/2/send
http://linux.die.net/man/2/recv

Don't forget to use close(sockfd) before exit().
ghost107
Poster
Poster
 
Posts: 110
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: C code problem

Post by iRhadagast on Tue Nov 01, 2011 5:23 pm
([msg=62734]see Re: C code problem[/msg])

Thanks, I think I'll go back and look at the previous stuff a little more in depth. I didn't know about the COE tags, or I would have used them, so thanks for letting me know about that, I'll use them in future posts when my noob side gets the better of me, which I suspect will be pretty frequently.
iRhadagast
New User
New User
 
Posts: 2
Joined: Mon Oct 31, 2011 8:37 pm
Blog: View Blog (0)


Re: C code problem

Post by FunctionCreep on Tue Nov 15, 2011 3:54 pm
([msg=62929]see Re: C code problem[/msg])

Well you need the headers hacking.h & network-hacking.h for the recv_line() & send_string() & fatal() functions included in the program you provided.

You also have a typo in the following line:
Code: Select all
target_addr.sin_addr = *((struct in_adr *)host_info->h_addr);

*((struct in_addr *)
"I hope for nothing. I fear nothing. I am free." ~ Nikos Kazantzakis
User avatar
FunctionCreep
Experienced User
Experienced User
 
Posts: 93
Joined: Tue May 18, 2010 6:19 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests