Files
libucore/src/fd_utils.c
T
Nils O. Selåsdal b24f27ec9f Mask the upper bits of dscp instead of failing if the user
supplied a too big value
2013-12-04 23:21:30 +01:00

97 lines
2.1 KiB
C

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "ucore/fd_utils.h"
int uc_set_nonblocking(int fd)
{
int flags = fcntl(fd,F_GETFL,NULL);
if(flags < 0 ) {
return flags;
}
return fcntl(fd,F_SETFL,flags | O_NONBLOCK);
}
int uc_set_blocking(int fd)
{
int flags = fcntl(fd,F_GETFL,NULL);
if(flags < 0 ) {
return flags;
}
return fcntl(fd,F_SETFL,flags&~O_NONBLOCK);
}
int uc_set_reuseaddr(int sockfd)
{
int opt = 1;
return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
}
int uc_set_tcp_keepalive(int sockfd)
{
int optval = 1;
return setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
}
int uc_set_tcp_keepalive_cfg(int sockfd, const struct UCKeepConfig *cfg)
{
int rc;
//first turn on keepalive
rc = uc_set_tcp_keepalive(sockfd);
if (rc != 0) {
return rc;
}
//set the keepalive options
rc = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, &cfg->keepcnt, sizeof cfg->keepcnt);
if (rc != 0) {
return rc;
}
rc = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &cfg->keepidle, sizeof cfg->keepidle);
if (rc != 0) {
return rc;
}
rc = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &cfg->keepintvl, sizeof cfg->keepintvl);
if (rc != 0) {
return rc;
}
return 0;
}
int uc_set_send_timeout(int sockfd,long timeoutms)
{
struct timeval so_sndtimeo = {timeoutms/1000,(timeoutms%1000)*1000};
return setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &so_sndtimeo, sizeof(so_sndtimeo));
}
int uc_set_receive_timeout(int sockfd,long timeoutms)
{
struct timeval so_sndtimeo = {timeoutms/1000,(timeoutms%1000)*1000};
return setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &so_sndtimeo, sizeof(so_sndtimeo));
}
int uc_set_ip_dscp(int fd, unsigned int dscp)
{
int val;
//only 6 bits for dscp
val = (dscp & 0x3f) << 2;
return setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val);
}