Added posix socket implementation
[fmail.git] / src / posixsocket.cpp
blob76f123cc91ded4065971cd622131de0126b855dc
1 /*
2 libfmail: Socket abstraction, Posix Implementation
4 Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <malloc.h>
25 #include <sys/types.h>
26 #include <sys/uio.h>
27 #include <unistd.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32 #include <sys/ioctl.h>
34 #include <libfmail/socket.h>
36 /* TODO: Add proper Unix socket support
37 * TODO: Add Poll implementation
38 * */
39 class PosixSocket : public Socket{
40 private:
41 int sock;
42 int sock_port;
43 struct sockaddr_in *sock_addr;
44 char *sock_saddr;
45 struct in_addr sock_iaddr;
46 public:
47 PosixSocket(int socktype, int options){
48 int family;
50 if (socktype == SOCKET_INET){
51 family = AF_INET;
52 }else if (socktype == SOCKET_UNIX){
53 family = AF_UNIX;
56 sock = socket(family, SOCK_STREAM, 0);
57 sock_addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
58 sock_addr->sin_family = family;
61 PosixSocket(int s, struct sockaddr_in *addr){
62 sock = s;
63 sock_addr = addr;
66 void setPort(int port){
67 sock_port = port;
68 sock_addr->sin_port = htons(port);
71 int getPort(){
72 return sock_port;
75 void setAddress(char *addr){
76 if (inet_aton(addr, &sock_iaddr)){
77 sock_saddr = addr;
78 sock_addr->sin_addr.s_addr = sock_iaddr.s_addr;
82 char *getAddress(){
83 return sock_saddr;
86 int Connect(){
87 int slen, r;
89 slen = sizeof(struct sockaddr_in);
91 r = connect(sock, (struct sockaddr*)(sock_addr), slen);
92 return r;
95 int Bind(){
96 int slen;
98 slen = sizeof(struct sockaddr_in);
100 return bind(sock, (struct sockaddr*)sock_addr, slen);
103 int Listen(int queue){
104 int r;
105 r = listen(sock, queue);
106 return r;
109 int Write(const char *buffer, int len){
110 return write(sock, buffer, len);
113 int Read(char *buffer, int len){
114 return read(sock, buffer, len);
117 Socket *Accept(){
118 int client_socket;
119 struct sockaddr_in *client;
120 socklen_t clen;
121 Socket *s;
123 client = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
124 clen = sizeof(struct sockaddr_in);
126 client_socket = accept(sock, (struct sockaddr*)client, &clen);
127 s = new PosixSocket(client_socket, client);
129 return s;
132 int Close(){
133 return close(sock);
136 int Poll(int ms, int mode){
140 Socket *Socket::CreateSocket(int socktype, int options){
141 return new PosixSocket(socktype, options);