1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
5 * Copyright (C) 2008-2009 Eduardo Silva P.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
28 #include <sys/socket.h>
38 * http://www.baus.net/on-tcp_cork
40 int mk_socket_set_cork_flag(int fd
, int state
)
42 return setsockopt(fd
, SOL_TCP
, TCP_CORK
, &state
, sizeof(state
));
45 int mk_socket_set_nonblocking(int sockfd
)
47 if (fcntl(sockfd
, F_SETFL
, fcntl(sockfd
, F_GETFD
, 0)|O_NONBLOCK
) == -1) {
54 int mk_socket_set_tcp_nodelay(int sockfd
)
57 return setsockopt(sockfd
, SOL_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
60 int mk_socket_get_ip(int socket
, char *ipv4
)
64 struct sockaddr_in m_addr
;
67 getpeername(socket
, (struct sockaddr
*)&m_addr
, &len
);
68 inet_ntop(PF_INET
, &m_addr
.sin_addr
, ipv4
, ipv4_len
);
73 int mk_socket_close(int socket
)
78 int mk_socket_timeout(int s
, char *buf
, int len
,
79 int timeout
, int recv_send
)
82 time_t init_time
, max_time
;
87 max_time
= init_time
+ timeout
;
95 if(recv_send
==ST_RECV
)
96 n
=select(s
+1,&fds
,NULL
,NULL
,&tv
); // recv
98 n
=select(s
+1,NULL
,&fds
,NULL
,&tv
); // send
106 //pthread_kill(pthread_self(), SIGPIPE);
110 if(recv_send
==ST_RECV
){
111 status
=recv(s
,buf
,len
, 0);
114 status
=send(s
,buf
,len
, 0);
118 if(time(NULL
) >= max_time
){
119 //pthread_kill(pthread_self(), SIGPIPE);
126 int mk_socket_create()
130 if ((sockfd
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1) {
131 perror("client: socket");
138 int mk_socket_connect(int sockfd
, char *server
, int port
)
141 struct sockaddr_in
*remote
;
143 remote
= (struct sockaddr_in
*)
144 mk_mem_malloc_z(sizeof(struct sockaddr_in
));
145 remote
->sin_family
= AF_INET
;
147 res
= inet_pton(AF_INET
, server
, (void *)(&(remote
->sin_addr
.s_addr
)));
151 perror("Can't set remote->sin_addr.s_addr");
156 perror("Invalid IP address\n");
161 remote
->sin_port
= htons(port
);
163 (struct sockaddr
*)remote
,
164 sizeof(struct sockaddr
)) == -1)
174 void mk_socket_reset(int socket
)
178 if(setsockopt(socket
,SOL_SOCKET
,SO_REUSEADDR
,&status
,sizeof(int))==-1) {
179 perror("setsockopt");
184 /* Just IPv4 for now... */
185 int mk_socket_server(int port
)
188 struct sockaddr_in local_sockaddr_in
;
190 /* Create server socket */
191 fd
=socket(PF_INET
,SOCK_STREAM
,0);
192 mk_socket_set_tcp_nodelay(fd
);
194 local_sockaddr_in
.sin_family
=AF_INET
;
195 local_sockaddr_in
.sin_port
=htons(port
);
196 local_sockaddr_in
.sin_addr
.s_addr
=INADDR_ANY
;
197 memset(&(local_sockaddr_in
.sin_zero
),'\0',8);
199 /* Avoid bind issues, reset socket */
202 if(bind(fd
,(struct sockaddr
*)&local_sockaddr_in
,
203 sizeof(struct sockaddr
)) != 0)
206 printf("Error: Port %i cannot be used\n", port
);
211 * The queue limit is given by /proc/sys/net/core/somaxconn
212 * we need to add a dynamic function to get that value on fly
214 if((listen(fd
, 1024))!=0) {