예제 #1
0
파일: osppeer.c 프로젝트: medoelling/CS111
static char *create_digest(char* filename)
{
  size_t n;
  int 
  unsigned offset = 0;
  md5_state_t *pms = malloc(sizeof(md5_state_t));
  md5_init(pms);
 
  char* digest = malloc(MD5_TEXT_DIGEST_SIZE+1);
  md5_byte_t *buf = malloc(BLKSIZE+1);

  int fd = open(filename, O_RDONLY);
  while(1){
    n = pread(fd, buf, BLKSIZE, offset);
    buf[n] = 0;

    md5_append(pms, buf, n);
    offset += n;

    if(n < BLKSIZE)
      break;
  }
  close(fd);
  free(buf);
  md5_finish_text(pms, digest, 1);
  digest[MD5_TEXT_DIGEST_SIZE] = 0;

  return digest;
}
예제 #2
0
파일: osppeer.c 프로젝트: caiboyang/CS
 char *calculate_MD5_CODE(char fn[])
{
			/*start calculate MD5 for file*/
		md5_state_t * state =NULL;
		FILE *temp =NULL;
		unsigned int tmp_size;//file size for temp;
		unsigned char *tmp_buf = NULL;//buffer to hold file content
		size_t r;//return read file result
		char *MD5_CODE = NULL;

		//initilize
		state = (md5_state_t *)malloc(sizeof(md5_state_t));
		MD5_CODE = (char*) malloc(MD5_TEXT_DIGEST_SIZE+1);
		MD5_CODE[MD5_TEXT_DIGEST_SIZE] = '\0';
		temp = fopen (fn, "rb" );
		if ( temp == NULL) 
        {
        	fputs ("Error occur while open file",stderr); 
        	exit (1);
        }
        //aquire size
        fseek (temp , 0 , SEEK_END);
    	tmp_size = ftell (temp);
    	rewind (temp);

        tmp_buf = (unsigned char*) malloc (sizeof(char)*tmp_size);
        if (tmp_buf == NULL) 
        {
        	fputs ("Allocation Error",stderr); 
        	exit (2);
        }
    	//reading file
    	r = fread (tmp_buf,1,tmp_size,temp);
    	if (r != tmp_size) 
    	{
    		fputs ("Error occur while open file",stderr); exit (3);
    	}

    	//Obtain MD5
    	md5_init(state);
   		md5_append(state, tmp_buf , tmp_size);
    	md5_finish_text(state, MD5_CODE, 1);    
    	
    	//Clean
    	fclose (temp);
    	free(tmp_buf);
    	return MD5_CODE;
}
예제 #3
0
int md5_generate(char *filename,
                        char text_digest[MD5_TEXT_DIGEST_SIZE + 1])
{
    int fd = open(filename, O_RDONLY);
    
    if(fd == -1) {
        return -1;
    }
    
    md5_state_t md5_state;
    md5_init(&md5_state);
    
    char buf[4096];
    int amt = 0;
    while((amt = read(fd, buf, 4096)) > 0) {
        md5_append(&md5_state, (md5_byte_t *) buf, amt);
    }
    
    md5_finish_text(&md5_state, text_digest, 1);
    text_digest[MD5_TEXT_DIGEST_SIZE] = '\0';
    
    return 0;
}
예제 #4
0
// register_files(tracker_task, myalias)
//	Registers this peer with the tracker, using 'myalias' as this peer's
//	alias.  Also register all files in the current directory, allowing
//	other peers to upload those files from us.
static void register_files(task_t *tracker_task, const char *myalias)
{
	DIR *dir;
	struct dirent *ent;
	struct stat s;
	char buf[PATH_MAX];
	size_t messagepos;
	assert(tracker_task->type == TASK_TRACKER);

	// Register address with the tracker.
	osp2p_writef(tracker_task->peer_fd, "ADDR %s %I:%d\n",
		     myalias, listen_addr, listen_port);
	messagepos = read_tracker_response(tracker_task);
	message("* Tracker's response to our IP address registration:\n%s",
		&tracker_task->buf[messagepos]);
	if (tracker_task->buf[messagepos] != '2') {
		message("* The tracker reported an error, so I will not register files with it.\n");
		return;
	}

	// Register files with the tracker.
	message("* Registering our files with tracker\n");
	if ((dir = opendir(".")) == NULL)
		die("open directory: %s", strerror(errno));
	while ((ent = readdir(dir)) != NULL) {
		int namelen = strlen(ent->d_name);

		// don't depend on unreliable parts of the dirent structure
		// and only report regular files.  Do not change these lines.
		if (stat(ent->d_name, &s) < 0 || !S_ISREG(s.st_mode)
		    || (namelen > 2 && ent->d_name[namelen - 2] == '.'
			&& (ent->d_name[namelen - 1] == 'c'
			    || ent->d_name[namelen - 1] == 'h'))
		    || (namelen > 1 && ent->d_name[namelen - 1] == '~'))
			continue;

		/* (Alan) calculate md5 */
		//start
		md5_state_t *md5_state;
		char taskbuf[TASKBUFSIZ + 1];
		taskbuf[TASKBUFSIZ] = '\0';
		char md5[MD5_TEXT_DIGEST_SIZE + 1];
		md5[MD5_TEXT_DIGEST_SIZE] = '\0';
	
		md5_state = (md5_state_t *) malloc(sizeof(md5_state_t));
		md5_init(md5_state);
		int bytes, file = open(ent->d_name, O_RDONLY);
		while ((bytes = read(file, taskbuf, TASKBUFSIZ)) > 0)
			md5_append(md5_state, (md5_byte_t *) &taskbuf, bytes);
		md5_finish_text(md5_state, md5, 1);
		free(md5_state);
		//end
		
		pthread_mutex_lock (&tracker_mutex);  // (Brian)
		osp2p_writef(tracker_task->peer_fd, "HAVE %s\n", ent->d_name, md5);	/* (Alan) add md5 argument */
		pthread_mutex_unlock (&tracker_mutex);  // (Brian)
		messagepos = read_tracker_response(tracker_task);
		if (tracker_task->buf[messagepos] != '2')
			error("* Tracker error message while registering '%s':\n%s",
			      ent->d_name, &tracker_task->buf[messagepos]);
	}

	closedir(dir);
}