Partially implemented server mode

This commit is contained in:
Pierre Pronchery 2012-10-18 00:06:19 +00:00
parent d57af2744b
commit 09a113a3b1

View File

@ -15,6 +15,12 @@
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <System.h>
#include "App/apptransport.h"
@ -38,6 +44,12 @@ static TCP * _tcp_init(AppTransportPluginHelper * helper,
AppTransportMode mode, char const * name);
static void _tcp_destroy(TCP * tcp);
/* useful */
static int _tcp_error(char const * message, int code);
/* callbacks */
static int _tcp_callback_accept(int fd, TCP * tcp);
/* public */
/* constants */
@ -56,23 +68,95 @@ AppTransportPluginDefinition definition =
/* functions */
/* plug-in */
/* tcp_init */
static int _init_client(TCP * tcp, char const * name);
static int _init_server(TCP * tcp, char const * name);
static TCP * _tcp_init(AppTransportPluginHelper * helper,
AppTransportMode mode, char const * name)
{
TCP * tcp;
int res = -1;
if((tcp = object_new(sizeof(*tcp))) == NULL)
return NULL;
tcp->helper = helper;
tcp->fd = -1;
/* FIXME really implement */
switch(mode)
{
case ATM_CLIENT:
res = _init_client(tcp, name);
break;
case ATM_SERVER:
res = _init_server(tcp, name);
break;
}
/* check for errors */
if(res != 0)
{
_tcp_destroy(tcp);
return NULL;
}
return tcp;
}
static int _init_client(TCP * tcp, char const * name)
{
if((tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -_tcp_error("socket", 1);
/* FIXME really implement */
return -1;
}
static int _init_server(TCP * tcp, char const * name)
{
struct sockaddr_in sa;
if((tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -_tcp_error("socket", 1);
sa.sin_family = AF_INET;
sa.sin_port = htons(4241); /* XXX hard-coded */
sa.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(tcp->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0)
return -_tcp_error("bind", 1);
if(listen(tcp->fd, 5) != 0)
return -_tcp_error("listen", 1);
event_register_io_read(tcp->helper->event, tcp->fd,
_tcp_callback_accept, tcp);
/* FIXME implement */
return -1;
}
/* tcp_destroy */
static void _tcp_destroy(TCP * tcp)
{
/* FIXME really implement */
if(tcp->fd >= 0)
close(tcp->fd);
object_delete(tcp);
}
/* useful */
/* tcp_error */
static int _tcp_error(char const * message, int code)
{
return error_set_code(code, "%s%s%s", (message != NULL) ? message : "",
(message != NULL) ? ": " : "", strerror(errno));
}
/* callbacks */
/* tcp_callback_accept */
static int _tcp_callback_accept(int fd, TCP * tcp)
{
struct sockaddr_in sa;
socklen_t sa_size = sizeof(sa);
int newfd;
if((newfd = accept(fd, (struct sockaddr *)&sa, &sa_size)) < 0)
return _tcp_error("accept", 1);
/* FIXME really implement */
close(newfd);
return 0;
}