1 #include <sys/socket.h>
  2 #include <string.h>
  3 #include <errno.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <stdio.h>
  7 #include <netinet/tcp.h>
  8 #include "rh_socket.h"
  9 
 10 #define RH_SOCKET_REQUIRE(_s)                   \
 11         if (NULL == (_s) || (_s)->fd < 0) {     \
 12                 errno = EINVAL;                 \
 13                 return -1;                      \
 14         }
 15 
 16 void rh_socket_init (rh_socket_t *s)
 17 {
 18         memset (s, 0, sizeof(*s));
 19         s->fd = -1;
 20 }
 21 
 22 int rh_socket_create (rh_socket_t *s, int domain, int type, int protocol)
 23 {
 24         if (NULL == s) {
 25                 errno = EINVAL;
 26                 return -1;
 27         }
 28 
 29         memset (s, 0, sizeof(*s));
 30         
 31         s->fd = socket (domain, type, protocol);
 32         s->addr.sin_family = domain;
 33 
 34         return (s->fd == -1 ? -1 : 0);
 35 }
 36 
 37 int rh_socket_close (rh_socket_t *s)
 38 {
 39         int success;
 40 
 41         RH_SOCKET_REQUIRE(s);
 42         
 43         rh_event_del (&s->event_write);
 44         rh_event_del (&s->event_read);
 45 
 46         success = close(s->fd);
 47 
 48         memset (s, 0, sizeof(*s));
 49         s->fd = -1;
 50         
 51         return success;
 52 }
 53 
 54 int rh_socket_listen (rh_socket_t *s, int backlog)
 55 {
 56         RH_SOCKET_REQUIRE(s);
 57 
 58         return listen (s->fd, backlog);
 59 }
 60 
 61 int rh_socket_bind (rh_socket_t *s)
 62 {
 63         RH_SOCKET_REQUIRE(s);
 64         
 65         return bind (   s->fd,
 66                         (void*)&s->addr,
 67                         sizeof(s->addr));
 68 }
 69 
 70 int rh_socket_set_addr (rh_socket_t *s, unsigned short port)
 71 {
 72         RH_SOCKET_REQUIRE(s);
 73 
 74         s->addr.sin_port = htons(port);
 75         
 76         return 0;
 77 }
 78                         
 79 
 80 int rh_socket_accept (rh_socket_t *server, rh_socket_t *client)
 81 {
 82         socklen_t       addr_len;
 83         
 84         RH_SOCKET_REQUIRE(server);
 85         
 86         if (NULL == client) {
 87                 errno = EINVAL;
 88                 return -1;
 89         }
 90 
 91         memset (client, 0, sizeof(*client));
 92 
 93         addr_len = sizeof(client->addr);
 94         
 95         client->fd = accept (   server->fd,
 96                                 (struct sockaddr*)(&client->addr),
 97                                 &addr_len);
 98 
 99 #if 0
100         printf ("%s(): server: %d client: %d\n", __FUNCTION__,
101                         server->fd, 
102                         client->fd );
103 #endif
104 
105         return (-1 == client->fd ? -1 : 0);
106 }
107 
108 int rh_socket_set (rh_socket_t *s, int what)
109 {
110         int     temp;
111         
112         RH_SOCKET_REQUIRE(s);
113 
114         switch (what) {
115         
116         case RH_SOCKET_NONBLOCK:
117                 temp = fcntl (s->fd, F_GETFL);
118                 if (-1 == temp)
119                         return -1;
120                 
121                 return fcntl (s->fd, F_SETFL, temp | O_NONBLOCK);
122         
123         case RH_SOCKET_REUSEADDR:
124                 temp = 1;
125 
126                 return setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR,
127                                    (char*)&temp, sizeof(temp));  
128         case RH_SOCKET_CORK_ON:
129                 temp = 1;
130                 
131                 return setsockopt (s->fd, SOL_TCP, TCP_CORK,
132                                    (char*)&temp, sizeof(temp));  
133         
134         case RH_SOCKET_CORK_OFF:
135                 temp = 0;
136                 return setsockopt (s->fd, SOL_TCP, TCP_CORK,
137                                    (char*)&temp, sizeof(temp));  
138         default:
139                 errno = EINVAL;
140                 return -1;
141         }
142 }
143 
144 ssize_t rh_socket_write (rh_socket_t *s, const void *src, size_t nsrc)
145 {
146         RH_SOCKET_REQUIRE(s);
147 
148         return write (s->fd, src, nsrc);
149 }
150 
151 ssize_t rh_socket_read (rh_socket_t *s, void *dst, size_t ndst)
152 {
153         RH_SOCKET_REQUIRE(s);
154 
155         return read (s->fd, dst, ndst);
156 }


syntax highlighted by Code2HTML, v. 0.9.1