Socket communication classes for the taskrambler framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

153 lines
4.9 KiB

/**
* \file
* Abstraction layer above BSD sockets. Capsules and simplifies connect
* accept and listen for TCP and UDP sockets.
* It also provides a mostly unified interface to both types of sockets.
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_SOCKET_H__
#define __TR_SOCKET_H__
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
//#include <arpa/inet.h> // for in_port_t
#include "trbase.h"
//#include "tr/interface/socket.h"
#include "tr/logger.h"
typedef enum TR_e_socket_fin {
TR_FIN_NO = 0,
TR_FIN_RD = 1,
TR_FIN_WR = 2,
TR_FIN_RDWR = 3
} TR_SocketFin;
#define TR_MAX_HOST 256
#define TR_MAX_CNAME 512
TR_CLASS(TR_Socket) {
TR_Logger log;
int flags;
int type;
char host[TR_MAX_HOST];
char cname[TR_MAX_CNAME];
int port;
time_t ttl;
union {
struct sockaddr info;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr;
socklen_t addrlen;
int handle;
TR_SocketFin fin_state;
};
TR_INSTANCE_INIT(TR_Socket);
#define TR_socketLog(socket) (((TR_Socket)(socket))->log)
#define TR_socketFlags(socket) (((TR_Socket)(socket))->flags)
#define TR_socketType(socket) (((TR_Socket)(socket))->type)
#define TR_socketHost(socket) (((TR_Socket)(socket))->host)
#define TR_socketPort(socket) (((TR_Socket)(socket))->port)
#define TR_socketCname(socket) (((TR_Socket)(socket))->cname)
#define TR_socketTtl(socket) (((TR_Socket)(socket))->ttl)
#define TR_socketAddr(socket) \
(((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
? ((TR_Socket)(socket))->addr.in \
: ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
? ((TR_Socket)(socket))->addr.in6 \
: NULL)
#define TR_socketAddrlen(socket) (((TR_Socket)(socket))->addrlen)
#define TR_socketHandle(socket) (((TR_Socket)(socket))->handle)
#define TR_socketFinState(socket) (((TR_Socket)(socket))->fin_state)
#define TR_socketFinRd(socket) ((TR_socketFinState((socket)) & 1) == 1)
#define TR_socketFinWr(socket) ((TR_socketFinState((socket)) & 2) == 2)
#define TR_socketFinRdWr(socket) ((TR_socketFinState((socket)) & 3) == 3)
#define TR_socketAddrPort(socket) \
(((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
? ((TR_Socket)(socket))->addr.in.sin_port \
: ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
? ((TR_Socket)(socket))->addr.in6.sin6_port \
: -1)
#define TR_socketAddrIp(socket) \
(((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
? ((TR_Socket)(socket))->addr.in.sin_addr.s_addr \
: ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
? ((TR_Socket)(socket))->addr.in6.sin6_addr.s6_addr \
: NULL)
#define TR_socketAddrIpStr(socket, buffer, nbuffer) \
(((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
? inet_ntop( \
((TR_Socket)(socket))->addr.info.sa_family, \
&((TR_Socket)(socket))->addr.in.sin_addr.s_addr, \
buffer, nbuffer) \
: ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
? inet_ntop( \
((TR_Socket)(socket))->addr.info.sa_family, \
((TR_Socket)(socket))->addr.in6.sin6_addr.s6_addr, \
buffer, nbuffer) \
: NULL)
TR_CLASS(TR_TcpSocket) {
TR_EXTENDS(TR_Socket);
int listen;
int connected;
};
TR_INSTANCE_INIT(TR_TcpSocket);
TR_CLASS(TR_UdpSocket) {
TR_EXTENDS(TR_Socket);
};
TR_INSTANCE_INIT(TR_UdpSocket);
typedef int (* TR_socketAction_fptr)(void *);
int TR_socketInit(TR_Socket, TR_socketAction_fptr);
TR_SocketFin TR_socketShutdownRead(TR_Socket);
TR_SocketFin TR_socketShutdownWrite(TR_Socket);
TR_SocketFin TR_socketShutdown(TR_Socket);
void TR_socketClose(TR_Socket);
void TR_socketNonblock(TR_Socket);
#define TR_socketBind(socket) \
(TR_socketInit((socket), TR_socketBindAction))
TR_TcpSocket TR_socketAccept(TR_TcpSocket);
#define TR_socketConnect(socket) \
(TR_socketInit((socket), TR_socketConnectAction))
#endif // __TR_SOCKET_H__
// vim: set ts=4 sw=4: