简体   繁体   中英

Why am i getting “direct://” as output for my program?

I am in the early stages of writing a proxy server in c for class and while debugging, my program gives me a weird output simpley with two lines of

direct://

direct://

what does this mean? I've never had this happen before. The program even outputs this when i dont provide 3 arguments which i required for this program to execute.

#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <string.h>

int main (int argc, char *argv[]){

   if(argc!=3){
      printf("Usage: proxy <IP address> <port no.>");
      exit(1);
   }

   int csock, ssock, clen, slen;
   int csocka, ssocka;
   int rc, fd, ttl;
   char method[40];
   char uri[80];
   char prot[40];
   char cbuf[100];
   time_t logtime;
   char * pch;

   struct sockaddr_in caddr;
   struct sockaddr_in caddr2;
   struct sockaddr_in saddr;
   struct sockaddr_in saddr2;

   csock = socket(AF_INET, SOCK_STREAM, 0);
   caddr.sin_family = AF_INET;
   caddr.sin_addr.s_addr = inet_addr(argv[1]);
   caddr.sin_port = htons(atoi(argv[2]));
   clen = sizeof(caddr);
   rc = bind(csock, (struct sockaddr *) &caddr, clen);
   if(rc < 0){
      printf("bind failed");
      exit(1);
   }
   rc = listen(csock, 5);
   if(rc < 0){
      printf("listen failed");
      exit(1);
   }
   printf("hey");
   csocka = accept(csock, (struct sockaddr *) &caddr2, &clen);
   if(csocka < 0){
      printf("accept failed");
      exit(1);
   }

   while(1){
      read(csocka,&cbuf,sizeof(cbuf));
      time(&logtime);                                   //time of req.
      if(cbuf==NULL){
         cerror("400 Bad Request: empty request");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      ttl = strlen(cbuf);
      while(cbuf[ttl-1] == '\n' || cbuf[ttl-1] == '\r'){
         cbuf[ttl--] = '\0';
      }
      if(sscanf(cbuf,"%[^ ] %[^ ] %[^ ]", method, uri, prot) != 3){
         cerror("400 Bad Request: Unexpected number of arguments");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      if(method!="GET" || method !="HEAD"){
         cerror("405 Method Not Allowed: GET/HEAD only");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      if(uri == (char*) 0){
         cerror("400 Bad Request: empty url");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      printf("%s \n", cbuf);
   }

   close(csocka);

}

The most probable reason is that you aren't running your program, but some system program.

If you are on a Linux machine, type:

which <program name>

to find out which executable you are actually running.

type:

./<program name>

to run your program instead (provided that you are in the same directory as your executable).

Why are you ignoring the return value of read ? What makes you think read is null-terminating cbuf for you? If read isn't null terminating cbuf , then what makes you think it's safe to pass buf to strlen ? You're invoking undefined behaviour by passing something that isn't a string to strlen... The behaviour that follows may seem strange or inconsistent, but that's the nature of undefined behaviour.

int len = read(csocka,&cbuf,sizeof(cbuf));
if (len <= 0) {
    /* something went wrong in read().
     * report an error and stop here... */
    break;
}
/* Once error checking is performed, len is the number of bytes recieved by read. */

Consider the code above. Do you need the line ttl=strlen(cbuf); , if you correctly check for errors?

printf("%s \\n", cbuf); is wrong, because cbuf isn't a string. Consider fwrite(cbuf, len, stdout); putchar('\\n'); fwrite(cbuf, len, stdout); putchar('\\n'); or printf("%.*s\\n", len, stdout); .

write(csocka, &errbuf, sizeof(errbuf)); also looks wrong, but I'll leave that in your hands. If you need us to fix these kinds of errors for you, then your method of learning isn't working very well. Which book are you reading?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM