Add fd_utils
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#ifndef FD_UTILS_H_
|
||||
#define FD_UTILS_H_
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
// inspect errno if any of these fails
|
||||
|
||||
/**
|
||||
* set the file descriptor to non-blocking
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_nonblocking(int fd);
|
||||
|
||||
/**
|
||||
* set the file descriptor to blocking
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_blocking(int fd);
|
||||
|
||||
/**
|
||||
* set the socket file descriptor to SO_REUSEADDR
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_reuseaddr(int sockfd);
|
||||
|
||||
/**
|
||||
* enable TCP keepalive on the socket
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_tcp_keepalive(int sockfd);
|
||||
|
||||
/**
|
||||
* Set the socket send timeout, in miliseconds
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_send_timeout(int sockfd,long timeoutms);
|
||||
|
||||
/**
|
||||
* Set the socket receive timeout, in miliseconds
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_receive_timeout(int sockfd,long timeoutms);
|
||||
|
||||
/*
|
||||
* Set the IP DSCP (differnetiated services code point) header value
|
||||
* The dscp must be < 64
|
||||
* @param fd file descriptor
|
||||
* @return 0 on success -1 on failure
|
||||
*/
|
||||
int set_ip_dscp(int fd, unsigned int dscp);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
#endif /* FD_UTILS_H_ */
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include "ucore/fd_utils.h"
|
||||
|
||||
int 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 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 set_reuseaddr(int sockfd)
|
||||
{
|
||||
int opt = 1;
|
||||
return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
||||
|
||||
}
|
||||
|
||||
int set_tcp_keepalive(int sockfd)
|
||||
{
|
||||
int optval = 1;
|
||||
return setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
|
||||
}
|
||||
|
||||
|
||||
int 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 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
|
||||
set_ip_dscp(int fd, unsigned int dscp) {
|
||||
int val;
|
||||
|
||||
if (dscp > 63) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
val = dscp << 2;
|
||||
return setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user