Use generic tcp_set_sockopt function for tcp setsockopt operations

This commit is contained in:
Peter Lieven
2012-10-20 18:54:46 +02:00
parent cbae6ae058
commit 881fc9743a

View File

@@ -58,13 +58,9 @@ static void set_nonblocking(int fd)
#endif #endif
} }
#ifndef TCP_USER_TIMEOUT int set_tcp_sockopt(int sockfd, int optname, int value)
#define TCP_USER_TIMEOUT 18
#endif
int set_tcp_user_timeout(struct iscsi_context *iscsi)
{ {
int level, value; int level;
#if defined(__FreeBSD__) || defined(__sun) #if defined(__FreeBSD__) || defined(__sun)
struct protoent *buf; struct protoent *buf;
@@ -77,12 +73,20 @@ int set_tcp_user_timeout(struct iscsi_context *iscsi)
level = SOL_TCP; level = SOL_TCP;
#endif #endif
value = iscsi->tcp_user_timeout; return setsockopt(sockfd, level, optname, &value, sizeof(value));
if (setsockopt(iscsi->fd, level, TCP_USER_TIMEOUT, &value, sizeof(value)) != 0) { }
#ifndef TCP_USER_TIMEOUT
#define TCP_USER_TIMEOUT 18
#endif
int set_tcp_user_timeout(struct iscsi_context *iscsi)
{
if (set_tcp_sockopt(iscsi->fd, TCP_USER_TIMEOUT, iscsi->tcp_user_timeout) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp user timeout. Error %s(%d)", strerror(errno), errno); iscsi_set_error(iscsi, "TCP: Failed to set tcp user timeout. Error %s(%d)", strerror(errno), errno);
return -1; return -1;
} }
DPRINTF(iscsi,3,"TCP_USER_TIMEOUT set to %d",value); DPRINTF(iscsi,3,"TCP_USER_TIMEOUT set to %d",iscsi->tcp_user_timeout);
return 0; return 0;
} }
@@ -92,25 +96,11 @@ int set_tcp_user_timeout(struct iscsi_context *iscsi)
int set_tcp_syncnt(struct iscsi_context *iscsi) int set_tcp_syncnt(struct iscsi_context *iscsi)
{ {
int level, value; if (set_tcp_sockopt(iscsi->fd, TCP_SYNCNT, iscsi->tcp_syncnt) != 0) {
#if defined(__FreeBSD__) || defined(__sun)
struct protoent *buf;
if ((buf = getprotobyname("tcp")) != NULL)
level = buf->p_proto;
else
return -1;
#else
level = SOL_TCP;
#endif
value = iscsi->tcp_syncnt;
if (setsockopt(iscsi->fd, level, TCP_SYNCNT, &value, sizeof(value)) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp syn retries. Error %s(%d)", strerror(errno), errno); iscsi_set_error(iscsi, "TCP: Failed to set tcp syn retries. Error %s(%d)", strerror(errno), errno);
return -1; return -1;
} }
DPRINTF(iscsi,3,"TCP_SYNCNT set to %d",value); DPRINTF(iscsi,3,"TCP_SYNCNT set to %d",iscsi->tcp_syncnt);
return 0; return 0;
} }
@@ -622,49 +612,34 @@ void iscsi_set_tcp_keepintvl(struct iscsi_context *iscsi, int value)
int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval) int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval)
{ {
int level, value;
#if defined(__FreeBSD__) || defined(__sun)
struct protoent *buf;
if ((buf = getprotobyname("tcp")) != NULL)
level = buf->p_proto;
else
return -1;
#else
level = SOL_TCP;
#endif
#ifdef SO_KEEPALIVE #ifdef SO_KEEPALIVE
value =1; int value = 1;
if (setsockopt(iscsi->fd, SOL_SOCKET, SO_KEEPALIVE, &value, sizeof(value)) != 0) { if (setsockopt(iscsi->fd, SOL_SOCKET, SO_KEEPALIVE, &value, sizeof(value)) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set socket option SO_KEEPALIVE. Error %s(%d)", strerror(errno), errno); iscsi_set_error(iscsi, "TCP: Failed to set socket option SO_KEEPALIVE. Error %s(%d)", strerror(errno), errno);
return -1; return -1;
} }
DPRINTF(iscsi,3,"SO_KEEPALIVE set to %d",value); DPRINTF(iscsi,3,"SO_KEEPALIVE set to %d",value);
#endif
#ifdef TCP_KEEPCNT #ifdef TCP_KEEPCNT
value = count; if (set_tcp_sockopt(iscsi->fd, TCP_KEEPCNT, count) != 0) {
if (setsockopt(iscsi->fd, level, TCP_KEEPCNT, &value, sizeof(value)) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive count. Error %s(%d)", strerror(errno), errno); iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive count. Error %s(%d)", strerror(errno), errno);
return -1; return -1;
} }
DPRINTF(iscsi,3,"TCP_KEEPCNT set to %d",value); DPRINTF(iscsi,3,"TCP_KEEPCNT set to %d",count);
#endif #endif
#ifdef TCP_KEEPINTVL #ifdef TCP_KEEPINTVL
value = interval; if (set_tcp_sockopt(iscsi->fd, TCP_KEEPINTVL, interval) != 0) {
if (setsockopt(iscsi->fd, level, TCP_KEEPINTVL, &value, sizeof(value)) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive interval. Error %s(%d)", strerror(errno), errno); iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive interval. Error %s(%d)", strerror(errno), errno);
return -1; return -1;
} }
DPRINTF(iscsi,3,"TCP_KEEPINTVL set to %d",value); DPRINTF(iscsi,3,"TCP_KEEPINTVL set to %d",interval);
#endif #endif
#ifdef TCP_KEEPIDLE #ifdef TCP_KEEPIDLE
value = idle; if (set_tcp_sockopt(iscsi->fd, TCP_KEEPIDLE, idle) != 0) {
if (setsockopt(iscsi->fd, level, TCP_KEEPIDLE, &value, sizeof(value)) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive idle. Error %s(%d)", strerror(errno), errno); iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive idle. Error %s(%d)", strerror(errno), errno);
return -1; return -1;
} }
DPRINTF(iscsi,3,"TCP_KEEPIDLE set to %d",value); DPRINTF(iscsi,3,"TCP_KEEPIDLE set to %d",idle);
#endif
#endif #endif
return 0; return 0;