4 * @brief management of sockets
6 * receiver reads from here, sender writes to here
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
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>.
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
26 #include <sys/types.h>
27 #include <sys/socket.h>
34 #include "utils/allocator.h"
36 typedef struct private_socket_s private_socket_t
;
38 struct private_socket_s
{
45 * currently we only have one socket, maybe more in the future ?
51 * implementation of socket_t.receive
53 status_t
receiver(private_socket_t
*this, packet_t
**packet
)
55 char buffer
[MAX_PACKET
];
57 packet_t
*pkt
= packet_create(AF_INET
);
60 /* add packet destroy handler for cancellation, enable cancellation */
61 pthread_cleanup_push((void(*)(void*))pkt
->destroy
, (void*)pkt
);
62 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
65 pkt
->data
.len
= recvfrom(this->socket_fd
, buffer
, MAX_PACKET
, 0,
66 &(pkt
->source
), &(pkt
->sockaddr_len
));
68 /* reset cancellation, remove packet destroy handler (without executing) */
69 pthread_setcancelstate(oldstate
, NULL
);
70 pthread_cleanup_pop(0);
73 /* TODO: get senders destination address, using
74 * IP_PKTINFO and recvmsg */
76 if (pkt
->data
.len
< 0)
79 /* TODO: log detailed error */
84 pkt
->data
.ptr
= allocator_alloc(pkt
->data
.len
);
85 memcpy(pkt
->data
.ptr
, buffer
, pkt
->data
.len
);
94 * implementation of socket_t.send
96 status_t
sender(private_socket_t
*this, packet_t
*packet
)
101 bytes_sent
= sendto(this->socket_fd
, packet
->data
.ptr
, packet
->data
.len
,
102 0, &(packet
->destination
), packet
->sockaddr_len
);
104 if (bytes_sent
!= packet
->data
.len
)
106 /* TODO: log detailed error */
113 * implementation of socket_t.destroy
115 status_t
destroy(private_socket_t
*this)
117 close(this->socket_fd
);
118 allocator_free(this);
123 socket_t
*socket_create(u_int16_t port
)
125 private_socket_t
*this = allocator_alloc_thing(private_socket_t
);
126 struct sockaddr_in addr
;
128 /* public functions */
129 this->public.send
= (status_t(*)(socket_t
*, packet_t
*))sender
;
130 this->public.receive
= (status_t(*)(socket_t
*, packet_t
**))receiver
;
131 this->public.destroy
= (status_t(*)(socket_t
*))destroy
;
133 /* create default ipv4 socket */
134 this->socket_fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
135 if (this->socket_fd
< 0) {
136 allocator_free(this);
137 /* TODO: log detailed error */
141 /* bind socket to all interfaces */
142 addr
.sin_family
= AF_INET
;
143 addr
.sin_addr
.s_addr
= INADDR_ANY
;
144 addr
.sin_port
= htons(port
);
145 if (bind(this->socket_fd
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
146 allocator_free(this);
147 /* TODO: log detailed error */
151 return (socket_t
*)this;