/** * \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 . */ #ifndef __TR_SOCKET_H__ #define __TR_SOCKET_H__ #include #include #include #include //#include // 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; TR_CLASS(TR_Socket) { TR_Logger log; int flags; int type; char * host; char * cname; int port; time_t ttl; struct sockaddr_in addr; // for now... should be more generic. 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) #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_socketGetIp(socket) (TR_socketAddr(socket).sin_addr.s_addr) #define TR_socketGetIpStr(socket) (inet_ntoa(TR_socketGetIp(socket))) 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: