~ubuntu-branches/ubuntu/karmic/gtk-gnutella/karmic

« back to all changes in this revision

Viewing changes to src/g2/udp_transceiver.c

  • Committer: Bazaar Package Importer
  • Author(s): Anand Kumria
  • Date: 2005-08-04 11:32:05 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050804113205-q746i4lgo3rtlegn
Tags: 0.95.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004, Jeroen Asselman
 
3
 *
 
4
 *----------------------------------------------------------------------
 
5
 * This file is part of gtk-gnutella.
 
6
 *
 
7
 *  gtk-gnutella is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  gtk-gnutella is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with gtk-gnutella; if not, write to the Free Software
 
19
 *  Foundation, Inc.:
 
20
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 *----------------------------------------------------------------------
 
22
 */
 
23
 
 
24
/**
 
25
 * @ingroup undoc
 
26
 * @file
 
27
 *
 
28
 * Needs short description here.
 
29
 *
 
30
 * @author Jeroen Asselman
 
31
 * @date 2004
 
32
 */
 
33
 
 
34
#include "../common.h"          /* For -DUSE_DMALLOC */
 
35
 
 
36
#include <math.h>
 
37
#include <glib.h>
 
38
 
 
39
int sequencenumber = 0;
 
40
 
 
41
static int header_length = 8;
 
42
static int MTU = 1500;
 
43
int transmit_retransmit_interval = 10;  /**< seconds */
 
44
int transmit_packet_timeout = 26;               /**< seconds */
 
45
int receive_packet_expiry = 30;                 /**< seconds */
 
46
 
 
47
GSList *receiving_fragments_list;
 
48
GSList *received_fragments_list;
 
49
 
 
50
typedef struct fragment_s fragment_t;
 
51
struct fragment_s
 
52
{
 
53
        char *fragment;
 
54
        int   fragment_length;
 
55
 
 
56
        time_t initial_transmitted;
 
57
        time_t last_transmitted;
 
58
 
 
59
        guint32 ipaddress;
 
60
        guint16 port;
 
61
};
 
62
 
 
63
void udp_transceiver_timer(time_t now)
 
64
{
 
65
        /* Fragments waiting for acknowledge > receive_packet_expiry?
 
66
         * Remove acknowledge packets.
 
67
 
 
68
         * Fragments waiting for acknowledge?
 
69
         * Time to retransmit?
 
70
         * Retransmit to be acknowledged packets
 
71
 
 
72
         * Fragments to be send?
 
73
         * Send a few fragments. No more then udp_bandwith_out / MTU
 
74
         */
 
75
}
 
76
 
 
77
/***
 
78
 *** Building and sending
 
79
 ***/
 
80
 
 
81
/**
 
82
 * Wether the udp transceiver might be able to handle this received fragment.
 
83
 */
 
84
gboolean
 
85
udp_transceiver_can_handle_received_fragment(char *buf, int length)
 
86
{
 
87
        return length > header_length &&
 
88
                buf[0] == 'G' && buf[1] == 'N' && buf[2] == 'D';
 
89
}
 
90
 
 
91
/**
 
92
 * Put fragment in the to send list.
 
93
 */
 
94
static void
 
95
udp_transceiver_put_send_fragment(fragment_t *fragment)
 
96
{
 
97
        /* FIXME: glist_prepend fragment */
 
98
}
 
99
 
 
100
/**
 
101
 * Send the fragment.
 
102
 */
 
103
static void
 
104
udp_transceiver_send_fragment(fragment_t *fragment,
 
105
                                                          gboolean deflate,
 
106
                                                          gboolean acknowledge)
 
107
{
 
108
        /* FIXME: Send fragment over socket. */
 
109
 
 
110
        if (acknowledge)
 
111
        {
 
112
            /* FIXME: Add fragment to waiting_for_acknowledge list */
 
113
        }
 
114
}
 
115
 
 
116
/**
 
117
 * Free the fragment and all its associated memory.
 
118
 */
 
119
void
 
120
udp_transceiver_free_fragment(fragment_t *fragment)
 
121
{
 
122
        if (fragment->fragment != NULL)
 
123
                free(fragment->fragment);
 
124
 
 
125
        free(fragment);
 
126
}
 
127
 
 
128
/**
 
129
 * Build a fragment.
 
130
 */
 
131
fragment_t *
 
132
udp_transceiver_build_fragment(char *buffer, int length,
 
133
                                                                int sequencenumber,
 
134
                                                                int partnumber,
 
135
                                                                int fragments,
 
136
                                                                gboolean deflate,
 
137
                                                                gboolean acknowledge)
 
138
{
 
139
        fragment_t *fragment = (fragment_t *) malloc(sizeof(fragment_t));
 
140
        fragment->fragment_length = length + header_length;
 
141
        fragment->fragment = (char *) malloc(fragment->fragment_length);
 
142
 
 
143
        /* Build identifier */
 
144
        fragment->fragment[0] = 'G';
 
145
        fragment->fragment[1] = 'N';
 
146
        fragment->fragment[2] = 'D';
 
147
 
 
148
        /* Endian doesn't matter */
 
149
        fragment->fragment[4] = (sequencenumber & 0x00FF);
 
150
        fragment->fragment[5] = (sequencenumber >> 8);
 
151
        fragment->fragment[6] = partnumber;
 
152
        fragment->fragment[7] = fragments;
 
153
 
 
154
        memcpy(fragment->fragment + header_length, buffer, length);
 
155
 
 
156
        return fragment;
 
157
}
 
158
 
 
159
/**
 
160
 * Send a fragment to the specified address.
 
161
 */
 
162
void
 
163
udp_transceiver_send(char *buffer, int length, gboolean deflate,
 
164
                                                        gboolean acknowledge,
 
165
                                                        guint32 address, guint16 port)
 
166
{
 
167
        /* Create fragments. */
 
168
        int fragments = (int) ceil((double)length / (double) MTU);
 
169
        int offset = 0;
 
170
        int i;
 
171
 
 
172
        sequencenumber++;
 
173
 
 
174
        for (i = 0; i < fragments; i++) {
 
175
                fragment_t *fragment = udp_transceiver_build_fragment(
 
176
                        buffer + offset, length - offset > MTU ? MTU : length - offset,
 
177
                        sequencenumber, i, fragments, deflate, acknowledge);
 
178
 
 
179
                fragment->ipaddress = address;
 
180
                fragment->port = port;
 
181
 
 
182
                udp_transceiver_send_fragment(fragment, deflate, acknowledge);
 
183
                offset += MTU;
 
184
        }
 
185
}
 
186
 
 
187
/***
 
188
 *** Receiving and parsing
 
189
 ***/
 
190
 
 
191
/**
 
192
 * Get the sequence number from a fragment.
 
193
 */
 
194
inline int
 
195
udp_transceiver_get_sequencenumber(fragment_t *fragment)
 
196
{
 
197
        return (fragment->fragment[4] << 8) + fragment->fragment[5];
 
198
}
 
199
 
 
200
/**
 
201
 * Get the part number from a fragment.
 
202
 */
 
203
inline int
 
204
udp_transceiver_get_partnumber(fragment_t *fragment)
 
205
{
 
206
        return fragment->fragment[6];
 
207
}
 
208
 
 
209
/**
 
210
 * Get the number of fragments from a datagram fragment.
 
211
 */
 
212
inline int
 
213
udp_transceiver_get_fragments(fragment_t *fragment)
 
214
{
 
215
        return fragment->fragment[7];
 
216
}
 
217
 
 
218
/**
 
219
 * Gets previously received fragments from the given source with matching
 
220
 * sequencenumber.
 
221
 */
 
222
static GSList *
 
223
udp_transceiver_lookup_fragments_by_sequencenumber_source(
 
224
        fragment_t *fragment)
 
225
{
 
226
        return NULL;
 
227
}
 
228
 
 
229
/**
 
230
 * Puts a received datagram fragment in a to handle list.
 
231
 */
 
232
static void
 
233
udp_transceiver_put_received_fragment(fragment_t *fragment)
 
234
{
 
235
        int fragments = udp_transceiver_get_fragments(fragment);
 
236
        int partnumber = udp_transceiver_get_partnumber(fragment);
 
237
        int sequencenumber = udp_transceiver_get_sequencenumber(fragment);
 
238
 
 
239
        GSList *fragment_list =
 
240
                udp_transceiver_lookup_fragments_by_sequencenumber_source(
 
241
                        fragment);
 
242
        fragment_t *fragment_tmp;
 
243
 
 
244
        if (fragment_list == NULL)
 
245
        {
 
246
                /* FIXME: Create new fragment list (array?) with size fragments */
 
247
        }
 
248
 
 
249
        /* fragment_tmp = gslist_at(fragment_list, partnumber) */
 
250
        if (fragment_tmp == NULL)
 
251
        {
 
252
                /* FIXME:
 
253
                 * gslist_insert_at(fragment_list, partnumber, fragment_received) */
 
254
        }
 
255
        else
 
256
        {
 
257
                /* FIXME: udp_transceiver_free_fragment(fragment) */
 
258
        }
 
259
 
 
260
        /* FIXME: Check if all fragments are received */
 
261
 
 
262
        /* All fragments received */
 
263
}
 
264
 
 
265
/**
 
266
 * Parses a datagram and if it is a fragment it will be handed over to
 
267
 * udp_transceiver_put_received_fragment.
 
268
 */
 
269
static gboolean
 
270
udp_transceiver_put_received_datagram(char *buf, int length,
 
271
                                      guint32 ip, guint16 port)
 
272
{
 
273
        fragment_t *fragment_received;
 
274
 
 
275
        if (!udp_transceiver_can_handle_received_fragment(buf, length))
 
276
                return FALSE;
 
277
 
 
278
        fragment_received = (fragment_t *) malloc(sizeof(fragment_t));
 
279
        fragment_received->fragment = (char *) malloc(length);
 
280
        memcpy(fragment_received->fragment, buf, length);
 
281
        fragment_received->fragment_length = length;
 
282
        fragment_received->ipaddress = ip;
 
283
        fragment_received->port = port;
 
284
 
 
285
        udp_transceiver_put_received_fragment(fragment_received);
 
286
}
 
287
 
 
288
/**
 
289
 * Handle receiving of data
 
290
 */
 
291
gboolean
 
292
udp_transceiver_receive(char **buffer, int *length)
 
293
{
 
294
    /* FIXME: for bla in fragments_received */
 
295
        /* *length += fragment->fragment_length - header_length */
 
296
 
 
297
        return FALSE;
 
298
}
 
299
 
 
300
/* vi: set ts=4 sw=4 cindent: */