1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
5 * Copyright (C) 2001-2010 Eduardo Silva P. <edsiper@gmail.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
28 #include <sys/socket.h>
29 #include <sys/sendfile.h>
42 * http://www.baus.net/on-tcp_cork
44 int mk_socket_set_cork_flag(int fd
, int state
)
48 MK_TRACE("Socket, set Cork Flag FD %i to %s", fd
, (state
? "ON" : "OFF"));
51 return setsockopt(fd
, SOL_TCP
, TCP_CORK
, &state
, sizeof(state
));
54 int mk_socket_set_nonblocking(int sockfd
)
58 MK_TRACE("Socket, set FD %i to non-blocking", sockfd
);
61 if (fcntl(sockfd
, F_SETFL
, fcntl(sockfd
, F_GETFD
, 0) | O_NONBLOCK
) == -1) {
68 int mk_socket_set_tcp_nodelay(int sockfd
)
72 return setsockopt(sockfd
, SOL_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
75 int mk_socket_get_ip(int socket
, char *ipv4
)
79 struct sockaddr_in m_addr
;
82 getpeername(socket
, (struct sockaddr
*) &m_addr
, &len
);
83 inet_ntop(PF_INET
, &m_addr
.sin_addr
, ipv4
, ipv4_len
);
88 int mk_socket_close(int socket
)
93 int mk_socket_create()
97 if ((sockfd
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1) {
98 perror("client: socket");
105 int mk_socket_connect(int socket_fd
, char *host
, int port
)
109 ret
= plg_netiomap
->connect(socket_fd
, host
, port
);
114 void mk_socket_reset(int socket
)
118 if (setsockopt(socket
, SOL_SOCKET
, SO_REUSEADDR
, &status
, sizeof(int)) ==
120 perror("setsockopt");
125 /* Just IPv4 for now... */
126 int mk_socket_server(int port
, char *listen_addr
)
130 socket_fd
= plg_netiomap
->server(port
, listen_addr
);
139 /* NETWORK_IO plugin functions */
140 int mk_socket_accept(int server_fd
, struct sockaddr_in sock_addr
)
142 return plg_netiomap
->accept(server_fd
, sock_addr
);
145 int mk_socket_sendv(int socket_fd
, struct mk_iov
*mk_io
, int to
)
147 return plg_netiomap
->writev(socket_fd
, mk_io
);
150 int mk_socket_send(int socket_fd
, const void *buf
, size_t count
)
152 return plg_netiomap
->write(socket_fd
, buf
, count
);
155 int mk_socket_read(int socket_fd
, void *buf
, int count
)
157 return plg_netiomap
->read(socket_fd
, (void *)buf
, count
);
160 int mk_socket_send_file(int socket_fd
, int file_fd
, off_t
*file_offset
,
163 return plg_netiomap
->send_file(socket_fd
, file_fd
,
164 file_offset
, file_count
);