예제 #1
0
파일: server.c 프로젝트: fs302/FileServer
// 等待握手
int WaitShakeHands(char *message, FILE **fp)
{
    printf("Waiting request ...\n");
    Packet fnpack;
    Recvfrom(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, &clen);
    bzero(message, FILE_NAME_MAX_SIZE+1);
    strncpy(message, fnpack.data, fnpack.dataLength>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:fnpack.dataLength);
    // dataID==-1 表示下载请求
    if (fnpack.dataID == -1)
    {
        printf("Request File name: %s\n",message);
        *fp = fopen(message, "rb");
        if (NULL == *fp)
        {
            printf("File:\t %s is not found.\n",message);
            fnpack.dataID = -5;
            fnpack.dataLength = 0;
            strncpy(fnpack.data, NULL, 0);
            Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
            return -5;
        }
        printf("File:\t %s Open.\n",message);
        fnpack.dataID = -1;
        fnpack.dataLength = 0;
        strncpy(fnpack.data, NULL, 0);
        Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
        return 1;
    }// dataID == -2 表示显示目录请求
    else if( fnpack.dataID == -2)
    {
        Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
        return 2;
    }// dataID == -3 表示上传请求
    else if( fnpack.dataID == -3)
    {
        // 先验证md5,若存在同md5文件,则启动秒传
        if (check_md5(message)==1) 
        {
            fnpack.dataID = -5;
            fnpack.dataLength = 0;
            strncpy(fnpack.data, NULL, 0);
            Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
            return -5;
        }
        else{
            fnpack.dataID = 0; // Wait for packet 0
            fnpack.dataLength = 0;
            fnpack.flag = -1;
            strncpy(fnpack.data,NULL, 0);
            Sendto(server_socket, (char *)&fnpack, sizeof(Packet), 0, (struct sockaddr *)&client_addr, clen);
            return 3;
        }
    }
}
예제 #2
0
// program start
int _tmain(int argc, TCHAR** argv) {
	//SET_UNICODE_MODE;
	STL_SETLOCALE_JAPAN;

	kjm::optionInfoList opts;
	opts.push_back(kjm::optionInfo(_T("binary"), _T("b"), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("check"), _T("c"), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("text"), _T("t"), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("output"), _T("o"), kjm::required_argument));
	opts.push_back(kjm::optionInfo(_T("quiet"), _T(""), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("status"), _T(""), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("warn"), _T("w"), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("help"), _T(""), kjm::no_argument));
	opts.push_back(kjm::optionInfo(_T("version"), _T(""), kjm::no_argument));

	kjm::cmdLine cmd;
	cmd.parse(argc, argv, opts);

	int exit_code = 0;

	if (cmd.hasOption(_T("version"))) {
		version();	// バージョンを表示して正常終了。
	}

	if (cmd.hasOption(_T("help"))) {
		usage( EXIT_SUCCESS );	// 使用法を表示して正常終了。
	}

	for (int i = 0; i < cmd.get_argCount(); i++) {
		kjm::_tstrings files = kjm::directory::glob(cmd.get_argument(i));

		for (int j = 0; j < files.size(); j++) {
			if (cmd.hasOption(_T("check"))) {
				if (check_md5(files[j], cmd) != 0) {
					exit_code = 1;
				}

				if (exit_code != 0 && cmd.hasOption(_T("status")) != false) {
					break;
				}
			} else {
				get_md5(files[j], cmd);
			}
		}
	}

	return exit_code;
}
예제 #3
0
object_t *server_write_to_cache(session_t *session, msgidx_t *msgidx)
{

    vnode_t *vnode = get_vnode_by_key(SERVER(session), msgidx->key_md5);
    assert(vnode != NULL);
    object_queue_t *caching_objects = vnode->caching_objects;

    int blockid = msgidx->message->id;
    uint32_t object_size = msgidx->object_size; 
    const char *write_buf = msgidx->data;
    uint32_t write_bytes = msgidx->data_size;;

    object_t obj;
    memset(&obj, 0, sizeof(object_t));
    memcpy(&obj.key_md5, msgidx->key_md5, sizeof(md5_value_t));
    object_t *object = object_queue_find(caching_objects, &obj);
    /*object_t *object = object_queue_find(caching_objects, &msgidx->key_md5);*/

    if ( object == NULL ){
        object = object_new(msgidx->key, msgidx->keylen);
        object->object_size = object_size;

        if ( check_md5(&object->key_md5, msgidx->key_md5) != 0 ){
            error_log("Check md5 error. session(%d), block_id:%d, key:%s object->key:%s", session->id, blockid, msgidx->key, object->key);
            /*assert(1==0);*/
            object_free(object);
            return NULL;
        }

        object_queue_insert(caching_objects, object);
    }

    object_add_slice(object, write_buf, write_bytes);
    /*slice_t *slice = slice_new(); */
    /*slice->seq_num = blockid;*/
    /*byte_block_write(&slice->byteblock, write_buf, write_bytes);*/
    /*listAddNodeTail(object->slices, slice);*/

    session->total_writed += msgidx->data_size;

    return object;

}
예제 #4
0
object_t *session_write_to_cache(session_t *session, msgidx_t *msgidx){

    vnode_t *vnode = get_vnode_by_key(SERVER(session), msgidx->key_md5);
    assert(vnode != NULL);
    object_queue_t *caching_objects = vnode->caching_objects;

    int blockid = msgidx->message->id;
    uint32_t object_size = msgidx->object_size; 
    const char *write_buf = msgidx->data;
    uint32_t write_bytes = msgidx->data_size;;

    object_t obj;
    memcpy(&obj.key_md5, msgidx->key_md5, sizeof(md5_value_t));

    object_t *object = object_queue_find(caching_objects, &obj);
    if ( object == NULL ){
        object = object_new(msgidx->key, msgidx->keylen);
        object->object_size = object_size;

        assert(check_md5(&object->key_md5, msgidx->key_md5) == 0 );

        pthread_mutex_lock(&caching_objects->queue_lock);
        object_queue_insert(caching_objects, object);
        pthread_mutex_unlock(&caching_objects->queue_lock);
    }

    slice_t *slice = slice_new(); 
    slice->seq_num = blockid;
    byte_block_write(&slice->byteblock, write_buf, write_bytes);

    listAddNodeTail(object->slices, slice);

    session->total_writed += msgidx->data_size;

    return object;

}
예제 #5
0
int
downloadfile(CURL *curlhandle, char *url, char *range)
{
	CURLcode		curlcode;
	int			retval;
#ifdef	TIMEIT
	struct timeval		start_time, end_time;
	unsigned long long	s, e;
#endif

	if ((curlcode = curl_easy_setopt(curlhandle, CURLOPT_URL, url)) !=
			CURLE_OK) {
		logmsg("downloadfile:curl_easy_setopt():failed:(%d)\n",
			curlcode);
		return(-1);
	}

#ifdef	DEBUG
	logmsg("URL : ");
	logmsg(url);
	logmsg("\n");
#endif

	if ((curlcode = curl_easy_setopt(curlhandle, CURLOPT_RANGE,
			range)) != CURLE_OK) {
		logmsg("downloadfile:curl_easy_setopt():failed:(%d)\n",
				curlcode);
		return(-1);
	}

#ifdef	TIMEIT
	gettimeofday(&start_time, NULL);
#endif

	if (MD5_Init(&context) != 1) {
		fprintf(stderr, "MD5_Init failed\n");
		exit(-1);
	}

	if ((curlcode = curl_easy_perform(curlhandle)) != CURLE_OK) {
		logmsg("downloadfile:curl_easy_perform():failed:(%d)\n",
			curlcode);
		return(-1);
	}

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("downloadfile:svc time: %lld usec:url %s\n", (e - s), url);
	curltime = e - s;
#endif

	/*
	 * if this is not a range request, then do an MD5 checksum of the file
	 */
	if (range == NULL) {
		retval = check_md5(url);

		/*
		 * check_md5 returns:
		 *
		 *	0 - the filename is not found in the packages.md5 file
		 *
		 *	1 - checksum passed
		 *
		 *	-1 - checksum failed
		 */
		if (retval == -1) {
			return(-1);
		}
	}

	return(0);
}
예제 #6
0
int main(int argc, char** argv)
{
    int ret;
    int i;

    (void)argc;
    (void)argv;

#if defined(MICROCHIP_PIC32)
    init_serial() ;  /* initialize PIC32MZ serial I/O */
    SYSTEMConfigPerformance(80000000);
    DBINIT();
#endif

    /* align key, iv pointers */
    key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_KEY);
    if (key == NULL) {
        printf("mcapi key alloc failed\n");
        return -1;
    }

    iv = (byte*)XMALLOC(16, NULL, DYNAMIC_TYPE_KEY);
    if (iv == NULL) {
        printf("mcapi iv alloc failed\n");
        return -1;
    }

    for (i = 0; i < OUR_DATA_SIZE; i++)
        ourData[i] = (byte)i;

    ret = check_md5();
    if (ret != 0) {
        printf("mcapi check_md5 failed\n");
        return -1;
    }

    ret = check_sha();
    if (ret != 0) {
        printf("mcapi check_sha failed\n");
        return -1;
    }

    ret = check_sha256();
    if (ret != 0) {
        printf("mcapi check_sha256 failed\n");
        return -1;
    }

    ret = check_sha384();
    if (ret != 0) {
        printf("mcapi check_sha384 failed\n");
        return -1;
    }

    ret = check_sha512();
    if (ret != 0) {
        printf("mcapi check_sha512 failed\n");
        return -1;
    }

    ret = check_hmac();
    if (ret != 0) {
        printf("mcapi check_hmac failed\n");
        return -1;
    }

    ret = check_compress();
    if (ret != 0) {
        printf("mcapi check_compress failed\n");
        return -1;
    }

    ret = check_rng();
    if (ret != 0) {
        printf("mcapi check_rng failed\n");
        return -1;
    }

    ret = check_des3();
    if (ret != 0) {
        printf("mcapi check_des3 failed\n");
        return -1;
    }

    ret = check_aescbc();
    if (ret != 0) {
        printf("mcapi check_aes cbc failed\n");
        return -1;
    }

    ret = check_aesctr();
    if (ret != 0) {
        printf("mcapi check_aes ctr failed\n");
        return -1;
    }

    ret = check_aesdirect();
    if (ret != 0) {
        printf("mcapi check_aes direct failed\n");
        return -1;
    }

    ret = check_rsa();
    if (ret != 0) {
        printf("mcapi check_rsa failed\n");
        return -1;
    }

    ret = check_ecc();
    if (ret != 0) {
        printf("mcapi check_ecc failed\n");
        return -1;
    }

    XFREE(iv,  NULL, DYNAMIC_TYPE_KEY);
    XFREE(key, NULL, DYNAMIC_TYPE_KEY);

    return 0;
}
예제 #7
0
파일: cuci.c 프로젝트: lonegoli/R7000
static void thread_imageUpgrade(void * args)
{
	pthread_detach(pthread_self());
	cJSON *valueSetObj= cJSON_CreateObject();
	struct upgradeArg *Pargs;
	s_config *config = config_get_config();
	int flag = 0;
	char file_name[1024], md5[64], id[32], exec[128], request[MAX_BUF];
	Pargs = (struct upgradeArg *)args;

	strcpy(file_name, Pargs->file_name);
	strcpy(md5, Pargs->md5);
	strcpy(id, Pargs->id);
	sleep(2);
	safe_free(Pargs);

	signal(SIGCHLD,SIG_DFL);
	do {
		//system("mkdir /tmp/download");
		//system("rm -rf /tmp/download/EliteAgent");
		if(download_file(file_name, DOWNIMAGEDIR)) {
			debug(LOG_ERR,"Download file failed.");
			create_http_json(valueSetObj, id, INFORM, IMAGEUPGRADESTATUS, "failed", "6", "Failed to download agent file", config->sn, request);
			break;
		}
		system("chmod 777 " DOWNIMAGEDIR);
		if(check_md5(DOWNIMAGEDIR, md5)) {
			debug(LOG_ERR,"MD5 check failed.");
			create_http_json(valueSetObj, id, INFORM, IMAGEUPGRADESTATUS, "failed", "8", "MD5 check failed", config->sn, request);
			break;
		}
		flag = 1;
		create_http_json(valueSetObj, id, INFORM, IMAGEUPGRADESTATUS, "success", "0", NULL, config->sn, request);
	}while(0);
	
	{
		int sockfd;
		ssize_t totalbytes;
		//debug(LOG_INFO, "Begin to build the connection with the server and then send trap");
		sockfd = try_connect_to_server();
		if(sockfd == -1) {
			return;
		}
		debug(LOG_INFO, "Send imageUpgrade info: %s", request);
		totalbytes = safe_encrypt_http_send(sockfd,request,strlen(request),0);
		debug(LOG_DEBUG, "Send %d bytes",totalbytes);
		safe_decrypt_http_read(sockfd, 3,  request);
		//debug(LOG_DEBUG, "Push real time trap end");
		shutdown(sockfd, 2);
		safe_close(sockfd);
		if(flag) {
			
			debug(LOG_INFO, "Upgrade image...");
			system("write "DOWNIMAGEDIR" linux");
			system("reboot");
		}
		system("rm -rf "DOWNIMAGEDIR);
	}
	
	config->upgrade_lock = 0;
	pthread_mutex_unlock(&sMutex);
	
}
예제 #8
0
/**
 * downloadfile it downloads the file and checks for integrity
 * if some errors are found it return -1 otherwise 0
 */
int
downloadfile(CURL *curlhandle, char *url, char *filename, FILE * fp, char *realfilename)
{
	CURLcode		curlcode;
#ifdef	TIMEIT
	struct timeval		start_time, end_time;
	unsigned long long	s, e;
#endif

	if ((curlcode = curl_easy_setopt(curlhandle, CURLOPT_URL, url)) !=
			CURLE_OK) {
		logmsg("downloadfile:curl_easy_setopt():failed:(%d)\n",
			curlcode);
		return(-1);
	}

	if ( isRpm == 0) {
		if (MD5_Init(&context) != 1) {
			fprintf(stderr, "MD5_Init failed\n");
			exit(-1);
		}
	}

#ifdef	DEBUG
	logmsg("URL : ");
	logmsg(url);
	logmsg("\n");
#endif

#ifdef	TIMEIT
	gettimeofday(&start_time, NULL);
#endif

	if ((curlcode = curl_easy_perform(curlhandle)) != CURLE_OK) {
		logmsg("downloadfile:curl_easy_perform():failed:(%d)\n",
			curlcode);
		return(-1);
	}

#ifdef	TIMEIT
	gettimeofday(&end_time, NULL);
	s = (start_time.tv_sec * 1000000) + start_time.tv_usec;
	e = (end_time.tv_sec * 1000000) + end_time.tv_usec;
	logmsg("downloadfile:svc time: %lld usec:url %s\n", (e - s), url);
	curltime = e - s;
#endif

	fclose(fp);
	if ( isRpm ){
		//this is not an rpm we can't do any checking
		logmsg("downloadfile:isRpm:file %s is an rpm going to verify\n", filename);
		return verifyRpmPackage(filename);
	}
	else{
		//verify 'normal' file
		// we need real file name to lookup it's name in /tmp/product/packages.md5

		/*
		 * check_md5 returns:
		 *
		 *	0 - the filename is not found in the packages.md5 file
		 *
		 *	1 - checksum passed
		 *
		 *	-1 - checksum failed
		 */
		
		if( check_md5(realfilename) == -1)
			//return error
			return -1;
		else 
			return 0;
	}
}
예제 #9
0
int
do_recv (const char *answer)
{
	if (get_ipv4 () < 0)
	{
		fprintf (stderr, "ERROR on getting IPv4 address, exiting...\n");
		return -1;
	}
	
	struct sockaddr_in my_addr, peer_addr;
	
	unsigned int file_size = 0, total_bytes_read = 0, tmp_file_size;
	ssize_t nread = 0, tx = 0;
	size_t socket_len = 0;
	void *filebuffer = NULL;
	char *filename = NULL;
	char yes_or_no[2], hash[33];
	
	/* Info sul server locale */
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = 15000;
	my_addr.sin_addr.s_addr = INADDR_ANY;
	
	init_signals ();

	if ((fsd.sockd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
	{
		fprintf (stderr, "Error on socket creation\n");
		return -1;
	}

	if (bind (fsd.sockd, (struct sockaddr *) &my_addr, sizeof (struct sockaddr_in)) < 0)
	{
		fprintf (stderr, "Bind error\n");
		return -1;
	}
	
	if (listen (fsd.sockd, 10) < 0)
	{
		fprintf (stderr, "Error on listen");
		return -1;
	}
	
	new_conn:
	socket_len = sizeof (peer_addr);
	if ((fsd.newsockd = accept (fsd.sockd, (struct sockaddr *) &peer_addr, (socklen_t *) &socket_len)) < 0)
	{
		fprintf (stderr, "Connection error (accept)\n");
		return -1;
  	}
	
	file_size = total_bytes_read = nread = 0;
	memset( yes_or_no, 0, sizeof (yes_or_no));
	if (recv (fsd.newsockd, &file_size, sizeof (file_size), 0) < 0)
	{
		fprintf(stderr, "Error on receiving the file name length\n");
   		return -1;
	}
	
	filename = (char *) malloc (file_size);
	if (filename == NULL)
	{
		fprintf(stderr, "Error during filename memory allocation\n");
		return -1;
	}
	if (recv (fsd.newsockd, filename, file_size, 0) < 0)
	{
   		fprintf(stderr, "Error on receiving the file name\n");
   		free(filename);
   		return -1;
	}
	file_size = 0;
	if (recv (fsd.newsockd, &file_size, sizeof(file_size), 0) < 0)
	{
		fprintf (stderr, "Error on receiving the file size\n");
   		free (filename);
		return -1;
	}
    
	if (strcmp (answer, "y") == 0)
	{
		strcpy (yes_or_no, "Y");
		goto auto_accept;
	}

	printf ("Do you want to receive the file '%s' which size is '%"PRIu32"' bytes? (Y or N)\n", filename, file_size);
	
	another_yorn:
	if(scanf("%1s", yes_or_no) == EOF)
	{
	   	fprintf (stderr, "Scanf error\n");
		free (filename);
		return -1;
	}
	yes_or_no[0] = toupper (yes_or_no[0]);
	if (strcmp (yes_or_no, "Y") != 0)
	{
	   	if (strcmp (yes_or_no, "N") != 0)
	   	{
			printf ("You have to write Y or N, try again: ");
			memset (yes_or_no, 0, sizeof (yes_or_no));
			goto another_yorn;
		}
	}
	auto_accept:
	if (strcmp (yes_or_no, "N") == 0)
	{
	   	printf ("Transfer aborted\n");
		if (send (fsd.newsockd, yes_or_no, 2, 0) < 0)
		{
			printf ("Error on sending N\n");
			free (filename);
			return -1;   
		}
		close (fsd.newsockd);
		free (filename);
		goto new_conn;
	}
	else
	{
		if (send (fsd.newsockd, yes_or_no, 2, 0) < 0)
		{
			fprintf (stderr, "Error on sending Y\n");
			free (filename);
			return -1;
		}
	}
	
	tmp_file_size = file_size;
	filebuffer = malloc (file_size);
	if (filebuffer == NULL)
	{
		fprintf (stderr, "Error during filebuffer memory allocation\n");
		free (filename);
		return -1;
	}
	
	fsd.fd = open (filename, O_CREAT | O_WRONLY, 0644);
	if (fsd.fd  < 0)
	{
		fprintf (stderr, "Error during file opening\n");
		free (filename);
		free (filebuffer);
   	  	return -1;
	}
	tx = 0;
	while((total_bytes_read != file_size) && ((nread = read(fsd.newsockd, filebuffer, tmp_file_size)) > 0))
	{
		tx += nread;
		printf ("\r%zd%%", (tx * 100 / file_size));
   	  	if (write (fsd.fd, filebuffer, nread) != nread)
   	  	{
			fprintf (stderr, "Write error\n");
			free (filename);
  		  	free (filebuffer);
			return -1;
   		}
   	  	total_bytes_read += nread;
   	  	tmp_file_size -= nread;
	}
	char *file_md5 = check_md5 (filename);
	if (recv (fsd.newsockd, hash, 33, 0) < 0)
	{
		fprintf (stderr, "Error on receiving file md5\n");
		free (filename);
		free (file_md5);
		return -1;
	}
	if (strcmp (file_md5, hash) == 0)
	{
		memset (yes_or_no, 0, sizeof(yes_or_no));
		strcpy (yes_or_no, "Y");
		if (send (fsd.newsockd, yes_or_no, 2, 0) < 0)
		{
			free (filename);
			free (file_md5);
			return -1;
		}
		printf ("\n--> File successfully received\n");
	}
	else
	{
	  	memset (yes_or_no, 0, sizeof (yes_or_no));
		strcpy (yes_or_no, "N");
		if (send (fsd.newsockd, yes_or_no, 2, 0) < 0)
		{
			close (fsd.newsockd);
			close (fsd.fd);
			free (filename);
			free (file_md5);
			goto new_conn;
		}
		printf ("\n--> File transfer FAILED, md5sum doesn't match\n");
	}
	
	free (file_md5);
	close (fsd.fd);
	free (filename);
	free (filebuffer);
	close (fsd.newsockd);
	goto new_conn;
}
예제 #10
0
int get_account_from_open_ticket( const unsigned char * buf ){
	
	char check_buf[1024] = {0};
	
	char * p = check_buf;

	char * dup_str = strdup( buf );

	char * pch = strtok( dup_str, ",");

	int account_id;

	if( pch ){

		account_id = atoi( pch );

		strncpy( check_buf , pch , 20 );	//account 
		
		p += strlen( pch );
	}else{

		free( dup_str );

		return 0;
	}

	pch = strtok( NULL , "," );

	if( pch ){
		int time = atoi(pch);
/*
		time_t now = time(NULL);

		if( now - time > 20 * 60 ){
			//key过期
			free( dup_str );
			return 0;
		}	
		*/

		strncpy( p , pch , 12 ); //unix时间戳,给个12字符上限够了
		
		p += strlen( pch );
	}else{
		free( dup_str );
		return 0;
	}

	pch = strtok( NULL , "," );

	if( !pch ){
		free( dup_str );
		return 0;
	}

	char * sign = pch;

	strncpy( p , VERIFY_SECRET_KEY , strlen(VERIFY_SECRET_KEY));

	if( check_md5( check_buf , sign ) ){
		free( dup_str );
		return account_id;
	}
			
	free( dup_str );
	return 0;
}