int DNS_query_create(struct DNS_Query *query,const char *hostname,const char *type){ if(query==NULL||hostname==NULL){ printf("query or hostname wrong.\n"); return -1; } memset(query, 0x00, sizeof(struct DNS_Query)); query->name=malloc(sizeof(hostname)+2); query->length=strlen(hostname)+2; char *ptr=query->name; int offset = 0; const char s[2]="."; char *hostname_dup= strdup(hostname); char *token=strtok(hostname_dup,s); while(token!=NULL){ size_t len=strlen(token); *(ptr+offset)=len; offset++; strncpy(ptr+offset,token,len+1); offset+=len; token=strtok(NULL,s); } free(hostname_dup); if(strcmp(type,"A")==0){ query->qtype= htons(TYPE_A); }else if(strcmp(type,"CNAME")==0){ query->qtype=htons(TYPE_CNAME); }else if(strcmp(type,"MX")==0){ query->qtype=htons(TYPE_MX); }else if(strcmp(type,"PTR")==0){ query->qtype=htons(TYPE_PTR); }else{ printf("No such type!!!\n"); exit(-1); } query->qclass= htons(0x0001); return offset; }
时间: 2024-04-06 14:28:26 浏览: 68
这段代码是一个函数,函数名为“DNS_query_create”,它接收三个参数,一个指向“DNS_Query”结构体的指针“query”,一个指向字符串的指针“hostname”,一个指向字符串的指针“type”。该函数的作用是根据传入的主机名和查询类型,初始化“query”指向的结构体,设置一些DNS协议查询的字段值,如name、length、qtype和qclass。如果传入的指针为NULL,则打印“query or hostname wrong.”的错误信息,并返回-1。该函数使用了C语言中的“memset”函数、“malloc”函数、“strdup”函数、“strtok”函数、“strlen”函数和“strcmp”函数。
相关问题
下面的代码修改一下 需要支持https双向认证int https_post(const char *cert_path, const char *url, const char *body, char *response) { int sockfd, len; struct sockaddr_in dest; struct hostent *host; SSL_CTX *ctx; SSL ssl; char request[MAX_BUF_SIZE], buf[MAX_BUF_SIZE]; // 初始化OpenSSL库 SSL_library_init(); SSL_load_error_strings(); OpenSSL_add_all_algorithms(); // 解析主机名 //printf("66666666 %s\n", url); char hostname[2560]; getHostFromURL(url, hostname); host = gethostbyname(hostname); if (host == NULL) { perror("gethostbyname"); return -1; } // 创建套接字 sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); return -1; } // 设置目标地址 bzero(&dest, sizeof(dest)); dest.sin_family = AF_INET; dest.sin_port = htons(443); dest.sin_addr.s_addr = (long)host->h_addr; // 连接服务器 if (connect(sockfd, (struct sockaddr)&dest, sizeof(dest)) != 0) { perror("connect"); return -1; } ctx = SSL_CTX_new(TLS_method()); // 设置支持的协议版本为 TLSv1.2 SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION); if (ctx == NULL) { perror("SSL_CTX_new"); return -1; } // 加载证书 if (SSL_CTX_load_verify_locations(ctx, cert_path, NULL) != 1) { perror("SSL_CTX_load_verify_locations"); return -1; } // 创建SSL ssl = SSL_new(ctx); if (ssl == NULL) { perror("SSL_new"); return -1; } // 将套接字绑定到SSL SSL_set_fd(ssl, sockfd); // SSL握手 if (SSL_connect(ssl) == -1) { perror("SSL_connect"); return -1; } // 构造HTTP请求 char *escaped_url = urlencode(url); sprintf(request, "POST / HTTP/1.1\r\n" "Host: %s\r\n" "Content-Type: application/json\r\n" "Content-Length: %d\r\n" "\r\n" "%s", escaped_url, strlen(body), body); // 发送HTTP请求 SSL_write(ssl, request, strlen(request)); // 接收HTTP响应 len = SSL_read(ssl, buf, MAX_BUF_SIZE); printf("response %s\n", buf); // 关闭SSL和套接字 SSL_free(ssl); close(sockfd); // 处理响应 strncpy(response, buf, len); printf("Child process created with PID %d\n", 60909); response[len] = '\0'; printf("Child process created with PID %d\n", 70909); // 清理OpenSSL库 SSL_CTX_free(ctx); EVP_cleanup(); printf("Child process created with PID %d\n", 909); free(escaped_url); return 0; }
以下是修改后的代码,支持https双向认证:
```
int https_post(const char *cert_path, const char *key_path, const char *url, const char *body, char *response) {
int sockfd, len;
struct sockaddr_in dest;
struct hostent *host;
SSL_CTX *ctx;
SSL *ssl;
char request[MAX_BUF_SIZE], buf[MAX_BUF_SIZE];
// 初始化OpenSSL库
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// 解析主机名
char hostname[2560];
getHostFromURL(url, hostname);
host = gethostbyname(hostname);
if (host == NULL) {
perror("gethostbyname");
return -1;
}
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
return -1;
}
// 设置目标地址
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(443);
dest.sin_addr.s_addr = *(long*)host->h_addr;
// 连接服务器
if (connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) != 0) {
perror("connect");
return -1;
}
ctx = SSL_CTX_new(TLS_method());
if (ctx == NULL) {
perror("SSL_CTX_new");
return -1;
}
// 设置支持的协议版本为 TLSv1.2
SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION);
// 加载 CA 证书
if (SSL_CTX_load_verify_locations(ctx, cert_path, NULL) != 1) {
perror("SSL_CTX_load_verify_locations");
return -1;
}
// 加载客户端证书和私钥
if (SSL_CTX_use_certificate_file(ctx, cert_path, SSL_FILETYPE_PEM) <= 0) {
perror("SSL_CTX_use_certificate_file");
return -1;
}
if (SSL_CTX_use_PrivateKey_file(ctx, key_path, SSL_FILETYPE_PEM) <= 0) {
perror("SSL_CTX_use_PrivateKey_file");
return -1;
}
if (!SSL_CTX_check_private_key(ctx)) {
perror("SSL_CTX_check_private_key");
return -1;
}
// 创建 SSL
ssl = SSL_new(ctx);
if (ssl == NULL) {
perror("SSL_new");
return -1;
}
// 将套接字绑定到SSL
SSL_set_fd(ssl, sockfd);
// SSL握手
if (SSL_connect(ssl) == -1) {
perror("SSL_connect");
return -1;
}
// 构造HTTP请求
char *escaped_url = urlencode(url);
sprintf(request, "POST / HTTP/1.1\r\n"
"Host: %s\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %d\r\n"
"\r\n"
"%s", escaped_url, strlen(body), body);
// 发送HTTP请求
SSL_write(ssl, request, strlen(request));
// 接收HTTP响应
len = SSL_read(ssl, buf, MAX_BUF_SIZE);
// 关闭SSL和套接字
SSL_shutdown(ssl);
SSL_free(ssl);
close(sockfd);
// 处理响应
strncpy(response, buf, len);
response[len] = '\0';
// 清理OpenSSL库
SSL_CTX_free(ctx);
EVP_cleanup();
free(escaped_url);
return 0;
}
```
您需要将证书路径和私钥路径作为参数传入,以支持双向认证。其中,cert_path 是 CA 证书路径,key_path 是客户端证书和私钥路径。
char *url = "https://otaapi-sit.lotuscars.com.cn/v50/vehicle/api/register"; char *escaped_url = urlencode(url); char *request = (char *)malloc(strlen(escaped_url) + 100); sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", escaped_url, escaped_url); //发送HTTP请求 //... free(escaped_url); free(request);上面的c代码是不是需要调整 如果用https通信的话
是的,如果使用HTTPS通信,则上述代码需要进行调整。您需要使用HTTPS的URL来初始化HTTP请求,并在请求头中添加必要的SSL证书验证和其他参数。以下是一个示例代码,可以帮助您进行HTTPS通信:
```c
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
// 定义HTTPS请求的URL
const char *url = "https://otaapi-sit.lotuscars.com.cn/v50/vehicle/api/register";
// 初始化SSL库
SSL_library_init();
// 创建SSL上下文
SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_client_method());
// 创建SSL套接字
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serveraddr = {0};
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(443);
serveraddr.sin_addr.s_addr = inet_addr("otaapi-sit.lotuscars.com.cn");
// 连接到服务器
connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
// 创建BIO对象
BIO *bio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, NULL);
SSL_set_mode(SSL_get_ssl(bio), SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(bio, "otaapi-sit.lotuscars.com.cn:https");
// 发送HTTP请求
char *request = "GET /v50/vehicle/api/register HTTP/1.1\r\n"
"Host: otaapi-sit.lotuscars.com.cn\r\n"
"Connection: close\r\n"
"\r\n";
BIO_write(bio, request, strlen(request));
// 读取HTTP响应
char buffer[4096];
int len;
while ((len = BIO_read(bio, buffer, sizeof(buffer))) > 0) {
// 处理响应数据
printf("%.*s", len, buffer);
}
// 释放资源
BIO_free_all(bio);
close(sockfd);
SSL_CTX_free(ctx);
```
请注意,以上代码仅供参考,您需要根据自己的实际情况进行修改。同时,为了确保通信安全,请在代码中添加必要的SSL证书验证。
阅读全文