ba1d8d8385d23b54298764027e218ab70a6f0cdf
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
27 #include <sys/types.h>
28 #include <sys/socket.h>
33 #include <pluto/constants.h>
34 #include <pluto/defs.h>
45 * currently we only have one socket, maybe more in the future ?
51 status_t
receiver(private_socket_t
*this, packet_t
**packet
)
54 char buffer
[MAX_PACKET
];
55 packet_t
*pkt
= packet_create();
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)
68 pkt
->data
.ptr
= alloc_bytes(pkt
->data
.len
, "data in packet_t");
69 memcpy(pkt
->data
.ptr
, buffer
, pkt
->data
.len
);
77 status_t
sender(private_socket_t
*this, packet_t
*packet
)
81 printf("@%d\n", __LINE__
);
83 bytes_sent
= sendto(this->socket_fd
, packet
->data
.ptr
, packet
->data
.len
,
84 0, &(packet
->receiver
.addr
), packet
->receiver
.len
);
86 printf("bytes: %d\n", bytes_sent
);
87 if (bytes_sent
!= packet
->data
.len
)
94 status_t
destroyer(private_socket_t
*this)
96 close(this->socket_fd
);
102 socket_t
*socket_create()
104 private_socket_t
*this = alloc_thing(socket_t
, "private_socket_t");
105 struct sockaddr_in addr
;
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
;
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) {
120 printf("@%d\n", __LINE__
);
121 addr
.sin_family
= AF_INET
;
122 addr
.sin_addr
.s_addr
= INADDR_ANY
;
124 if (bind(this->socket_fd
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
129 printf("@%d\n", __LINE__
);
130 return (socket_t
*)this;