socket.c 5.3 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"
11
12
#include "feedback_receiver.h"
#include "data_transmitter.h"
13

14
int PrrtSocket_create(PrrtSocket *sock_ptr, uint16_t port, uint8_t is_sender) {
15
    // Create Data Socket
16
    if((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
17
18
19
20
21
22
23
24
25
26
27
        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);

28
    if(bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
29
30
31
32
33
        perror("cannot bind socket");
        return -1;
    }

    // Create Feedback Socket
34
    if((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
35
36
37
38
39
40
41
42
43
44
        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

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

50
    if(is_sender) {
51
52
53
54
        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();
55
56

        int rc = pthread_create(&sock_ptr->receive_thread, NULL, receive_feedback_loop, (void *) sock_ptr);
57
58
59
60
61
        if(rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }

62
63
64
65
66
        rc = pthread_create(&sock_ptr->send_thread, NULL, send_data_loop, (void *) sock_ptr);
        if(rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
67
68
69
70
    } else {

    }

71
72
73
    return 0;
}

74
75
int PrrtSocket_connect(PrrtSocket *sock_ptr, char *host, uint16_t port) {
    PrrtReceiver recv =  {host , port};
76

77
78
79
80
81
82
    if(sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
        sock_ptr->receivers[sock_ptr->receiver_len] =recv;
        sock_ptr->receiver_len++;
    } else {
        return -1;
    }
83
84
85
86

    return 0;
}

87
int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, size_t data_len) {
88
    pthread_mutex_lock(&sock_ptr->out_queue_filled_mutex);
89

90
    PrrtPacket * packet = malloc(sizeof(PrrtPacket));
91
    assert(packet != NULL);
92
    PrrtPacket_create_data_packet(packet, 5, data, data_len);
93

94
95
96
    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);
97

98
    return 0;
99
100
}

101
PrrtPacket *PrrtSocket_recv(PrrtSocket *sock_ptr) {
102
103
    unsigned char buffer[MAX_PAYLOAD_LENGTH];

104
105
106
107
108
    // RECEIVE DATA
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

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

113
    PrrtPacket *packet = malloc(sizeof(PrrtPacket));
114
    assert(packet != NULL);
115
    PrrtPacket_decode(buffer, (uint16_t) n, packet);
116
117
118
119
120

    // REPLY
    struct sockaddr_in targetaddr;
    memset((char*) &targetaddr, 0, sizeof(targetaddr));
    targetaddr.sin_family = AF_INET;
121
    targetaddr.sin_port = htons((uint16_t) (remote_port + 1));
122
123
124

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

127
    PrrtPacket * feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
128
    uint8_t buf[MAX_PAYLOAD_LENGTH];
129
    uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
130

131
    if(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
132
133
134
135
136
137
138
        perror("BUF too small.");
    } else {
        if((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
            perror("sendto failed");
            return NULL;
        }

139
    }
140
    return packet;
141
142
}

143
int PrrtSocket_close(PrrtSocket *sock_ptr) {
144
145
    // TODO: shut down threads;

146
147
    // TODO: clean up all receivers

148
149
150
    pthread_mutex_destroy(&sock_ptr->out_queue_filled_mutex);
    pthread_cond_destroy(&sock_ptr->out_queue_filled_cv);
    List_destroy(sock_ptr->out_queue);
151

152
153
    close(sock_ptr->fd_data);
    close(sock_ptr->fd_feedback);
154
155
156
    return 0;
}

157
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *sock_ptr, void *bufin, size_t length) {
158
159
160
161
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

162
    n = recvfrom(sock_ptr->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
163
    if(n < 0) {
164
        perror("RECVFROM FAIL");
165
166
        return NULL;
    }
167
168
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
169
    printf("RECV FEEDBACK: %s:%d\n", remote_host, remote_port);
170

171
    PrrtPacket * packet_ptr = malloc(sizeof(PrrtPacket));
172
    assert(packet_ptr != NULL);
173
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
174
    return packet_ptr;
175
}