aboutsummaryrefslogtreecommitdiff
path: root/src/socket.c
blob: 2efe53a1cb39510f2516f54b6e7e4a5af6255a00 (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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>

#include "socket.h"
#include "typedef.h"

// #define LOG_INFO_STDOUT

void *get_in_addr(struct sockaddr *sa)
{
	if (sa->sa_family == AF_INET) {
		return &(((struct sockaddr_in*)sa)->sin_addr);
	}

	return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int listener(char *port, void (*on_recv)(message *))
{
    int sockfd;
    int ret;

    struct addrinfo hints, *servinfo;
    memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_INET6; // set to AF_INET to use IPv4
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_PASSIVE; // use my IP

    int rv;
    if((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) {
		err("getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

    struct addrinfo *p;
    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if((sockfd = socket(p->ai_family, p->ai_socktype,
                             p->ai_protocol)) == -1) {
			perror("listener: socket");
			continue;
		}

		if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("listener: bind");
			continue;
		}

		break;
	}

    if(p == NULL) {
	    err("listener: failed to bind socket\n");
		return 1;
	}

    freeaddrinfo(servinfo);

    int i = 0;
    time_t now = 0;
    while(1)
    {
        if(now < time(NULL)) {
            now = time(NULL);
            info("FPS: %d\n", i);
            i = 0;
        }

        message m = {0};
        struct sockaddr_storage their_addr;
        socklen_t addr_len = sizeof(their_addr);

        int numbytes;
        if((numbytes = recvfrom(sockfd, &m, BUF_CAP, 0,
                                 (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            return 1;
        }


    #ifdef LOG_INFO_STDOUT
        char s[INET6_ADDRSTRLEN];
        info("listener: got packet from %s, %d bytes long\n",
               inet_ntop(their_addr.ss_family,
                         get_in_addr((struct sockaddr *)&their_addr),
                         s, sizeof(s)), numbytes);
    #endif

        on_recv(&m);

        i++;
    }

    return 0;
}

int talker(char *port, char *address, void (*on_send)(message *))
{
    int sockfd;

    struct addrinfo hints, *servinfo;
    memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_INET6; // set to AF_INET to use IPv4
	hints.ai_socktype = SOCK_DGRAM;

    int rv;
    if((rv = getaddrinfo(address, port, &hints, &servinfo)) != 0) {
        err("getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

    struct addrinfo *p;
    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if((sockfd = socket(p->ai_family, p->ai_socktype,
                             p->ai_protocol)) == -1) {
			perror("talker: socket");
			continue;
		}

		break;
	}

    if(p == NULL) {
        err("talker: failed to create socket\n");
		return 1;
	}

    int i = 0;
    time_t now = 0;
    while(1)
    {
        if(now < time(NULL)) {
            now = time(NULL);
            info("FPS %d\n", i);
            i = 0;
        }

        message m = {0};
        on_send(&m);

        int numbytes;
        if((numbytes = sendto(sockfd, &m, MESSAGE_SZ, 0,
                              p->ai_addr, p->ai_addrlen)) == -1) {
            perror("talker: sendto");
            return 1;
        }
    #ifdef LOG_INFO_STDOUT
        info("talker: sent %d bytes to %s\n", numbytes, address);
    #endif

        i++;
    }

    freeaddrinfo(servinfo);
    close(sockfd);

    return 0;
}