-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
socketpair support for posix compliance (IDFGH-12794) #13772
Comments
Thanks for the request, we do indeed not support his. It is something we can consider adding support for in the future. From looking at their github I could see some discussion about porting the library though, and it seems that even if we had |
we most likely will not support as for porting high-leverl libraries, we typically provide alternative implementation if https://github.com/espressif/esp-protocols/tree/master/components/asio Regarding the libplctag library, I think it's just easier to supply IDF specific implementation of |
@david-cermak @ESP-Marius thank you getting back on this issue.
@ESP-Marius I have some concerns about the RAM usage as well and I had started looking into one issue at a time. Initially the logging code using 1.5k buffer on the stack which was blowing up the stack very quickly. I think I can handle some RAM related issues if the networking is set up and there are packets on the wire. We also have 2MB PSRAM and can increase it on the design if needed just for this stack. |
@sramrajkar I think you're just missing the non-blocking mode, everything else looks good. #include <netdb.h>
#include <sys/socket.h>
#include "esp_check.h"
#define INVALID_SOCKET (-1)
static const char *TAG = "socket_helpers";
static int set_nonblocking(int sock)
{
int opt;
opt = fcntl(sock, F_GETFL, 0);
if (opt == -1){
return -1;
}
if (fcntl(sock, F_SETFL, opt | O_NONBLOCK) == -1) {
return -1;
}
return 0;
}
int socketpair(int domain, int type, int protocol, int sv[2])
{
struct sockaddr_storage ss;
struct sockaddr_in *sa = (struct sockaddr_in *)&ss;
socklen_t ss_len = sizeof(struct sockaddr_in);
int fd1 = INVALID_SOCKET;
int fd2 = INVALID_SOCKET;
int listenfd = INVALID_SOCKET;
int ret = 0; // Success
sa->sin_family = AF_INET;
sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sa->sin_port = 0;
listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ESP_GOTO_ON_FALSE(listenfd != INVALID_SOCKET, -1, err, TAG, "Cannot create listening socket");
ESP_GOTO_ON_FALSE(listen(listenfd, 1) == 0, -1, err, TAG, "Failed to listen");
memset(&ss, 0, sizeof(ss));
ss_len = sizeof(ss);
ESP_GOTO_ON_FALSE(getsockname(listenfd, (struct sockaddr *)&ss, &ss_len) >= 0, -1, err, TAG, "getsockname failed");
sa->sin_family = AF_INET;
sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
fd1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ESP_GOTO_ON_FALSE(fd1 != INVALID_SOCKET, -1, err, TAG, "Cannot create read side socket");
ESP_GOTO_ON_FALSE(set_nonblocking(fd1) == 0, -1, err, TAG, "Failed to set socket to nonblocking mode");
if (connect(fd1, (struct sockaddr *)&ss, ss_len) < 0) {
ESP_GOTO_ON_FALSE(errno == EINPROGRESS || errno == EWOULDBLOCK, -1, err, TAG, "Failed to connect fd1");
}
fd2 = accept(listenfd, NULL, 0);
if (fd2 == -1) {
ESP_GOTO_ON_FALSE(errno == EINPROGRESS || errno == EWOULDBLOCK, -1, err, TAG, "Failed to accept fd2");
}
ESP_GOTO_ON_FALSE(set_nonblocking(fd2) == 0, -1, err, TAG, "Failed to set socket to nonblocking mode");
close(listenfd);
sv[0] = fd1;
sv[1] = fd2;
return ret;
err:
if (listenfd != INVALID_SOCKET) {
close(listenfd);
}
if (fd1 != INVALID_SOCKET) {
close(fd1);
}
if (fd2 != INVALID_SOCKET) {
close(fd2);
}
return ret;
} |
@sramrajkar We will soon publish |
@david-cermak yes I can try this over the weekend with the new module. Thank you for looking into this. |
Here's an example of using sock-utils with libplctag library: https://github.com/david-cermak/esp-network-examples/tree/main/plctag/simple There's still one problem with declaration of |
Answers checklist.
General issue report
I am trying to compile the following library for ESP32S3 using 5.x.x branches
https://github.com/libplctag/libplctag
and need some implementation for int socketpair(int domain, int type, int protocol, int sv[2])
Looks like ESP IDF is missing this posix API.
The text was updated successfully, but these errors were encountered: