socket.c 4.46 KB
Newer Older
1
2
3
4
5
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
6
7
#include <stdlib.h>
#include "../defines.h"
8
9
#include "socket.h"

10
int prrt_create_socket(prrt_socket *sock_ptr, uint16_t port) {
11
    // Create Data Socket
12
    if((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
13
14
15
16
17
18
19
20
21
22
23
        perror("cannot create socket");
        return -1;
    }

    // Bind Data Socket
    struct sockaddr_in address;
    memset((char*) &address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons(port);

24
    if(bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
25
26
27
28
29
        perror("cannot bind socket");
        return -1;
    }

    // Create Feedback Socket
30
    if((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
31
32
33
34
35
36
37
38
39
40
        perror("cannot create socket");
        return -1;
    }

    // Bind Feedback Socket
    memset((char*) &address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons((uint16_t) (port + 1)); // TODO: fail if port is 65535

41
    if(bind(sock_ptr->fd_feedback, (struct sockaddr *) &address, sizeof(address)) < 0) {
42
43
44
45
46
47
48
        perror("cannot bind socket");
        return -1;
    }

    return 0;
}

49
50
int prrt_connect(prrt_socket *sock_ptr, char *host, uint16_t port) {
    prrt_receiver recv =  { host , port};
51

52
53
54
55
56
57
    if(sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
        sock_ptr->receivers[sock_ptr->receiver_len] =recv;
        sock_ptr->receiver_len++;
    } else {
        return -1;
    }
58
59
60
61

    return 0;
}

62
int prrt_send(prrt_socket *sock_ptr, const void *data, size_t data_len) {
63
64
65
66
67
68
69
70
71
72
73
74
    prrt_packet * packet = malloc(sizeof(prrt_packet));
    create_packet_source(packet, 5, data, data_len);

    uint8_t buf[MAX_PAYLOAD_LENGTH];
    uint32_t length = packet_size(packet);
    if(encode_packet_source(buf, MAX_PAYLOAD_LENGTH, packet) < 0) {
        perror("BUF too small.");
        return -1;
    }

    print_packet(packet);

75
76
77
    int i;
    for(i = 0; i < sock_ptr->receiver_len; i++) {
        prrt_receiver recv = sock_ptr->receivers[i];
78

79
        struct hostent *hp;
80

81
82
83
84
        struct sockaddr_in targetaddr;
        memset((char*) &targetaddr, 0, sizeof(targetaddr));
        targetaddr.sin_family = AF_INET;
        targetaddr.sin_port = htons(recv.port);
85

86
87
        hp = gethostbyname(recv.host_name);
        memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
88

89
        if((sendto(sock_ptr->fd_data, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
90
91
92
            perror("sendto failed");
            return -1;
        }
93
94
95
96
    }
    return 0;
}

97
98
99
prrt_packet *prrt_recv(prrt_socket *sock_ptr) {
    unsigned char buffer[MAX_PAYLOAD_LENGTH];

100
101
102
103
104
    // RECEIVE DATA
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

105
    n = recvfrom(sock_ptr->fd_data, buffer, MAX_PAYLOAD_LENGTH, 0, (struct sockaddr *) &remote, &addrlen);
106
107
108
109
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
    printf("got a datagram from %s port %d\n", remote_host, remote_port);

110
111
    prrt_packet *packet = malloc(sizeof(prrt_packet));
    decode_packet_source(buffer, (uint16_t) n, packet);
112
113
114
115
116

    // REPLY
    struct sockaddr_in targetaddr;
    memset((char*) &targetaddr, 0, sizeof(targetaddr));
    targetaddr.sin_family = AF_INET;
117
    targetaddr.sin_port = htons((uint16_t) (remote_port + 1));
118
119
120
121

    struct hostent *hp;

    hp = gethostbyname(remote_host);
122
    memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
123

124
    if((sendto(sock_ptr->fd_data, buffer, (size_t) n, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
125
        perror("sendto failed");
126
        return NULL;
127
128
    }
    
129
    return packet;
130
131
}

132
133
134
135
136
int prrt_close_socket(prrt_socket *sock_ptr) {
    // TODO: clean up all receivers

    close(sock_ptr->fd_data);
    close(sock_ptr->fd_feedback);
137
138
139
    return 0;
}

140
ssize_t prrt_recv_feedback(prrt_socket *sock_ptr, void *bufin, size_t length) {
141
142
143
144
145
    printf("RECEIVING FB");
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

146
    n = recvfrom(sock_ptr->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
147
148
149
150
151
152
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
    printf("got feedback from %s port %d\n", remote_host, remote_port);

    return n;
}