21 changed files with 887 additions and 249 deletions
-
1.gitignore
-
52include/tr/interface/socket.h
-
48include/tr/remote_data.h
-
85include/tr/socket.h
-
4include/trio.h
-
18src/Makefile.am
-
115src/i_socket.c
-
59src/listen.c
-
66src/remote_data.c
-
43src/remote_data_set_data.c
-
64src/socket.c
-
48src/socket_accept.c
-
14src/socket_close.c
-
103src/socket_init.c
-
2src/socket_nonblock.c
-
19src/socket_shutdown.c
-
32src/socket_shutdown_read.c
-
47src/socket_shutdown_write.c
-
121src/tcp_socket.c
-
102src/udp_socket.c
-
93trio.h
@ -0,0 +1,52 @@ |
|||
/** |
|||
* \file |
|||
* Inteface for common socket operations. (bind, send, recv) |
|||
* |
|||
* \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_INTERFACE_SOCKET_H__ |
|||
#define __TR_INTERFACE_SOCKET_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/remote_data.h" |
|||
|
|||
typedef int (* fptr_TR_socketBind)(void *); |
|||
typedef int (* fptr_TR_socketConnect)(void *); |
|||
typedef TR_RemoteData (* fptr_TR_socketRecv)(void *, size_t); |
|||
typedef ssize_t (* fptr_TR_socketSend)(void *, TR_RemoteData); |
|||
|
|||
TR_INTERFACE(TR_Socket) { |
|||
TR_IFID; |
|||
fptr_TR_socketBind bind; |
|||
fptr_TR_socketConnect connect; |
|||
fptr_TR_socketRecv recv; |
|||
fptr_TR_socketSend send; |
|||
}; |
|||
|
|||
extern int TR_socketBindAction(void *); |
|||
extern int TR_socketConnectAction(void *); |
|||
extern TR_RemoteData TR_socketRecv(void *, size_t); |
|||
extern ssize_t TR_socketSend(void *, TR_RemoteData); |
|||
|
|||
#endif // __TR_INTERFACE_SOCKET_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* \file |
|||
* Abstraction layer above BSD sockets. Capsules and simplifies connect |
|||
* accept and listen. |
|||
* |
|||
* \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_REMOTE_DATA_H__ |
|||
#define __TR_REMOTE_DATA_H__ |
|||
|
|||
#include <sys/types.h> |
|||
#include <sys/socket.h> |
|||
#include <netdb.h> |
|||
|
|||
#include "trbase.h" |
|||
|
|||
TR_CLASS(TR_RemoteData) { |
|||
unsigned char * data; |
|||
size_t ndata; |
|||
struct sockaddr_in addr; // for now... should be more generic |
|||
socklen_t addrlen; |
|||
}; |
|||
|
|||
extern TR_RemoteData TR_emptyRemoteData; |
|||
|
|||
void TR_remoteDataSetData(TR_RemoteData, unsigned char *, size_t); |
|||
|
|||
#endif // __TR_REMOTE_DATA_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
|
|||
@ -0,0 +1,115 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <errno.h> |
|||
|
|||
#include "tr/interface/socket.h" |
|||
#include "tr/remote_data.h" |
|||
#include "trbase.h" |
|||
|
|||
TR_CREATE_INTERFACE(TR_Socket, 4); |
|||
|
|||
int |
|||
TR_socketBindAction(void * _this) |
|||
{ |
|||
int callret; |
|||
|
|||
TR_RETCALL(_this, TR_Socket, bind, callret); |
|||
|
|||
return callret; |
|||
} |
|||
|
|||
int |
|||
TR_socketConnectAction(void * _this) |
|||
{ |
|||
int callret; |
|||
|
|||
TR_RETCALL(_this, TR_Socket, connect, callret); |
|||
|
|||
return callret; |
|||
} |
|||
|
|||
TR_RemoteData |
|||
TR_socketRecv(void * _this, size_t size) |
|||
{ |
|||
TR_RemoteData remote_data; |
|||
|
|||
TR_RETCALL(_this, TR_Socket, recv, remote_data, size); |
|||
|
|||
if (remote_data->ndata < 0) { |
|||
switch (errno) { |
|||
case (EAGAIN|EWOULDBLOCK): |
|||
TR_delete(remote_data); |
|||
return TR_emptyRemoteData; |
|||
|
|||
case EINTR: |
|||
case ENOMEM: |
|||
// these are fatal and should lead to a shutown |
|||
// of the whole application... |
|||
TR_delete(remote_data); |
|||
return NULL; |
|||
|
|||
default: |
|||
TR_delete(remote_data); |
|||
return NULL; |
|||
} |
|||
} else if (remote_data->ndata == 0) { |
|||
// this is a remote close... |
|||
TR_delete(remote_data); |
|||
return NULL; |
|||
} |
|||
|
|||
return remote_data; |
|||
} |
|||
|
|||
ssize_t |
|||
TR_socketSend(void * _this, TR_RemoteData data) |
|||
{ |
|||
ssize_t size; |
|||
|
|||
TR_RETCALL(_this, TR_Socket, send, size, data); |
|||
TR_delete(data); |
|||
|
|||
if (size < 0) { |
|||
switch (errno) { |
|||
case EINTR: |
|||
case ENOMEM: |
|||
// these are fatal and should lead to a shutown |
|||
// of the whole application... |
|||
return 0; |
|||
|
|||
case (EAGAIN|EWOULDBLOCK): |
|||
return -1; |
|||
|
|||
case ECONNRESET: |
|||
// this is a remote close... |
|||
return -2; |
|||
|
|||
default: |
|||
return -2; |
|||
} |
|||
} |
|||
|
|||
return size; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,59 +0,0 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <stdlib.h> // for atoi() and exit() |
|||
#include <errno.h> // for errno |
|||
|
|||
#include "tr/socket.h" |
|||
#include "tr/logger.h" |
|||
|
|||
|
|||
void |
|||
TR_socketListen(TR_Sock this, int backlog) |
|||
{ |
|||
(this->addr).sin_family = AF_INET; // Internet address family |
|||
(this->addr).sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface |
|||
//(this->addr).sin_addr.s_addr = inet_addr("127.0.0.1"); // Any incoming interface |
|||
(this->addr).sin_port = htons(this->port); // Local port |
|||
|
|||
/** |
|||
* Bind to the local address |
|||
*/ |
|||
if (-1 == bind(this->handle, (struct sockaddr *) &(this->addr), sizeof(this->addr))) { |
|||
TR_loggerLog(this->log, TR_LOGGER_CRIT, |
|||
"error binding socket: %s - service terminated", |
|||
strerror(errno)); |
|||
exit(EXIT_FAILURE); |
|||
} |
|||
|
|||
/** |
|||
* Mark the socket so it will listen for incoming connections |
|||
*/ |
|||
if (-1 == listen(this->handle, backlog)) { |
|||
TR_loggerLog(this->log, TR_LOGGER_CRIT, |
|||
"error binding socket: %s - service terminated", |
|||
strerror(errno)); |
|||
exit(EXIT_FAILURE); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,66 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <errno.h> |
|||
#include <stdlib.h> |
|||
#include <unistd.h> |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
|
|||
#include <sys/types.h> |
|||
#include <sys/socket.h> |
|||
#include <netdb.h> |
|||
|
|||
#include "tr/remote_data.h" |
|||
#include "trbase.h" |
|||
|
|||
static |
|||
int |
|||
remoteDataCtor(void * _this, va_list * params) |
|||
{ |
|||
TR_RemoteData this = _this; |
|||
struct sockaddr * addr_ptr = va_arg(*params, struct sockaddr *); |
|||
|
|||
this->addrlen = va_arg(*params, socklen_t); |
|||
memcpy(&(this->addr), addr_ptr, this->addrlen); |
|||
|
|||
this->data = NULL; |
|||
this->ndata = 0; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
remoteDataDtor(void * _this) |
|||
{ |
|||
TR_RemoteData this = _this; |
|||
|
|||
TR_MEM_FREE(this->data); |
|||
} |
|||
|
|||
TR_INIT_IFACE(TR_Class, remoteDataCtor, remoteDataDtor, NULL); |
|||
TR_CREATE_CLASS(TR_RemoteData, NULL, TR_IF(TR_Class)); |
|||
|
|||
TR_INSTANCE(TR_RemoteData, TR_emptyRemoteData, NULL, 0, NULL, NULL); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,43 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <string.h> // for atoi() and exit() |
|||
#include <errno.h> // for errno |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
|
|||
#include "tr/remote_data.h" |
|||
#include "tr/logger.h" |
|||
|
|||
void |
|||
TR_remoteDataSetData(TR_RemoteData this, unsigned char * data, size_t size) |
|||
{ |
|||
if (this->data && TR_getSize(this->data) < size) { |
|||
TR_MEM_FREE(this->data); |
|||
} |
|||
|
|||
this->data = TR_malloc(size); |
|||
this->ndata = size; |
|||
memcpy(this->data, data, size); |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,103 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#define _POSIX_SOURCE 1 |
|||
|
|||
#include <stdlib.h> // for atoi() and exit() |
|||
#include <errno.h> // for errno |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include <fcntl.h> |
|||
|
|||
#include <sys/types.h> |
|||
#include <sys/socket.h> |
|||
#include <netdb.h> |
|||
|
|||
#include "tr/socket.h" |
|||
#include "tr/logger.h" |
|||
|
|||
int |
|||
TR_socketInit(TR_Socket this, TR_socketAction_fptr action) |
|||
{ |
|||
struct addrinfo hint; |
|||
struct addrinfo * info, * current_info; |
|||
char port_str[6]; |
|||
|
|||
hint.ai_socktype = this->type; |
|||
hint.ai_flags = this->flags; |
|||
hint.ai_family = AF_UNSPEC; |
|||
hint.ai_protocol = 0; |
|||
hint.ai_addrlen = 0; |
|||
hint.ai_canonname = NULL; |
|||
hint.ai_addr = NULL; |
|||
hint.ai_next = NULL; |
|||
|
|||
sprintf(port_str, "%u", this->port); |
|||
if (0 != getaddrinfo(this->host, port_str, &hint, &info)) { |
|||
// TODO error handling... |
|||
return FALSE; |
|||
} |
|||
|
|||
current_info = info; |
|||
for ( |
|||
current_info = info; |
|||
current_info; |
|||
current_info = current_info->ai_next) |
|||
{ |
|||
this->handle = socket( |
|||
current_info->ai_family, |
|||
current_info->ai_socktype, |
|||
current_info->ai_protocol); |
|||
|
|||
if (-1 == this->handle) { |
|||
continue; |
|||
} |
|||
|
|||
this->addrlen = current_info->ai_addrlen; |
|||
memcpy(&(this->addr), current_info->ai_addr, this->addrlen); |
|||
|
|||
if (0 == action(this)) { |
|||
break; // success / open and bind or open and connect... |
|||
} |
|||
|
|||
close(this->handle); |
|||
this->handle = -1; |
|||
} |
|||
|
|||
if (NULL != current_info) { |
|||
int reUse = 1; //! \todo make this configurable |
|||
//int flags = fcntl(this->handle, F_GETFL, 0); |
|||
|
|||
//fcntl(this->handle, F_SETFL, flags | O_NONBLOCK); |
|||
this->cname = TR_strdup(current_info->ai_canonname); |
|||
this->fin_state = TR_FIN_NO; |
|||
|
|||
//! Make the socket REUSE a TIME_WAIT socket |
|||
setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof(reUse)); |
|||
} |
|||
|
|||
freeaddrinfo(info); |
|||
|
|||
return TRUE; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,47 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <stdlib.h> // for atoi() and exit() |
|||
#include <errno.h> // for errno |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
|
|||
#include "tr/socket.h" |
|||
#include "tr/logger.h" |
|||
|
|||
TR_SocketFin |
|||
TR_socketShutdownWrite(TR_Socket this) |
|||
{ |
|||
if (TR_socketFinWr(this)) { |
|||
return this->fin_state; |
|||
} |
|||
|
|||
if (0 == shutdown(this->handle, SHUT_WR)) { |
|||
this->fin_state |= TR_FIN_WR; |
|||
} else { |
|||
this->fin_state |= TR_FIN_RDWR; |
|||
} |
|||
|
|||
return this->fin_state; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,121 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <errno.h> |
|||
#include <stdlib.h> |
|||
#include <unistd.h> |
|||
#include <stdio.h> |
|||
|
|||
#include <sys/types.h> |
|||
#include <sys/socket.h> |
|||
#include <netdb.h> |
|||
|
|||
#include "tr/socket.h" |
|||
#include "tr/logger.h" |
|||
#include "trbase.h" |
|||
|
|||
static |
|||
int |
|||
tcpSocketCtor(void * _this, va_list * params) |
|||
{ |
|||
TR_TcpSocket this = _this; |
|||
|
|||
TR_PARENTCALL(_this, TR_Class, ctor, params); |
|||
|
|||
TR_socketType(this) = SOCK_STREAM; |
|||
this->listen = FALSE; |
|||
this->connected = FALSE; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
int |
|||
tcpSocketBind(void * _this) |
|||
{ |
|||
TR_TcpSocket this = _this; |
|||
int bind_ret; |
|||
|
|||
TR_PARENTRETCALL(_this, TR_Socket, bind, bind_ret); |
|||
|
|||
if (bind_ret != 0) { |
|||
return -1; |
|||
} |
|||
|
|||
if (listen(TR_socketHandle(this), 128) != 0) { |
|||
// error |
|||
return -1; |
|||
} |
|||
this->listen = TRUE; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
int |
|||
tcpSocketConnect(void * _this) |
|||
{ |
|||
TR_Socket this = _this; |
|||
|
|||
return connect( |
|||
this->handle, |
|||
(struct sockaddr *)&(this->addr), |
|||
this->addrlen); |
|||
} |
|||
|
|||
static |
|||
TR_RemoteData |
|||
tcpSocketRecv(TR_Socket this, size_t size) |
|||
{ |
|||
TR_RemoteData rdata = TR_new(TR_RemoteData, &(this->addr), this->addrlen); |
|||
unsigned char buffer[size]; |
|||
ssize_t received; |
|||
|
|||
received = recv(this->handle, buffer, size, this->flags); |
|||
|
|||
if (-1 == received) { |
|||
perror("recv"); |
|||
TR_delete(rdata); |
|||
} else { |
|||
TR_remoteDataSetData(rdata, buffer, received); |
|||
} |
|||
|
|||
return rdata; |
|||
} |
|||
|
|||
static |
|||
ssize_t |
|||
tcpSocketSend(TR_Socket this, TR_RemoteData data) |
|||
{ |
|||
return send(this->handle, data->data, data->ndata, this->flags); |
|||
} |
|||
|
|||
TR_INIT_IFACE(TR_Class, tcpSocketCtor, NULL, NULL); |
|||
TR_INIT_IFACE( |
|||
TR_Socket, |
|||
tcpSocketBind, |
|||
tcpSocketConnect, |
|||
tcpSocketRecv, |
|||
tcpSocketSend); |
|||
TR_CREATE_CLASS(TR_TcpSocket, TR_Socket, TR_IF(TR_Class), TR_IF(TR_Socket)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,102 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \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/>. |
|||
*/ |
|||
|
|||
#include <errno.h> |
|||
#include <stdlib.h> |
|||
#include <unistd.h> |
|||
#include <stdio.h> |
|||
|
|||
#include <sys/types.h> |
|||
#include <sys/socket.h> |
|||
#include <netdb.h> |
|||
|
|||
#include "tr/socket.h" |
|||
#include "tr/logger.h" |
|||
#include "trbase.h" |
|||
|
|||
static |
|||
int |
|||
udpSocketCtor(void * _this, va_list * params) |
|||
{ |
|||
TR_Socket this = _this; |
|||
|
|||
TR_PARENTCALL(_this, TR_Class, ctor, params); |
|||
|
|||
this->type = SOCK_DGRAM; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
TR_RemoteData |
|||
udpSocketRecv(TR_Socket this, size_t size) |
|||
{ |
|||
TR_RemoteData rdata; |
|||
struct sockaddr_in addr; |
|||
socklen_t addrlen = this->addrlen; |
|||
unsigned char buffer[size]; |
|||
ssize_t received; |
|||
|
|||
received = recvfrom( |
|||
this->handle, |
|||
buffer, |
|||
size, |
|||
this->flags, |
|||
(struct sockaddr *)&addr, |
|||
&addrlen); |
|||
|
|||
if (-1 == received) { |
|||
perror("recvfrom"); |
|||
} else { |
|||
rdata = TR_new(TR_RemoteData, &addr, addrlen); |
|||
TR_remoteDataSetData(rdata, buffer, received); |
|||
} |
|||
|
|||
return rdata; |
|||
} |
|||
|
|||
static |
|||
ssize_t |
|||
udpSocketSend(TR_Socket this, TR_RemoteData data) |
|||
{ |
|||
ssize_t send; |
|||
|
|||
send = sendto( |
|||
this->handle, |
|||
data->data, |
|||
data->ndata, |
|||
this->flags, |
|||
(struct sockaddr *)&(data->addr), |
|||
data->addrlen); |
|||
|
|||
if (-1 == send) { |
|||
perror("sendto"); |
|||
} |
|||
|
|||
return send; |
|||
} |
|||
|
|||
TR_INIT_IFACE(TR_Class, udpSocketCtor, NULL, NULL); |
|||
TR_INIT_IFACE(TR_Socket, NULL, NULL, udpSocketRecv, udpSocketSend); |
|||
TR_CREATE_CLASS(TR_UdpSocket, TR_Socket, TR_IF(TR_Class), TR_IF(TR_Socket)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,93 +0,0 @@ |
|||
/* trio.h. Generated from trio.h.in by configure. */ |
|||
/* trio.h.in. Generated from configure.ac by autoheader. */ |
|||
|
|||
/* Define to 1 if you have the <dlfcn.h> header file. */ |
|||
#define HAVE_DLFCN_H 1 |
|||
|
|||
/* Define to 1 if you have the <inttypes.h> header file. */ |
|||
#define HAVE_INTTYPES_H 1 |
|||
|
|||
/* Define to 1 if you have the <memory.h> header file. */ |
|||
#define HAVE_MEMORY_H 1 |
|||
|
|||
/* Define to 1 if you have the `memset' function. */ |
|||
/* #undef HAVE_MEMSET */ |
|||
|
|||
/* Define to 1 if you have the <stdarg.h> header file. */ |
|||
#define HAVE_STDARG_H 1 |
|||
|
|||
/* Define to 1 if stdbool.h conforms to C99. */ |
|||
/* #undef HAVE_STDBOOL_H */ |
|||
|
|||
/* Define to 1 if you have the <stdint.h> header file. */ |
|||
#define HAVE_STDINT_H 1 |
|||
|
|||
/* Define to 1 if you have the <stdio.h> header file. */ |
|||
#define HAVE_STDIO_H 1 |
|||
|
|||
/* Define to 1 if you have the <stdlib.h> header file. */ |
|||
#define HAVE_STDLIB_H 1 |
|||
|
|||
/* Define to 1 if you have the <strings.h> header file. */ |
|||
#define HAVE_STRINGS_H 1 |
|||
|
|||
/* Define to 1 if you have the <string.h> header file. */ |
|||
#define HAVE_STRING_H 1 |
|||
|
|||
/* Define to 1 if you have the <syslog.h> header file. */ |
|||
#define HAVE_SYSLOG_H 1 |
|||
|
|||
/* Define to 1 if you have the <sys/stat.h> header file. */ |
|||
#define HAVE_SYS_STAT_H 1 |
|||
|
|||
/* Define to 1 if you have the <sys/types.h> header file. */ |
|||
#define HAVE_SYS_TYPES_H 1 |
|||
|
|||
/* Define to 1 if you have the <unistd.h> header file. */ |
|||
#define HAVE_UNISTD_H 1 |
|||
|
|||
/* Define to 1 if the system has the type `_Bool'. */ |
|||
#define HAVE__BOOL 1 |
|||
|
|||
/* Define to the sub-directory in which libtool stores uninstalled libraries. |
|||
*/ |
|||
#define LT_OBJDIR ".libs/" |
|||
|
|||
/* Name of package */ |
|||
#define PACKAGE "libtrio" |
|||
|
|||
/* Define to the address where bug reports for this package should be sent. */ |
|||
#define PACKAGE_BUGREPORT "Georg Hopp <georg@steffers.org>" |
|||
|
|||
/* Define to the full name of this package. */ |
|||
#define PACKAGE_NAME "libtrio" |
|||
|
|||
/* Define to the full name and version of this package. */ |
|||
#define PACKAGE_STRING "libtrio 0.0.0" |
|||
|
|||
/* Define to the one symbol short name of this package. */ |
|||
#define PACKAGE_TARNAME "libtrio" |
|||
|
|||
/* Define to the home page for this package. */ |
|||
#define PACKAGE_URL "" |
|||
|
|||
/* Define to the version of this package. */ |
|||
#define PACKAGE_VERSION "0.0.0" |
|||
|
|||
/* Define to 1 if you have the ANSI C header files. */ |
|||
#define STDC_HEADERS 1 |
|||
|
|||
/* Version number of package */ |
|||
#define VERSION "0.0.0" |
|||
|
|||
/* Define to `__inline__' or `__inline' if that's what the C compiler |
|||
calls it, or to nothing if 'inline' is not supported under any name. */ |
|||
#ifndef __cplusplus |
|||
/* #undef inline */ |
|||
#endif |
|||
|
|||
/* Define to `int' if <sys/types.h> does not define. */ |
|||
/* #undef pid_t */ |
|||
|
|||
/* Define to `unsigned int' if <sys/types.h> does not define. */ |
|||
/* #undef size_t */ |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue