Security Plugin: set http status for URL rule match
[MonkeyD.git] / src / socket.c
blob3c58cbc851dada75eeb765778030f0f628b3a4a1
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
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.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
28 #include <sys/socket.h>
29 #include <sys/sendfile.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
34 #include "socket.h"
35 #include "memory.h"
36 #include "utils.h"
37 #include "plugin.h"
38 #include "monkey.h"
41 * Example from:
42 * http://www.baus.net/on-tcp_cork
44 int mk_socket_set_cork_flag(int fd, int state)
47 #ifdef TRACE
48 MK_TRACE("Socket, set Cork Flag FD %i to %s", fd, (state ? "ON" : "OFF"));
49 #endif
51 return setsockopt(fd, SOL_TCP, TCP_CORK, &state, sizeof(state));
54 int mk_socket_set_nonblocking(int sockfd)
57 #ifdef TRACE
58 MK_TRACE("Socket, set FD %i to non-blocking", sockfd);
59 #endif
61 if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0) | O_NONBLOCK) == -1) {
62 perror("fcntl");
63 return -1;
65 return 0;
68 int mk_socket_set_tcp_nodelay(int sockfd)
70 int on = 1;
72 return setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &on, sizeof(on));
75 int mk_socket_get_ip(int socket, char *ipv4)
77 int ipv4_len = 16;
78 socklen_t len;
79 struct sockaddr_in m_addr;
81 len = sizeof(m_addr);
82 getpeername(socket, (struct sockaddr *) &m_addr, &len);
83 inet_ntop(PF_INET, &m_addr.sin_addr, ipv4, ipv4_len);
85 return 0;
88 int mk_socket_close(int socket)
90 return close(socket);
93 int mk_socket_create()
95 int sockfd;
97 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
98 perror("client: socket");
99 return -1;
102 return sockfd;
105 int mk_socket_connect(int socket_fd, char *host, int port)
107 int ret;
109 ret = plg_netiomap->connect(socket_fd, host, port);
111 return ret;
114 void mk_socket_reset(int socket)
116 int status = 1;
118 if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &status, sizeof(int)) ==
119 -1) {
120 perror("setsockopt");
121 exit(1);
125 /* Just IPv4 for now... */
126 int mk_socket_server(int port, char *listen_addr)
128 int socket_fd;
130 socket_fd = plg_netiomap->server(port, listen_addr);
132 if (socket_fd < 0) {
133 exit(EXIT_FAILURE);
136 return socket_fd;
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,
161 size_t file_count)
163 return plg_netiomap->send_file(socket_fd, file_fd,
164 file_offset, file_count);