C++在Qt中的行为不同,bind()返回EINVAL



我已经签出了bind((返回EINVAL,这不是问题所在。在你对重复感到愤怒之前,请通读一遍

我正在尝试连接到wpa_supplicant。基本上,我试图使用C++实现这一点:

import os
import select
import socket
interface = "wlp4s0"
wpa_send_path = "/run/wpa_supplicant/"+interface
wpa_recv_path = "/tmp/wpa_ctrl_{pid}-{count}".format(pid=os.getpid(), count=1)
soc = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM, 0)
soc.bind(wpa_recv_path)
soc.connect(wpa_send_path)
print("> PING")
soc.send(b"PING")
print("<", soc.recv(4096).decode().strip())

来自https://gist.github.com/artizirk/cd3980c8ff870eb0bfce68bc26a2676b

我已经做了我想做的事情,但使用了纯C++。这是代码:

#include <iostream>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <sys/un.h>
#define PATH   "/run/wpa_supplicant/wlp4s0"
char path[100] = "/tmp/wpa_ctrl_";
int main(void) {
int ctrl_socket;
char buffer[1024];
struct sockaddr_un socket_addr, send_addr;
memset(&socket_addr, 0, sizeof(struct sockaddr_un));
memset(&send_addr, 0, sizeof(struct sockaddr_un));
socket_addr.sun_family = AF_UNIX;
send_addr.sun_family = AF_UNIX;
strcpy(send_addr.sun_path, PATH);
strcat(path, std::to_string(getpid()).c_str());
strcat(path, "-1");
strcat(socket_addr.sun_path, path);
if ((ctrl_socket = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
std::cerr << "Error creating socket!" << std::endl;
std::exit(EXIT_FAILURE);
}
/* Unlink if already bound */
unlink(socket_addr.sun_path);
if ((connect(ctrl_socket, (struct sockaddr *)&send_addr, SUN_LEN(&send_addr))) == -1) {
std::cerr << "Error connecting to socket!" << std::endl;
std::exit(EXIT_FAILURE);
}
if (bind(ctrl_socket, (const struct sockaddr *)&socket_addr, offsetof(struct sockaddr_un, sun_path) + strlen(path) + 1) == -1) {
perror("Error");
exit(EXIT_FAILURE);
}
send(ctrl_socket, "PING", 5, 0);
recv(ctrl_socket, buffer, 1024, 0);
std::cout << buffer << std::endl;
close(ctrl_socket);
return 0;
}

此代码运行良好。但当我在Qt中执行时,bind()总是返回EINVAL,即Invalid Arguments。这是代码:

WPASupplicantControl::WPASupplicantControl(std::string wlan_interface_name)
:wpa_send_ctrl_iface(WPA_SEND_CTRL_IFACE_PREFIX + QString::fromStdString(wlan_interface_name)),
wpa_recv_ctrl_iface(
WPA_RECV_CTRL_IFACE_PREFIX +
QString::fromStdString(std::to_string(getpid())) +
"-1"
)
{
struct sockaddr_un send_address, recv_address;
send_address.sun_family = AF_UNIX;
recv_address.sun_family = AF_UNIX;
memset(&send_address, 0, sizeof (send_address));
memset(&recv_address, 0, sizeof (recv_address));
strncpy(send_address.sun_path, wpa_send_ctrl_iface.toStdString().c_str(), wpa_send_ctrl_iface.length());
strncpy(recv_address.sun_path, wpa_recv_ctrl_iface.toStdString().c_str(), wpa_send_ctrl_iface.length());
if ((wpa_control_socket = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
qCritical() << "socket() failed!";
exit(EXIT_FAILURE);
}
/* Attatch to sending and receiving control interfaces */
if (connect(wpa_control_socket, (const struct sockaddr *)&send_address, SUN_LEN(&send_address)) == -1) {
qCritical() << "Error connecting to wpa_supplicant send control iface!";
close(wpa_control_socket);
exit(EXIT_FAILURE);
}
/* Detatch if it's already bound */
unlink(recv_address.sun_path);
if (bind(wpa_control_socket, (const struct sockaddr *)&recv_address, offsetof(struct sockaddr_un, sun_path) + wpa_recv_ctrl_iface.length() + 1) == -1) {
qCritical() << "Error binding to wpa_supplicant recv control iface!";
close(wpa_control_socket);
exit(EXIT_FAILURE);
}
}

测试此代码的功能伪项目位于:https://github.com/gaurav712/socket_test

我已经检查了数百次所有的值,它们都很好,但仍然不起作用。它只是转到connect()呼叫。

我想我应该使用Qt特定的东西,我发现了QLocalSockethttps://doc.qt.io/qt-5/qlocalsocket.html但当我导入它时,Qt无法识别该类。也许是因为我在Qt6.x上,它现在被弃用了(尽管我没有发现任何官方的弃用通知(。我该怎么办?我做错了什么?

与Qt完全无关:在将套接字家族设置为AF_UNIX(在大多数平台上为"1"(之后,对sockaddr结构的所有0执行memset。系统应该如何知道您想要本地套接字?

strncpy将最多从源向目标复制n字节。如果n个字节中没有空字符,它就不会复制它。strncpy的第三个参数通常用于传递目标缓冲区的长度。

更改此项:

strncpy(send_address.sun_path, wpa_send_ctrl_iface.toStdString().c_str(), wpa_send_ctrl_iface.length());
strncpy(recv_address.sun_path, wpa_recv_ctrl_iface.toStdString().c_str(), wpa_send_ctrl_iface.length());

成为这样:

strncpy(send_address.sun_path, 
wpa_send_ctrl_iface.toStdString().c_str(),
sizeof(send_address.sun_path) );
strncpy(recv_address.sun_path,
wpa_recv_ctrl_iface.toStdString().c_str(),
sizeof(recv_address.sun_path));

此外,正如评论中所讨论的,bind的长度参数看起来也很时髦。尝试以上两种更改。

相关内容

  • 没有找到相关文章

最新更新