https原始套接字获取 sin
时间: 2023-11-21 09:24:45 浏览: 36
获取 sin 的过程需要先建立一个 HTTPS 套接字连接,然后使用原始套接字获取 sin。具体步骤如下:
1. 创建一个 HTTPS 套接字连接。可以使用 Python 的 requests 模块或者 OpenSSL 库来创建一个 HTTPS 套接字连接。
2. 使用 HTTPS 套接字连接发送一个 HTTP 请求。可以使用 requests 模块的 get() 方法或者 OpenSSL 库的 SSL_write() 方法来发送请求。
3. 接收 HTTP 响应。可以使用 requests 模块的 response 属性或者 OpenSSL 库的 SSL_read() 方法来接收响应。
4. 在 HTTP 响应中查找 sin。可以使用正则表达式或者 BeautifulSoup 等 HTML 解析库来查找 sin。
5. 关闭 HTTPS 套接字连接。可以使用 requests 模块的 close() 方法或者 OpenSSL 库的 SSL_shutdown() 方法来关闭连接。
需要注意的是,在使用原始套接字获取 sin 的过程中,需要确保网络连接是安全的,避免泄露敏感信息。
相关问题
原始套接字编程 ping
原始套接字编程是指在应用层直接访问传输层以下的网络协议栈,可以自己构造IP数据包和ICMP数据包等,实现对网络的底层控制。ping是基于ICMP协议的网络工具,用于测试网络连接是否正常。在原始套接字编程中,可以通过构造ICMP数据包来实现ping的功能。具体实现步骤如下:
```c
// 声明头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
// 计算校验和
unsigned short checkSum(unsigned short *addr, int len) {
unsigned int sum = 0;
while (len > 1) {
sum += *addr++;
len -= 2;
}
if (len == 1) {
sum += *(unsigned char *)addr;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return (unsigned short)(~sum);
}
// 发送ICMP请求报文
void sendPing(int sockfd, struct sockaddr_in *addr) {
struct icmp icmp;
icmp.icmp_type = ICMP_ECHO;
icmp.icmp_code = 0;
icmp.icmp_id = getpid();
icmp.icmp_seq = 0;
icmp.icmp_cksum = 0;
icmp.icmp_cksum = checkSum((unsigned short *)&icmp, sizeof(icmp));
sendto(sockfd, &icmp, sizeof(icmp), 0, (struct sockaddr *)addr, sizeof(*addr));
}
// 接收ICMP响应报文
int recvPing(int sockfd, struct sockaddr_in *addr) {
char buf[1024];
struct ip *ip = (struct ip *)buf;
struct icmp *icmp;
int len = sizeof(*addr);
int n = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)addr, &len);
icmp = (struct icmp *)(buf + (ip->ip_hl << 2));
if (icmp->icmp_type == ICMP_ECHOREPLY) {
return 1;
}
return 0;
}
// ping主函数
void ping(char *ip) {
int sockfd;
struct sockaddr_in addr;
struct hostent *host;
int count = 4;
int success = 0;
int i;
// 创建原始套接字
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0) {
perror("socket");
exit(1);
}
// 获取目标主机地址
host = gethostbyname(ip);
if (host == NULL) {
perror("gethostbyname");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr = *((struct in_addr *)host->h_addr);
// 发送ICMP请求报文
for (i = 0; i < count; i++) {
sendPing(sockfd, &addr);
if (recvPing(sockfd, &addr)) {
success++;
}
sleep(1);
}
// 输出结果
printf("Ping %s (%s) %d times, success %d times\n", ip, inet_ntoa(addr.sin_addr), count, success);
close(sockfd);
}
```
Linux C++ 发送DHCP报文获取DHCP信息
发送DHCP报文获取DHCP信息的方法如下:
1. 创建原始套接字:
```c++
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sock < 0) {
perror("socket");
exit(1);
}
```
2. 设置套接字选项:
```c++
int on = 1;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
perror("setsockopt");
exit(1);
}
```
3. 构造DHCP报文:
```c++
struct dhcp_packet {
uint8_t op; /* op: Message opcode/type */
uint8_t htype; /* htype: Hardware addr type */
uint8_t hlen; /* hlen: Hardware addr length */
uint8_t hops; /* hops: Number of relay agent hops from client */
uint32_t xid; /* xid: Transaction ID */
uint16_t secs; /* secs: Seconds since client started trying to boot */
uint16_t flags; /* flags: Flag bits */
struct in_addr ciaddr; /* ciaddr: Client IP address (if already in use) */
struct in_addr yiaddr; /* yiaddr: Your (client) IP address */
struct in_addr siaddr; /* siaddr: Next server to use in bootstrap */
struct in_addr giaddr; /* giaddr: Relay agent IP address */
uint8_t chaddr[16]; /* chaddr: Client hardware address */
uint8_t sname[64]; /* sname: Server host name */
uint8_t file[128]; /* file: Boot file name */
uint8_t options[312]; /* options: Optional parameters (actual length dependent on MTU). */
} dhcp_packet;
memset(&dhcp_packet, 0, sizeof(dhcp_packet));
dhcp_packet.op = BOOTREQUEST;
dhcp_packet.htype = ARPHRD_ETHER;
dhcp_packet.hlen = ETH_ALEN;
dhcp_packet.xid = htonl(random());
memcpy(dhcp_packet.chaddr, mac_address, ETH_ALEN);
```
在构造DHCP报文时,需要填写一些固定的字段,如op、htype、hlen、xid、chaddr等,还需要填写一些可选参数,如sname、file、options等。其中,mac_address是客户端的MAC地址。
4. 发送DHCP报文:
```c++
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(67);
addr.sin_addr.s_addr = INADDR_BROADCAST;
if (sendto(sock, &dhcp_packet, sizeof(dhcp_packet), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("sendto");
exit(1);
}
```
在发送DHCP报文时,需要指定目的地地址为广播地址,即INADDR_BROADCAST。
5. 接收DHCP响应报文:
```c++
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
char buf[1024];
ssize_t n = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen);
if (n < 0) {
perror("recvfrom");
exit(1);
}
```
在接收DHCP响应报文时,需要设置接收缓冲区大小,一般建议设置为1024字节。收到响应报文后,可以解析报文中的各个字段,如yiaddr、siaddr、giaddr等,从而获取DHCP服务器分配的IP地址等信息。
以上就是发送DHCP报文获取DHCP信息的基本步骤。注意,发送DHCP报文需要root权限才能执行。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)