-
Notifications
You must be signed in to change notification settings - Fork 130
/
socket.c
61 lines (52 loc) · 1.65 KB
/
socket.c
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
/*
* udptunnel
* Copyright © 2013 Felipe Astroza A.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int socket_create(unsigned short port)
{
struct sockaddr_in sa;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1) {
perror("socket");
exit(1);
}
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = port;
sa.sin_family = AF_INET;
if(bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind");
exit(1);
}
return fd;
}
void socket_put_packet(int fd, struct sockaddr_in *sa, socklen_t salen, char *buf, unsigned int buflen)
{
if(sendto(fd, buf, buflen, 0, (struct sockaddr *)sa, salen) == -1) {
perror("sendto");
exit(1);
}
}
unsigned int socket_get_packet(int fd, struct sockaddr_in *sa, socklen_t *salen, char *buf, unsigned int bufsize)
{
return recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)sa, salen);
}