tab
CC.c
#include<stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <termios.h>
#include <fcntl.h>
#include<stdio.h> // printf
#include<string.h> // strlen // string
#include<sys/socket.h> // socket
#include<arpa/inet.h> // inet_addr
#include<netdb.h> // hostenta
#include <unistd.h>
#include<sys/socket.h> // socket
#include<arpa/inet.h> // inet_addr
struct sockaddr_in server;
#define DO 0xfd
#define WONT 0xfc
#define WILL 0xfb
#define DONT 0xfe
#define CMD 0xff
#define CMD_ECHO 1
#define CMD_WINDOW_SIZE 31
#define BUFLEN 20
#define BUFLEN 20
int len;
struct sockaddr_in server;
unsigned char buf[BUFLEN + 1];
static struct termios tin;
int hostname_to_ip(char *hostname, char *ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ((he = gethostbyname(hostname)) == NULL)
{
// get the host info
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **)he->h_addr_list;
for (i = 0; addr_list[i] != NULL; i++)
{
// Return the first one;
strcpy(ip, inet_ntoa(*addr_list[i]));
return 0;
}
return 1;
}
int main(int argc, char *argv[])
{
char *hostname = argv[1];
char ip[100];
int nread; /* return from read() */
int nready;
int maxfd;
hostname_to_ip(hostname, ip);
printf("%s resolved to %s", hostname, ip);
printf("podaj adres ");
char *adres[100];
scanf("%s", adres);
// website url
char *url = "google.pl";
/* char *pHttpPost =
"POST %s HTTP/1.1\r\n" "Host: %s:%d\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: %d\r\n\r\n" "%s";
char *addr = "http://localhost/post.php";
char *host = "127.0.0.1";
int port = 80;
char *msg = "aaa=1&bbb=2";
char strHttpPost[1024] = { 0 };*/
// sprintf(strHttpPost, pHttpPost, addr, host, port, strlen(msg), msg);
char *pHttpGet = "GET %s?%s HTTP/1.1\r\n" "Host: %s:%d\r\n%s\r\n\r\n";
char *addr = (adres);
char *host = (ip);
char*userAgent=("User-Agent:Chrome()ls");
int post = 80;
char *msg = (" [following]");
char strHttpGet[1024] = { 0 };
sprintf(strHttpGet, pHttpGet, addr, msg, host, post,userAgent);
printf("%s", strHttpGet);
// //**************************/////
// HTTP GET work on google.com;
int sock = socket(PF_INET, SOCK_STREAM, 0);
server.sin_family = PF_INET;
if (argc < 2)
{
printf("run witch argv toturialspoint.com\n");
}
else
{
server.sin_addr.s_addr = inet_addr(ip);
}
int port = 80;
if (argc == 3)
{
port = atoi(argv[2]);
}
printf("%s resolved to %s", hostname, ip);
server.sin_port = htons(port);
connect(sock, (struct sockaddr *)&server, sizeof(server));
ssize_t numBytesRecv = 10;
char replyMessage[520];
char *replyMessage2[5200];
FILE *fp;
if ((fp = fopen("test2.html", "w")) == NULL)
{
printf("can't open file to write\n");
exit(1);
};
// / ######################## ///
// https://kruszwil.pl/asortyment/bluzy
printf("port is: %d\n", port);
printf("host ip is %s\n", argv[1]);
printf("enter the , exemple:/index.aspx\n");
if (port == 80)
{
send(sock, strHttpGet, sizeof(strHttpGet) + 255, 0);
do
{
numBytesRecv = recv(sock, replyMessage, strlen(replyMessage) + 255, 0);
printf("%s\n", replyMessage);
fprintf(fp, "%s\n", replyMessage);
memset(&replyMessage, 0, sizeof(replyMessage));
}
while (numBytesRecv > 0);
}
else
{
while (1)
{
//terminal_set();
// atexit(terminal_reset);
struct timeval ts;
ts.tv_sec = 1; // 1 second
ts.tv_usec = 0;
fd_set fds;
/* Set up polling. */
FD_ZERO(&fds);
if (sock != 0)
FD_SET(sock, &fds);
FD_SET(0, &fds);
// wait for data
int nready = select(sock + 1, &fds, (fd_set *) 0, (fd_set *) 0, &ts);
if (nready < 0)
{
perror("select. Error");
return 1;
}
else if (nready == 0)
{
ts.tv_sec = 1; // 1 second
ts.tv_usec = 0;
}
else if (sock != 0 && FD_ISSET(sock, &fds))
{
// start by reading a single byte
int rv;
if ((rv = recv(sock, buf, 1, 0)) < 0)
{
return 1;
}
else if (rv == 0)
{
printf("Connection closed by the remote end\n\r");
return 0;
}
if (buf[0] == CMD)
{
// read 2 more bytes
len = recv(sock, buf + 1, 2, 0);
if (len < 0)
{
return 1;
}
else if (len == 0)
{
printf("Connection closed by the remote end\n\r");
return 0;
}
// negotiate(sock, buf, 3);
}
else
{
len = 1;
buf[len] = '\0';
printf("%s", buf);
fprintf(fp, "%s", buf);
fflush(0);
// tcflush(sock, 0);
}
}
else if (FD_ISSET(0, &fds))
{
buf[0] = getc(stdin); // fgets(buf, 1, stdin);
if (send(sock, buf, 1, 0) < 0)
return 1;
if (buf[0] == '\n') // with the terminal in raw mode we need
// to force a LF
putchar('\r');
}
}
}
printf("save in test2.html file\n");
fclose(fp);
}