socket.c 6.78 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
#include <stdlib.h>
7
#include <pthread.h>
8
#include <assert.h>
9
#include "../defines.h"
10
#include "socket.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
11
12
#include "processes/feedback_receiver.h"
#include "processes/data_transmitter.h"
13
#include "packet.h"
14

15
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
16
    sock_ptr->seqno_source = 1;
17
    sock_ptr->seqnoRedundancy = 1;
18

19
    // Create Data Socket
20
    if ((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
21
22
23
24
25
        perror("cannot create socket");
        return -1;
    }

    // Create Feedback Socket
26
    if ((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
27
28
29
30
        perror("cannot create socket");
        return -1;
    }

31
    if (is_sender) {
32
33
        // Bind Feedback Socket
        struct sockaddr_in address;
34
        memset((char *) &address, 0, sizeof(address));
35
36
37
        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
38

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

44
45
46
47
        pthread_mutex_init(&sock_ptr->out_queue_filled_mutex, NULL);
        pthread_cond_init(&sock_ptr->out_queue_filled_cv, NULL);

        sock_ptr->out_queue = List_create();
48
49

        int rc = pthread_create(&sock_ptr->receive_thread, NULL, receive_feedback_loop, (void *) sock_ptr);
50
        if (rc) {
51
52
53
54
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }

55
        rc = pthread_create(&sock_ptr->send_thread, NULL, send_data_loop, (void *) sock_ptr);
56
        if (rc) {
57
58
59
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
60
    } else {
61
62
        // Bind Data Socket
        struct sockaddr_in address;
63
        memset((char *) &address, 0, sizeof(address));
64
65
66
67
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = htonl(INADDR_ANY);
        address.sin_port = htons(port);

68
        if (bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
69
70
71
            perror("cannot bind socket");
            return -1;
        }
72

73
        sock_ptr->forwardPacketTable = malloc(sizeof(PrrtForwardPacketTable));
74
        PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable);
75
76
    }

77
78
79
    return 0;
}

80
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
81
    PrrtReceiver recv = {host, port};
82

83
84
    if (sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
        sock_ptr->receivers[sock_ptr->receiver_len] = recv;
85
86
87
88
        sock_ptr->receiver_len++;
    } else {
        return -1;
    }
89
90
91
92

    return 0;
}

93
int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_len) {
94
    pthread_mutex_lock(&sock_ptr->out_queue_filled_mutex);
95

96
    PrrtPacket *packet = malloc(sizeof(PrrtPacket));
97
    assert(packet != NULL);
98
    PrrtPacket_create_data_packet(packet, 5, data, data_len, sock_ptr->seqno_source++);
99

100
101
102
    List_push(sock_ptr->out_queue, packet);
    pthread_cond_signal(&sock_ptr->out_queue_filled_cv);
    pthread_mutex_unlock(&sock_ptr->out_queue_filled_mutex);
103

104
    return 0;
105
106
}

107
uint32_t PrrtSocket_recv(const PrrtSocket *sock_ptr, void *buf_ptr) {
108
109
    unsigned char buffer[MAX_PAYLOAD_LENGTH];

110
    while (1) {
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        // RECEIVE DATA
        ssize_t n;
        struct sockaddr_in remote;
        socklen_t addrlen = sizeof(remote);

        n = recvfrom(sock_ptr->fd_data, buffer, MAX_PAYLOAD_LENGTH, 0, (struct sockaddr *) &remote, &addrlen);
        uint16_t remote_port = ntohs(remote.sin_port);
        char *remote_host = inet_ntoa(remote.sin_addr);

        PrrtPacket *packet = malloc(sizeof(PrrtPacket));
        assert(packet != NULL);
        PrrtPacket_decode(buffer, (uint16_t) n, packet);

        // REPLY
        struct sockaddr_in targetaddr;
126
        memset((char *) &targetaddr, 0, sizeof(targetaddr));
127
128
129
130
131
        targetaddr.sin_family = AF_INET;
        targetaddr.sin_port = htons((uint16_t) (remote_port + 1));

        struct hostent *hp;
        hp = gethostbyname(remote_host);
132
        memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
133

134
        PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
135
136
137
        uint8_t buf[MAX_PAYLOAD_LENGTH];
        uint32_t length = PrrtPacket_size(feedback_pkt_ptr);

138
        if (PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
139
140
            perror("BUF too small.");
        } else {
141
142
            if ((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) <
                 0)) {
143
144
145
146
                perror("sendto failed");
                return NULL;
            }
        }
147

148
        switch (PrrtPacket_type(packet)) {
149
            case PACKET_TYPE_DATA:
150
151
                // packet.timestamp + packet.timeout < now: break

152
153
154
155
                if (PrrtForwardPacketTable_test_set_is_number_relevant(sock_ptr->forwardPacketTable, packet->seqno) ==
                    FALSE) {
                    break;
                }
156
157
158
159
160
161

                // check incomplete_prrt_blocks for this seqno: insert if found
                // else: insert in data_packet_store

                // forward to application layer

162
163
                memcpy(buf_ptr, packet->payload + PRRT_PACKET_DATA_HEADER_SIZE,
                       packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
164
165
                return packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE;
            default:
166
                //PrrtPacket_print(packet);
167
                break;
168
169
        }

170
        // TODO: enough packets received?
171
    }
172
    return 0;
173
174
}

175
int PrrtSocket_close(const PrrtSocket *sock_ptr) {
176
177
    // TODO: shut down threads;

178
179
    // TODO: clean up all receivers

180
181
182
    pthread_mutex_destroy(&sock_ptr->out_queue_filled_mutex);
    pthread_cond_destroy(&sock_ptr->out_queue_filled_cv);
    List_destroy(sock_ptr->out_queue);
183

184
185
    close(sock_ptr->fd_data);
    close(sock_ptr->fd_feedback);
186
187
188
    return 0;
}

189
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, const size_t length) {
190
191
192
193
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

194
    n = recvfrom(sock_ptr->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
195
    if (n < 0) {
196
        perror("RECVFROM FAIL");
197
198
        return NULL;
    }
199
200
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
201
    printf("RECV FEEDBACK: %s:%d\n", remote_host, remote_port);
202

203
    PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
204
    assert(packet_ptr != NULL);
205
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
206
    return packet_ptr;
207
}