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.
25 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <sys/socket.h>
32 #include <sys/ioctl.h>
34 #include <libfmail/socket.h>
36 /* TODO: Add proper Unix socket support
37 * TODO: Add Poll implementation
39 class PosixSocket
: public Socket
{
43 struct sockaddr_in
*sock_addr
;
45 struct in_addr sock_iaddr
;
47 PosixSocket(int socktype
, int options
){
50 if (socktype
== SOCKET_INET
){
52 }else if (socktype
== SOCKET_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
){
66 void setPort(int port
){
68 sock_addr
->sin_port
= htons(port
);
75 void setAddress(char *addr
){
76 if (inet_aton(addr
, &sock_iaddr
)){
78 sock_addr
->sin_addr
.s_addr
= sock_iaddr
.s_addr
;
89 slen
= sizeof(struct sockaddr_in
);
91 r
= connect(sock
, (struct sockaddr
*)(sock_addr
), slen
);
98 slen
= sizeof(struct sockaddr_in
);
100 return bind(sock
, (struct sockaddr
*)sock_addr
, slen
);
103 int Listen(int queue
){
105 r
= listen(sock
, queue
);
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
);
119 struct sockaddr_in
*client
;
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
);
136 int Poll(int ms
, int mode
){
140 Socket
*Socket::CreateSocket(int socktype
, int options
){
141 return new PosixSocket(socktype
, options
);