|
design and implementation of each module |
design and implementation of 4.1 seed parsing module
Parsing seed files is done primarily in parse_metafile.h and parse_metafile.c. The contents of the Parse_metafile.h file are:
Parse_metafile.h
#ifndef Parse_metafile
#define Parse_metafile
Save the URL of the tracker retrieved from the seed file
typedef struct _ANNOUNCE_LIST {
Char announce[128];
struct _announce_list *next;
} announce_list;
Save the path and length of each file to be downloaded
typedef struct _FILES {
Char path[256];
Long length;
struct _files *next;
} Files;
int Read_metafile (char *metafile_name); Read the seed file
int Find_keyword (char *keyword,long *position); Find a keyword in a seed file
int read_announce_list (); Get the address of each tracker server
int add_an_announce (char* URL); Add a URL to the tracker list
int Get_piece_length (); Gets the length of each piece, typically 256KB
int get_pieces (); Read hash values for individual piece
int Is_multi_files (); Decide whether to download a single file or multiple files
int Get_file_name (); Gets the file name, for multiple files, gets the directory name
int Get_file_length (); Get the total length of the file to download
int Get_files_length_path (); Gets the path and length of the file, valid for multiple file seeds
int Get_info_hash (); Calculate Info_hash by the value corresponding to the Info keyword
int get_peer_id (); Generates PEER_ID, each peer has a 20-byte peer_id
void Release_memory_in_parse_metafile ()//releasing dynamically allocated memory in PARSE_METAFILE.C
int Parse_metafile (char *metafile); Call the function defined in this file to complete the parse seed file
#endif
The following is the head of the parse_metafile.c file, which contains some header files and defines some global variables, and the definitions of each function are listed later.
Parse_metafile.c
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "parse_metafile.h"
#include "Sha1.h"
char *metafile_content = NULL; Save the contents of a seed file
Long filesize; The length of the seed file
int piece_length = 0; Length of each piece, usually 256KB or 262144 bytes