-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec16.c
More file actions
132 lines (101 loc) · 3.84 KB
/
spec16.c
File metadata and controls
132 lines (101 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "header.h"
//Competed using TCP and DNS
/*#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <regex.h>*/
// Function to send an HTTP GET request
void sendHttpGetRequest(int sockfd, const char *url) {
char request[4096]; // Adjust the buffer size as needed
snprintf(request, sizeof(request), "GET %s HTTP/1.1\r\nHost: man.he.net\r\n\r\n", url);
// HTTP GET request string using the provided url,
//HTTP version (HTTP/1.1)
write(sockfd, request, strlen(request));
}
// Function to extract and print text content using regular expressions
void extractAndPrintText(const char *htmlContent) {
regex_t regex;
regmatch_t matches[1];
// Define a regular expression pattern to match text within HTML tags
const char *pattern = ">([^<]+)<";
if (regcomp(®ex, pattern, REG_EXTENDED) != 0) {
fprintf(stderr, "Failed to compile regular expression\n");
return;
}
const char *cursor = htmlContent;
while (regexec(®ex, cursor, 1, matches, 0) == 0) {
// Print the matched text
printf("%.*s\n", (int)(matches[0].rm_eo - matches[0].rm_so - 2), cursor + matches[0].rm_so + 1);
// Move the cursor to the next position
cursor += matches[0].rm_eo;
}
regfree(®ex);
}
int iMan_comm(const char *command) {
char input[strlen(command) + 1]; // Remove iMan
strcpy(input, command);
strtok(input, " \n"); // iMan
char *temp = strtok(NULL, " \n"); // sleep or ls or echo ...
char inp[100];
strcpy(inp, temp);
char url[1024]; // Adjust the buffer size as needed
snprintf(url, sizeof(url), "http://man.he.net/?topic=iMan+%s§ion=all", inp);
printf("URL: %s\n", url);
// DNS Resolution:
//DNS resolution, which is the process of converting a human-readable domain name (e.g., "man.he.net") into an IP address
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));// initially set to zero
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_STREAM; //TCP
int addr_result = getaddrinfo("man.he.net", "80", &hints, &res);
if (addr_result != 0) { //hostname ,http service
fprintf(stderr, "DNS resolution failed: %s\n", gai_strerror(addr_result));
return 1;
}
//Got:IP of hostname stored in res
// Open a TCP Socket
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);//integer: socket file descriptor
if (sockfd == -1) {
perror("Socket creation failed");
return 1;
}
// Connect to the Server
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) {
perror("Socket connection failed");
close(sockfd);
return 1;
}
// Send an HTTP GET Request
sendHttpGetRequest(sockfd, url);
// Read the HTML Response
char buffer[1024]; // Adjust the buffer size as needed
ssize_t bytes_read;
char *htmlContent = NULL;
size_t htmlSize = 0;
while ((bytes_read = read(sockfd, buffer, sizeof(buffer))) > 0) {
// Resize the htmlContent buffer
htmlContent = realloc(htmlContent, htmlSize + bytes_read + 1);
//original + read curr
if (htmlContent == NULL) {
perror("Memory allocation failed");
close(sockfd);
return 1;
}
// Copy the new data into htmlContent
memcpy(htmlContent + htmlSize, buffer, bytes_read);
// Pointer+Size pe buffer put
htmlSize += bytes_read;
htmlContent[htmlSize] = '\0';
}
// Extract and Print Text Content
extractAndPrintText(htmlContent);
// Clean up
free(htmlContent);
close(sockfd);
return 0;
}
//SERVER: socket ,bind , listen , accept , send-receive
//CLIENT: socket , ,connect, , send-receive