socket::accept continual to return EGAIN - linux

I use nonblocking socket to receive new connection. But the code repeatedly fails to accept().
int sockfd = ::socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
::bind(sockfd, bind_addr, static_cast<socklen_t>(sizeof(struct sockaddr_in6)));
ret = ::listen(sockfd, SOMAXCONN);
while (True) {
::poll(&*pollfds_.begin(), pollfds_.size(), timeoutMs);
struct sockaddr_in6 addr;
bzero(&addr, sizeof addr);
socklen_t addrlen = static_cast<socklen_t>(sizeof *addr);
int connfd = ::accept4(sockfd, sockaddr_cast(addr),
&addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
}
errno is EAGAIN.

From the manpage to accept(2):
EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and no connections are present to be accepted. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
This means that the call to accept is made before the client has connected.

Before calling accept, you must call listen and bind.
But as your socket is not blocking, you should wait for client to wait to connect. You can do that with select function:
int sockfd = ::socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
// addr is for accept call, sin for bind call
struct sockaddr_in6 addr, sin;
bzero(&addr, sizeof addr);
// prepare sin to tell bind to listen on any connection on given port
sin.sin6_family = family;
sin.sin6_addr = in6addr_any;
sin.sin6_port = htons(port); // choose port on which client could connect
sin.sin6_scope_id = 0;
// bind socket to interface
if (::bind(sock, (struct sockaddr*) &sin, sizeof(sin)) < 0)
{
perror("bind");
}
// listen for new connection
if (::listen(sock, SOMAXCONN) < 0)
{
perror("socket");
}
while (1)
{
fd_set conset;
FD_ZERO(&conset);
FD_SET(sockfd, &conset);
struct timeval timeout = {10, 0};
int maxfd = sockfd;
// wait for new client
select(maxfd + 1, &conset, NULL, NULL, &timeout);
if (FD_ISSET(sockfd, &conset))
{
// a new client is waiting
int connfd = ::accept(sockfd, &addr);
if (connfd < 0)
{
perror("accept");
}
else
{
// do thing with new client
}
}
else
{
printf("no new client in last 10 seconds")
}
}

Related

Non-blocking Linux server socket

I want to create a server socket that always prints to screen "tick" , and if one client sends data to this server, the server will print that data. I use non-blocking socket, but the it does not work, server prints to screen "tick" but cannot receives data from client.
Server
int main(int argc, char *argv[]) {
int server_sockfd, client_sockfd;
sockaddr_un server_address;
sockaddr_un client_address;
int client_len;
int res;
/* remove old socket and create unnamed socket */
unlink("server_socket");
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
/* non-blocking socket */
fcntl(server_sockfd, F_SETFL, O_NONBLOCK);
/* name the socket */
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "server_socket");
bind(server_sockfd, (sockaddr*)&server_address, sizeof(server_address));
/* listen client */
printf("server_waiting\n");
listen(server_sockfd, 5);
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (sockaddr*)&client_address, (socklen_t*)&client_len);
while(1) {
char ch;
res = recv(client_sockfd, &ch, 1, 0);
if (res == -1) {
printf("tick\n");
}
else {
printf("received: %c\n", ch);
}
}
}
client
int main(int argc, char *argv[]) {
int sock_fd;
struct sockaddr_un address;
int result;
char ch = 'A';
/* create socket for client */
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
/* name of socket as agreed with server */
address.sun_family = AF_UNIX;
strcpy(address.sun_path, "server_socket");
result = connect(sock_fd, (sockaddr*) &address, sizeof(address));
if (result == -1) {
perror("fail\n");
exit(1);
}
/* write via socket */
send(sock_fd, &ch, 1, 0);
close(sock_fd);
exit(0);
}
You are setting the listing socket to be non-blocking instead of the accepted socket.
Following your code logic, you DO want to wait on the accept call, but not the recv call
Instead of
/* non-blocking socket */
fcntl(server_sockfd, F_SETFL, O_NONBLOCK);
Delete it and instead add the fcntl call to the socket you are getting back from the accept call, like
client_sockfd = accept(....);
int flags = fcntl(client_sockfd, F_GETFL, 0);
fcntl(client_sockfd, F_SETFL, flags | O_NONBLOCK);
accept and fcntl can fail so you should check for failures in production code.

Does Linux AIO support RAW sockets?

I am struggling to get AIO working on Linux (version 3.19) for receiving packets on a RAW socket, but to no avail. I've successfully used AIO for UDP and TCP sockets, but can't make it work for a RAW socket. I've tried both IPv4 and IPv6.
Does anyone know if AIO supports RAW sockets?
Here's some code snippets from my application:
void readCallback(sigval_t sigval) {
debug(LOG_DEBUG, "RAW packet received\n");
}
int main(int argc, char *argv[]) {
int sock = socket(domain, SOCK_RAW, IPPROTO_RAW);
if (-1 == sock) {
debug(LOG_CRIT, "FAILED to create raw socket\n");
return 1;
}
char *iface = "eth0";
ifreq ifr;
memset (&ifr, 0, sizeof(ifr));
snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), "%s", iface);
if (ioctl (sock, SIOCGIFINDEX, &ifr) < 0) {
debug(LOG_CRIT, "FAILED to query interface '%s' index\n", iface);
return 1;
}
// Set flag so socket expects us to provide IP header.
const int on = 1;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
debug(LOG_CRIT, "FAILED to configure raw socket on '%s'\n", iface);
return 1;
}
// Bind socket to interface index.
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) {
debug(LOG_CRIT, "FAILED to bind socket to %s/%u\n", iface, ifr.ifr_ifindex);
return 1;
}
// listen for packets
struct aiocb aio;
char buf[MAX_PACKET];
bzero((char*)&aio, sizeof(struct aiocb));
aio.aio_fildes = sock;
aio.aio_buf = &buf;
aio.aio_nbytes = MAX_PACKET;
aio.aio_offset = 0;
aio.aio_sigevent.sigev_notify = SIGEV_THREAD;
aio.aio_sigevent.sigev_notify_function = readCallback;
aio.aio_sigevent.sigev_notify_attributes = NULL;
aio.aio_sigevent.sigev_value.sival_ptr = buf;
if (!RequestAioRead(&aio)) {
debug(LOG_DEBUG, "FAILED to listen on raw socket...\n");
return 1;
}
debug(LOG_DEBUG, "Listening on raw socket...\n");
// main loop
while (true) {
usleep(100000);
}
close(sock);
return 0;
}
Turns out my socket() protocol was wrong. The correct protocol seems to be htons(0x0800):
socket(AF_PACKET, SOCK_RAW, htons(0x0800));
With this, aio seems to work fine.

Understandng the reason for recv blocking forever

I run a Linux program written in C that would periodically receive data by parsing an HTTP response, crunch some numbers and then report the result by HTTP GET of another web page.
My problem is that sometimes, one of the instances would "freeze".
Looking at top I can see that it is in sk_wait_data state and attaching a debugger reveals that it is blocked by a recv call.
Here is a minimal version of the code that does the TCP connection (it was adapted from http://www.linuxhowtos.org/C_C++/socket.htm):
int connectTCP(const char* host, const char* page, int portno) {
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
// Create socket //
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
error("ERROR opening socket");
// Get ip from hostname //
server = gethostbyname(host);
if (server == NULL)
error("ERROR, can not find host\n");
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char *)&serv_addr.sin_addr.s_addr, // Destination
(char *)server->h_addr, // Source
server->h_length); // Size
serv_addr.sin_port = htons(portno);
// Conect to socket //
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
return sockfd;
}
char* httpGet(const char* host, const char* page, int portno) {
int sockfd, n;
sockfd = connectTCP(host, page, portno);
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", page, host);
n = send(sockfd,buffer,strlen(buffer), 0);
if (n < 0)
error("ERROR writing to socket");
int count = 0;
do {
n = recv(sockfd, buffer + count, BUFFER_MAX_SIZE - count, 0);
if (n < 0) {
error("ERROR reading from socket");
}
count += n;
} while(n != 0);
close(sockfd);
return buffer;
}
Bugs in your code:
If recv() returns zero you whould close the socket and stop reading.
If recv() returns -1 you should report the error, close the socket, and stop reading, unless you had set a read timeout and errno was EAGAIN/EWOULDBLOCK, in which case you should handle the timeout however is appropriate for your application.

Why select do not tell me that a client wants to connect?

I've made a simple tcp server that I can test with telnet program.
When running it on windows, it works as expected, when running it on linux, the behavior is strange:
telnet clients understand that they are connected to the server,
the server do not see clients (select return always 0),
when I kill the server, the clients detect the disconnection.
I think I missed something in accept, listen or select.
What did I missed?
Thanks.
Here's the program source:
#include "headers.h"
#define DEFAULT_PORT 24891
/**
* test_server [ip port]
*/
int main(int argc, char *argv[])
{
sockaddr_in sin;
socket_t sock;
/* listening socket creation */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sock)
{ die("socket()"); }
/* binding */
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(DEFAULT_PORT);
if (3 == argc)
{
sin.sin_addr.s_addr = inet_addr(argv[1]);
sin.sin_port = htons(strtol(argv[2], NULL, 0));
}
if (-1 == bind(sock, (sockaddr*) &sin, sizeof(sin)))
{ die("bind()"); }
/* Listening */
if (-1 == listen(sock, SOMAXCONN))
{ die("listen()"); }
while (1)
{
timeval timeout = { 1, 0 };
fd_set in_set;
FD_ZERO(&in_set);
FD_SET(sock, &in_set);
// select the set
int cnt = select(1, &in_set, NULL, NULL, &timeout);
if (cnt > 0)
{
// ask if an event occurs on listening socket
if (FD_ISSET(sock, &in_set))
{
/* a new client wants to connect */
socket_t csock = accept(sock, NULL, NULL);
send(csock, "hello\r\n", 7, 0);
printf("new client!\n");
close(csock);
}
}
else if (cnt < 0)
{ die("select"); }
}
/* closing listen socket */
close(sock);
printf("socket closed\n");
return 0;
}
You simply call select incorrectly. The first parameter needs to be the highest numbered fd from the fdset, plus one. See man page:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
....
nfds is the highest-numbered file descriptor in any of the three sets, plus 1.
The code may work, or may not, that depends on the fd returned by "socket()".
In your case the value of "nfds" needs to be "sock + 1", generally you need to track the highest numbered fd when doing a select on multiple fd's.

how to read/write from/to a SOCK_SEQPACKET socket?

I try to use a SOCK_SEQPACKET socket with this:
int rc, len;
int worker_sd, pass_sd;
char buffer[80];
struct iovec iov[1];
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
memset(iov, 0, sizeof(iov));
iov[0].iov_base = buffer;
iov[0].iov_len = sizeof(buffer);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if((socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
{
perror("server: socket");
exit -1;
}
memset(&server_address, 0, sizeof(server_address));
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "/mysocket");
unlink("/mysocket");
if(bind(socket_fd, (const struct sockaddr *) &server_address, sizeof(server_address)) < 0)
{
close(socket_fd);
perror("server: bind error");
return 1;
}
while(1)
{
printf("wait for message\n");
bytes_received = recvmsg(socket_fd, &msg, MSG_WAITALL);
printf("%d bytes\n", bytes_received);
}
The problem is that the process does not wait but receives -1 from recvmsg and loops forever. Nowhere in the manpages is there any reference what functions shall be used with SOCK_SEQPACKET-style sockets, for example I am not really sure whether recvmsg is even the correct function.
SOCK_SEQPACKET is connection-orientated so you must first accept a connection then do your IO on the accepted client socket.
recvmsg() returns -1 when an error has occured - errno will be set to the error number.
Read here: http://pubs.opengroup.org/onlinepubs/009695399/functions/recvmsg.html

Resources