Simple tools for networking / objects in plain C Snapshot
Typedefs | Enumerations | Enumerator | Functions
IOUTILS

functions for setting of common socket options and doing stuff with sockets. More...

Typedefs

typedef enum _Buffsize_op Buffsize_op

Enumerations

enum  _Buffsize_op { Receive_buffer, Send_buffer }

Functions

int disable_sigpipe ()
 disable SIG_PIPE signal, checks for static flag so as not to call system call again and again.
int fd_set_blocking (int fd, int is_blocking)
 set mode to blocking / non blocking.
int fd_get_bytes_available (int fd)
 return bytes available for reading
int fd_get_error (int fd)
 return socket error.
int fd_set_buf_size (int fd, Buffsize_op op, int size)
 set receive or send buffer size
int fd_get_buf_size (int fd, Buffsize_op op)
 return the receive or send buffer size
int fd_set_reuse_address (int fd, int reuse_on)
 set reuse address option
int fd_get_reuse_address (int fd)
 get reuse address option
int fd_set_nagling (int fd, int on)
 set naggle algorithm (tcp no delay option)
int fd_set_linger_option (int fd, int on, int linger_value)
 set linger option
int fd_close_by_RST (int fd)
 ungracefull connection termination - send RST to peer;
int fd_make_tcp_listener (SOCKADDR *saddr, int backlog)
 make socket for accepting connections (listening endpoint), with SO_REUSE_ADDRESS and given backlog length.

Detailed Description

functions for setting of common socket options and doing stuff with sockets.


Typedef Documentation

typedef enum _Buffsize_op Buffsize_op

Enumeration Type Documentation

Enumerator:
Receive_buffer 
Send_buffer 

Definition at line 35 of file ioutils.h.


Function Documentation

int disable_sigpipe ( )

disable SIG_PIPE signal, checks for static flag so as not to call system call again and again.

Definition at line 12 of file ioutils.c.

{
  //if (!has_disabled_sigpipe) {
    if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) {
      return -1;
    }
  //has_disabled_sigpipe = 1;
  //}

  return 0;
}
int fd_close_by_RST ( int  fd)

ungracefull connection termination - send RST to peer;

Definition at line 134 of file ioutils.c.

{
  fd_set_linger_option( fd, 1, 0 );
  return close(fd);
}
int fd_get_buf_size ( int  fd,
Buffsize_op  op 
)

return the receive or send buffer size

Definition at line 75 of file ioutils.c.

{
  int size;
  socklen_t ln = sizeof(size);
        
  if (getsockopt(fd,
                 SOL_SOCKET,
                 op == Send_buffer ? SO_SNDBUF : SO_RCVBUF ,
                 (char *) &size,
                 &ln)  != 0) {
    return -1;
  }
  return size;
        
}
int fd_get_bytes_available ( int  fd)

return bytes available for reading

Definition at line 37 of file ioutils.c.

{
  int ret = 0;

  ioctl( fd, FIONREAD,  &ret );
        
  return ret;
}
int fd_get_error ( int  fd)

return socket error.

Definition at line 46 of file ioutils.c.

{
  int err = 0;

  socklen_t sz = sizeof(err);

  if (getsockopt( fd, SOL_SOCKET, SO_ERROR, (char *) &err, &sz ) != 0) {
    return -1;
  }
        
  return err;
}
int fd_get_reuse_address ( int  fd)

get reuse address option

Definition at line 103 of file ioutils.c.

{
  int flag_on;
  socklen_t sz = sizeof(flag_on);

  if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR , (char *) &flag_on, &sz)  != 0) {

    return -1;
  }  
  return flag_on;
}
int fd_make_tcp_listener ( SOCKADDR saddr,
int  backlog 
)

make socket for accepting connections (listening endpoint), with SO_REUSE_ADDRESS and given backlog length.

Definition at line 141 of file ioutils.c.

{
  int fd;
 
  fd = socket( SOCKADDR_family(saddr), SOCK_STREAM, IPPROTO_TCP );
  if (fd == -1) {
    return -1;
  }

  if (fd_set_reuse_address( fd, 1 )) {
    goto err;
  }

  if (bind( fd, SOCKADDR_saddr( saddr ), SOCKADDR_length( saddr )) ) {
    goto err;
  }

  if (listen( fd, backlog )) {
    goto err;
  }
  return fd;

err:
  close(fd);
  return -1;
}
int fd_set_blocking ( int  fd,
int  is_blocking 
)

set mode to blocking / non blocking.

Definition at line 24 of file ioutils.c.

{
  unsigned long cmd = is_blocking ? 0 : 1;
  int res;

#ifdef _WIN32
  res = ioctlsocket( fd, FIONBIO, &cmd);
#else    
  res = ioctl( fd, FIONBIO, &cmd);
#endif
  return res;
}
int fd_set_buf_size ( int  fd,
Buffsize_op  op,
int  size 
)

set receive or send buffer size

Definition at line 59 of file ioutils.c.

{
  int rt;

  rt = setsockopt( fd,
                    SOL_SOCKET,
                    op == Send_buffer ? SO_SNDBUF : SO_RCVBUF,
                    (const char *) &size,
                    sizeof(int));
  if (rt != 0) {
     return rt;
  }
  return 0;
        
}
int fd_set_linger_option ( int  fd,
int  on,
int  linger_value 
)

set linger option

Definition at line 124 of file ioutils.c.

{
  struct linger l;
   
  l.l_onoff = on ? 1 : 0; 
  l.l_linger = linger_value;

  return setsockopt( fd, SOL_SOCKET, SO_LINGER, &l, sizeof(struct linger) );
}
int fd_set_nagling ( int  fd,
int  on 
)

set naggle algorithm (tcp no delay option)

Definition at line 116 of file ioutils.c.

{
  if (on) {
    on = 1;
  }
  return setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on) );
}
int fd_set_reuse_address ( int  fd,
int  reuse_on 
)

set reuse address option

Definition at line 91 of file ioutils.c.

{
  int flag_on = reuse_on ? 1 : 0;
  int rt;

  rt = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*) &flag_on, sizeof(flag_on)); 
  if (rt != 0) {
    return -1;
  }
  return 0;  
}