From 41b0b5b9d9b13abb09779789ef243722e0dff07b Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Mon, 23 Jul 2007 01:42:18 -0700 Subject: [PATCH] Added posix socket implementation --- src/posixsocket.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/posixsocket.cpp diff --git a/src/posixsocket.cpp b/src/posixsocket.cpp new file mode 100644 index 0000000..76f123c --- /dev/null +++ b/src/posixsocket.cpp @@ -0,0 +1,143 @@ +/* +libfmail: Socket abstraction, Posix Implementation + +Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela + +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 2 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, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* TODO: Add proper Unix socket support + * TODO: Add Poll implementation + * */ +class PosixSocket : public Socket{ +private: + int sock; + int sock_port; + struct sockaddr_in *sock_addr; + char *sock_saddr; + struct in_addr sock_iaddr; +public: + PosixSocket(int socktype, int options){ + int family; + + if (socktype == SOCKET_INET){ + family = AF_INET; + }else if (socktype == SOCKET_UNIX){ + family = AF_UNIX; + } + + sock = socket(family, SOCK_STREAM, 0); + sock_addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in)); + sock_addr->sin_family = family; + } + + PosixSocket(int s, struct sockaddr_in *addr){ + sock = s; + sock_addr = addr; + } + + void setPort(int port){ + sock_port = port; + sock_addr->sin_port = htons(port); + } + + int getPort(){ + return sock_port; + } + + void setAddress(char *addr){ + if (inet_aton(addr, &sock_iaddr)){ + sock_saddr = addr; + sock_addr->sin_addr.s_addr = sock_iaddr.s_addr; + } + } + + char *getAddress(){ + return sock_saddr; + } + + int Connect(){ + int slen, r; + + slen = sizeof(struct sockaddr_in); + + r = connect(sock, (struct sockaddr*)(sock_addr), slen); + return r; + } + + int Bind(){ + int slen; + + slen = sizeof(struct sockaddr_in); + + return bind(sock, (struct sockaddr*)sock_addr, slen); + } + + int Listen(int queue){ + int r; + r = listen(sock, queue); + return r; + } + + int Write(const char *buffer, int len){ + return write(sock, buffer, len); + } + + int Read(char *buffer, int len){ + return read(sock, buffer, len); + } + + Socket *Accept(){ + int client_socket; + struct sockaddr_in *client; + socklen_t clen; + Socket *s; + + client = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in)); + clen = sizeof(struct sockaddr_in); + + client_socket = accept(sock, (struct sockaddr*)client, &clen); + s = new PosixSocket(client_socket, client); + + return s; + } + + int Close(){ + return close(sock); + } + + int Poll(int ms, int mode){ + } +}; + +Socket *Socket::CreateSocket(int socktype, int options){ + return new PosixSocket(socktype, options); +} + -- 2.11.4.GIT