aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/network/socket.c
blob: 41a2224b820c1ca4e618b6c6e2a0152a91f0518c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * @file socket.c
 *
 * @brief Implementation of socket_t.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

#include "socket.h"

#include <globals.h>
#include <utils/allocator.h>
#include <utils/logger_manager.h>


typedef struct private_socket_t private_socket_t;

/**
 * Private data of an socket_t object
 */
struct private_socket_t{
	/**
	 * public functions
	 */
	 socket_t public;

	 /**
	  * currently we only have one socket, maybe more in the future ?
	  */
	 int socket_fd;
	 
	 /** 
	  * logger for this socket
	  */
	 logger_t *logger;
};

/**
 * implementation of socket_t.receive
 */
status_t receiver(private_socket_t *this, packet_t **packet)
{
	char buffer[MAX_PACKET];
	int oldstate;
	packet_t *pkt = packet_create();

	/* add packet destroy handler for cancellation, enable cancellation */
	pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
	
	pkt->source = host_create(AF_INET, "0.0.0.0", 0);
	pkt->destination = host_create(AF_INET, "0.0.0.0", 0);


	this->logger->log(this->logger, CONTROL|MORE, "going to read from socket");
	/* do the read */
	pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
							pkt->source->get_sockaddr(pkt->source), 
							pkt->source->get_sockaddr_len(pkt->source));

	/* reset cancellation, remove packet destroy handler (without executing) */
	pthread_setcancelstate(oldstate, NULL);
	pthread_cleanup_pop(0);


	/* TODO: get senders destination address, using
	 * IP_PKTINFO and recvmsg */

	if (pkt->data.len < 0)
	{
		pkt->destroy(pkt);
		this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
		return FAILED;
	}
	
	this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
						pkt->source->get_address(pkt->source), 
						pkt->source->get_port(pkt->source));

	/* fill in packet */
	pkt->data.ptr = allocator_alloc(pkt->data.len);
	memcpy(pkt->data.ptr, buffer, pkt->data.len);

	/* return packet */
	*packet = pkt;

	return SUCCESS;
}

/**
 * implementation of socket_t.send
 */
status_t sender(private_socket_t *this, packet_t *packet)
{
	ssize_t bytes_sent;


	this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
						packet->destination->get_address(packet->destination), 
						packet->destination->get_port(packet->destination));
	/* send data */
	bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
						0, packet->destination->get_sockaddr(packet->destination), 
						*(packet->destination->get_sockaddr_len(packet->destination)));

	if (bytes_sent != packet->data.len)
	{
		this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
		return FAILED;
	}
	return SUCCESS;
}

/**
 * implementation of socket_t.destroy
 */
void destroy(private_socket_t *this)
{
	close(this->socket_fd);
	global_logger_manager->destroy_logger(global_logger_manager, this->logger);
	allocator_free(this);
}

socket_t *socket_create(u_int16_t port)
{
	private_socket_t *this = allocator_alloc_thing(private_socket_t);
	struct sockaddr_in addr;

	/* public functions */
	this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
	this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
	this->public.destroy = (void(*)(socket_t*))destroy;
	
	this->logger = global_logger_manager->create_logger(global_logger_manager, SOCKET, NULL);

	/* create default ipv4 socket */
	this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
	if (this->socket_fd < 0) 
	{
		this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
		global_logger_manager->destroy_logger(global_logger_manager, this->logger);
		allocator_free(this);
		return NULL;
	}

	/* bind socket to all interfaces */
	addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);
    if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) 
    {
		this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
		global_logger_manager->destroy_logger(global_logger_manager, this->logger);
		allocator_free(this);
        return NULL;
    }

	return (socket_t*)this;
}