summaryrefslogtreecommitdiff
path: root/trackerlogic.h
blob: ad18cba50a4fafbf691f8152b8404fa14725e412 (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
#ifndef __TRACKERLOGIC_H__
#define __TRACKERLOGIC_H__

#include <sys/types.h>

/* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */
/* They mark memory used as data instead of integer or human readable string -
   they should be cast before used as integer/text */
typedef unsigned char  ot_byte;
typedef unsigned short ot_word;
typedef unsigned long  ot_dword;

typedef unsigned long  ot_time;
typedef ot_byte        ot_hash[20];
typedef ot_byte        ot_ip[ 4/*0*/ ];
// tunables
static const unsigned long OT_TIMEOUT          = 2700;
static const unsigned long OT_HUGE_FILESIZE    = 1024*1024*256; // Thats 256MB per file, enough for 204800 peers of 128 bytes

// We will not service v6, yes
#define OT_COMPACT_ONLY

#define MEMMOVE              memmove
#define BZERO                bzero
#define FORMAT_FIXED_STRING  sprintf
#define FORMAT_FORMAT_STRING sprintf
#define BINARY_FIND          binary_search
#define NOW                  time(NULL)

typedef struct ot_peer {
#ifndef OT_COMPACT_ONLY
  ot_hash id;
  ot_hash key;
#endif
  ot_ip   ip;
  ot_word port;
  ot_time death;
  ot_byte flags;
} *ot_peer;
static const ot_byte PEER_FLAG_SEEDING   = 0x80;
static const ot_byte PEER_IP_LENGTH_MASK = 0x3f;

typedef struct {
  ot_hash       hash;
  ot_peer       peer_list;
  unsigned long peer_count;
  unsigned long seed_count;
} *ot_torrent;

void *map_file( char *file_name );
void  unmap_file( char *file_name, void *map, unsigned long real_size );

// This behaves quite like bsearch but allows to find
// the insertion point for inserts after unsuccessful searches
// in this case exactmatch is 0 on exit
//
void *binary_search( const void *key, const void *base,
                     const unsigned long member_count, const unsigned long member_size,
                     int (*compar) (const void *, const void *),
                     int *exactmatch );

//
// Exported functions
//

int  init_logic( char *chdir_directory );
void deinit_logic( );

ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer );
size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply );
void heal_torrent( ot_torrent torrent );

#endif