- first attempt of socket abstraction (more to do)
[strongswan.git] / Source / charon / socket.c
1 /**
2 * @file socket.c
3 *
4 * @brief management of sockets
5 *
6 * receiver reads from here, sender writes to here
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include "socket.h"
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <freeswan.h>
33 #include <pluto/constants.h>
34 #include <pluto/defs.h>
35
36
37
38 typedef struct {
39 /**
40 * public functions
41 */
42 socket_t public;
43
44 /**
45 * currently we only have one socket, maybe more in the future ?
46 */
47 int socket_fd;
48 } private_socket_t;
49
50
51 status_t receiver(private_socket_t *this, packet_t **packet)
52 {
53
54 char buffer[MAX_PACKET];
55 packet_t *pkt = packet_create();
56
57 /* do the read */
58 pkt->sender.len = sizeof(pkt->sender.addr);
59 pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
60 &(pkt->sender.addr), &(pkt->sender.len));
61 if (pkt->data.len < 0)
62 {
63 pkt->destroy(pkt);
64 return FAILED;
65 }
66
67 /* fill in packet */
68 pkt->data.ptr = alloc_bytes(pkt->data.len, "data in packet_t");
69 memcpy(pkt->data.ptr, buffer, pkt->data.len);
70
71 /* return packet */
72 *packet = pkt;
73
74 return SUCCESS;
75 }
76
77 status_t sender(private_socket_t *this, packet_t *packet)
78 {
79 ssize_t bytes_sent;
80
81 printf("@%d\n", __LINE__);
82 /* send data */
83 bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
84 0, &(packet->receiver.addr), packet->receiver.len);
85
86 printf("bytes: %d\n", bytes_sent);
87 if (bytes_sent != packet->data.len)
88 {
89 return FAILED;
90 }
91 return SUCCESS;
92 }
93
94 status_t destroyer(private_socket_t *this)
95 {
96 close(this->socket_fd);
97 pfree(this);
98
99 return SUCCESS;
100 }
101
102 socket_t *socket_create()
103 {
104 private_socket_t *this = alloc_thing(socket_t, "private_socket_t");
105 struct sockaddr_in addr;
106
107 /* public functions */
108 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
109 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
110 this->public.destroy = (status_t(*)(socket_t*))destroyer;
111
112 printf("@%d\n", __LINE__);
113 /* create default ipv4 socket */
114 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
115 if (this->socket_fd < 0) {
116 pfree(this);
117 return NULL;
118 }
119
120 printf("@%d\n", __LINE__);
121 addr.sin_family = AF_INET;
122 addr.sin_addr.s_addr = INADDR_ANY;
123 addr.sin_port = 500;
124 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
125 pfree(this);
126 return NULL;
127 }
128
129 printf("@%d\n", __LINE__);
130 return (socket_t*)this;
131 }