boost::shared_ptr< Base<EncodingT> > IDiaEnumLineNumbersPtrInterpreter<EncodingT>::next(const boost::shared_ptr< Base<EncodingT> >& celt, boost::shared_ptr< Base<EncodingT> >& rgelt, boost::shared_ptr< Base<EncodingT> >& pceltFetched)
{
	boost::shared_ptr< Base<EncodingT> > res(new Numeric<EncodingT>());
	unsigned long nativeCelt;
	IDiaLineNumberPtr nativeRgelt;
	unsigned long nativePceltFetched;
	if (check_numeric(celt, nativeCelt) && 
		check_IDiaLineNumberPtr(rgelt, nativeRgelt) && 
		check_numeric(pceltFetched, nativePceltFetched))
	{
		res.reset(new Numeric<EncodingT>(value()->Next(nativeCelt, &nativeRgelt, &nativePceltFetched)));
		reset_IDiaLineNumberPtr(rgelt, nativeRgelt);
		reset_numeric(pceltFetched, nativePceltFetched);
	}
	return res;
}
void CppEnumInterpreterAccess<EncodingT>::fillOneCppEnumConstant(boost::shared_ptr< Base<EncodingT> >& refCppEnum,
				const boost::shared_ptr< Base<EncodingT> >& identifier,
				const boost::shared_ptr< Base<EncodingT> >& nowait)
{
	clearError();
	try
	{
		bool nativeNoWait;
		boost::shared_ptr< _CppEnum<EncodingT> > nativeRefCppEnum;
		int nativeIdentifier;
		if (check_cppEnum(refCppEnum, nativeRefCppEnum) && 
			check_numeric(identifier, nativeIdentifier) &&
			check_bool(nowait, nativeNoWait))
		{
			m_object->fillOneCppEnumConstant(nativeRefCppEnum,
				nativeIdentifier,
				nativeNoWait);
			reset_cppEnum(refCppEnum, nativeRefCppEnum);
		}
	}
	catch (std::exception& e)
	{
		setError(e);
	}
}
void CppClassInterpreter<EncodingT>::setLinesCount(boost::shared_ptr< Base<EncodingT> > const& linesCount)
{
	int nativeLinesCount;
	if (check_numeric(linesCount, nativeLinesCount))
	{
		getValue()->setLinesCount(nativeLinesCount);
	}
}
void CppClassInterpreter<EncodingT>::setIsAbstract(boost::shared_ptr< Base<EncodingT> > const& isAbstract)
{
	int nativeIsAbstract;
	if (check_numeric(isAbstract, nativeIsAbstract))
	{
		getValue()->setIsAbstract(nativeIsAbstract);
	}
}
void CppClassInterpreter<EncodingT>::removeCppAttribute(boost::shared_ptr< Base<EncodingT> > const& n)
{
	int nativePosition;
	if (check_numeric(n, nativePosition))
	{
		getValue()->eraseCppAttribute(getValue()->getCppAttributesBeginning()+nativePosition);
	}
}
void CppAttributeInterpreter<EncodingT>::setIsStatic(boost::shared_ptr< Base<EncodingT> > const& isStatic)
{
	int nativeIsStatic;
	if (check_numeric(isStatic, nativeIsStatic))
	{
		getValue()->setIsStatic(nativeIsStatic);
	}
}
void CppEnumConstantInterpreter<EncodingT>::setDefaultValue(boost::shared_ptr< Base<EncodingT> > const& defaultValue)
{
	int nativeDefaultValue;
	if (check_numeric(defaultValue, nativeDefaultValue))
	{
		getValue()->setDefaultValue(nativeDefaultValue);
	}
}
void CppEnumConstantInterpreter<EncodingT>::setStartBlock(boost::shared_ptr< Base<EncodingT> > const& startBlock)
{
	int nativeStartBlock;
	if (check_numeric(startBlock, nativeStartBlock))
	{
		getValue()->setStartBlock(nativeStartBlock);
	}
}
void CppAttributeInterpreter<EncodingT>::setIsConst(boost::shared_ptr< Base<EncodingT> > const& isConst)
{
	int nativeIsConst;
	if (check_numeric(isConst, nativeIsConst))
	{
		getValue()->setIsConst(nativeIsConst);
	}
}
void CppAttributeInterpreter<EncodingT>::setLineNumber(boost::shared_ptr< Base<EncodingT> > const& lineNumber)
{
	int nativeLineNumber;
	if (check_numeric(lineNumber, nativeLineNumber))
	{
		getValue()->setLineNumber(nativeLineNumber);
	}
}
boost::shared_ptr< Base<EncodingT> > IDiaSourceFilePtrInterpreter<EncodingT>::get_checksum(boost::shared_ptr< Base<EncodingT> >& cbData, boost::shared_ptr< Base<EncodingT> >& pcbData, boost::shared_ptr< Base<EncodingT> >& data)
{
	boost::shared_ptr< Base<EncodingT> > res(new Numeric<EncodingT>());
	unsigned long nativeCbData;
	unsigned long nativePcbData;
	std::vector< unsigned char > nativeData;
	if (check_numeric(cbData, nativeCbData) && 
		check_numeric(pcbData, nativePcbData) && 
		check_array(data, nativeData, check_numeric<EncodingT, unsigned char>))
	{
		res.reset(new Numeric<EncodingT>(value()->get_checksum(nativeCbData, &nativePcbData, nativeData.data())));
		reset_numeric(cbData, nativeCbData);
		reset_numeric(pcbData, nativePcbData);
		reset_array(data, nativeData, reset_numeric<EncodingT, unsigned char>);
	}
	return res;
}
void CppClassInterpreter<EncodingT>::setIsInterface(boost::shared_ptr< Base<EncodingT> > const& isInterface)
{
	int nativeIsInterface;
	if (check_numeric(isInterface, nativeIsInterface))
	{
		getValue()->setIsInterface(nativeIsInterface);
	}
}
void CppAttributeInterpreter<EncodingT>::setLengthBlock(boost::shared_ptr< Base<EncodingT> > const& lengthBlock)
{
	int nativeLengthBlock;
	if (check_numeric(lengthBlock, nativeLengthBlock))
	{
		getValue()->setLengthBlock(nativeLengthBlock);
	}
}
Ejemplo n.º 14
0
//disgusting function because they can't seem to use a standard
//date format...
void reformat_date(const char * src, char * dest) {
	if (src == NULL) return;
	std::size_t len = std::strlen(src);
	std::size_t offset = 0;
	if (len < 8 || len > 10) {
		throw parse_error("invalid date format");
	}
	check_numeric(src[0]);
	if (src[1] == '-') {
		offset = 2;
		dest[8] = '0';
		dest[9] = src[0];
	}
	else {
		offset = 3;
		check_numeric(src[1]);
		check_dash(src[2]);
		dest[8] = src[0];
		dest[9] = src[1];
	}
	dest[7] = '-';
	check_numeric(src[offset]);
	if (src[offset+1] == '-') {
		dest[5] = '0';
		dest[6] = src[offset];
		offset += 2;
	}
	else {
		check_numeric(src[offset+1]);
		check_dash(src[offset+2]);
		dest[5] = src[offset];
		dest[6] = src[offset+1];
		offset += 3;
	}
	dest[4] = '-';
	if (len - offset != 4) {
		throw parse_error("invalid date format");
	}
	for (int i = 0; i < 4; ++i) {
		check_numeric(src[offset+i]);
		dest[i] = src[offset+i];
	}
	dest[10] = '\0';
	//DONE!
}
Ejemplo n.º 15
0
static int get_passive_sock(UrlResource *rsrc, int control)
{
    unsigned char *addr;
    struct sockaddr_in sa;
    int sock;
    int x;
    char *line, *orig_line;

    send_control(control, "PASV\r\n", NULL);

    if( !((line = get_line(rsrc, control)) &&
          check_numeric("227", line)) ) {
        nvfree(line);
        return 0;
    }

    orig_line = line;

    if (strlen(line) < 4) {
        nvfree(line);
        return 0;
    }

    if (!(sock = sock_init(rsrc->op, &sa, control))) return -1;

    /* skip the numeric response */
    line += 4;

    /* then find the digits */
        
    while (!(isdigit(*line))) line++;
    
    /* ugliness from snarf 1.x */

    sa.sin_family = AF_INET;
    addr = (unsigned char *)&sa.sin_addr;

    for(x = 0; x < 4; x++) {
        addr[x] = atoi(line);
        line = strchr(line,',') + 1;
    }

    addr = (unsigned char *)&sa.sin_port ;
    addr[0] = atoi(line);
    line = strchr(line,',') + 1;
    addr[1] = atoi(line);
        
    if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
        nvfree(orig_line);
        ui_error(rsrc->op, "unable to connect (%s)", strerror(errno));
        return -1;
    }

    nvfree(orig_line);
    return sock;

} /* get_passive_sock() */
void CppClassInterpreter<EncodingT>::insertCppAttribute(boost::shared_ptr< Base<EncodingT> > const& n, boost::shared_ptr< Base<EncodingT> > const& cppAttribute)
{
	int nativePosition;
	boost::shared_ptr< _CppAttribute<EncodingT> > nativeCppAttribute;
	if (check_numeric(n, nativePosition) &&
		check_cppAttribute(cppAttribute, nativeCppAttribute))
	{
		getValue()->insertCppAttribute(getValue()->getCppAttributesBeginning()+nativePosition, nativeCppAttribute);
	}
}
boost::shared_ptr< Base<EncodingT> > CppClassInterpreter<EncodingT>::getCppAttribute(boost::shared_ptr< Base<EncodingT> > const& n)
{
	boost::shared_ptr< Base<EncodingT> > res(new CppAttributeInterpreter<EncodingT>());
	int nativePosition;
	if (check_numeric(n, nativePosition))
	{
		res.reset(new CppAttributeInterpreter<EncodingT>(getValue()->getCppAttributeAt(nativePosition)));
	}
	return res;
}
boost::shared_ptr< Base<EncodingT> > IDiaEnumLineNumbersPtrInterpreter<EncodingT>::skip(const boost::shared_ptr< Base<EncodingT> >& celt)
{
	boost::shared_ptr< Base<EncodingT> > res(new Numeric<EncodingT>());
	unsigned long nativeCelt;
	if (check_numeric(celt, nativeCelt))
	{
		res.reset(new Numeric<EncodingT>(value()->Skip(nativeCelt)));
	}
	return res;
}
boost::shared_ptr< Base<EncodingT> > IDiaEnumLineNumbersPtrInterpreter<EncodingT>::get_Count(boost::shared_ptr< Base<EncodingT> >& pRetVal)
{
	boost::shared_ptr< Base<EncodingT> > res(new Numeric<EncodingT>());
	long nativePRetVal;
	if (check_numeric(pRetVal, nativePRetVal))
	{
		res.reset(new Numeric<EncodingT>(value()->get_Count(&nativePRetVal)));
		reset_numeric(pRetVal, nativePRetVal);
	}
	return res;
}
CppEnumConstantInterpreter<EncodingT>::CppEnumConstantInterpreter(boost::shared_ptr< Base<EncodingT> > const& name,
				boost::shared_ptr< Base<EncodingT> > const& defaultValue,
				boost::shared_ptr< Base<EncodingT> > const& startBlock,
				boost::shared_ptr< Base<EncodingT> > const& lengthBlock)
{
	typename EncodingT::string_t nativeName;
	int nativeDefaultValue;
	int nativeStartBlock;
	int nativeLengthBlock;
	if (check_string<EncodingT>(name, nativeName) &&
		check_numeric(defaultValue, nativeDefaultValue) &&
		check_numeric(startBlock, nativeStartBlock) &&
		check_numeric(lengthBlock, nativeLengthBlock))
	{
		setValue(boost::shared_ptr< _CppEnumConstant<EncodingT> >(new _CppEnumConstant<EncodingT>(nativeName,
				nativeDefaultValue,
				nativeStartBlock,
				nativeLengthBlock)));
	}
}
CppAttributeInterpreter<EncodingT>::CppAttributeInterpreter(boost::shared_ptr< Base<EncodingT> > const& attrType,
				boost::shared_ptr< Base<EncodingT> > const& name,
				boost::shared_ptr< Base<EncodingT> > const& accessSpecifier,
				boost::shared_ptr< Base<EncodingT> > const& isStatic,
				boost::shared_ptr< Base<EncodingT> > const& isConst,
				boost::shared_ptr< Base<EncodingT> > const& constValue,
				boost::shared_ptr< Base<EncodingT> > const& lineNumber,
				boost::shared_ptr< Base<EncodingT> > const& startBlock,
				boost::shared_ptr< Base<EncodingT> > const& lengthBlock)
{
	typename EncodingT::string_t nativeAttrType;
	typename EncodingT::string_t nativeName;
	typename EncodingT::string_t nativeAccessSpecifier;
	int nativeIsStatic;
	int nativeIsConst;
	typename EncodingT::string_t nativeConstValue;
	int nativeLineNumber;
	int nativeStartBlock;
	int nativeLengthBlock;
	if (check_string<EncodingT>(attrType, nativeAttrType) &&
		check_string<EncodingT>(name, nativeName) &&
		check_string<EncodingT>(accessSpecifier, nativeAccessSpecifier) &&
		check_numeric(isStatic, nativeIsStatic) &&
		check_numeric(isConst, nativeIsConst) &&
		check_string<EncodingT>(constValue, nativeConstValue) &&
		check_numeric(lineNumber, nativeLineNumber) &&
		check_numeric(startBlock, nativeStartBlock) &&
		check_numeric(lengthBlock, nativeLengthBlock))
	{
		setValue(boost::shared_ptr< _CppAttribute<EncodingT> >(new _CppAttribute<EncodingT>(nativeAttrType,
				nativeName,
				nativeAccessSpecifier,
				nativeIsStatic,
				nativeIsConst,
				nativeConstValue,
				nativeLineNumber,
				nativeStartBlock,
				nativeLengthBlock)));
	}
}
boost::shared_ptr< Base<EncodingT> > IDiaEnumLineNumbersPtrInterpreter<EncodingT>::item(const boost::shared_ptr< Base<EncodingT> >& index, boost::shared_ptr< Base<EncodingT> >& symbol)
{
	boost::shared_ptr< Base<EncodingT> > res(new Numeric<EncodingT>());
	long nativeIndex;
	IDiaLineNumberPtr nativeSymbol;
	if (check_numeric(index, nativeIndex) && 
		check_IDiaLineNumberPtr(symbol, nativeSymbol))
	{
		res.reset(new Numeric<EncodingT>(value()->Item(nativeIndex, &nativeSymbol)));
		reset_IDiaLineNumberPtr(symbol, nativeSymbol);
	}
	return res;
}
Ejemplo n.º 23
0
int get_sock(UrlResource *rsrc, int control)
{
	struct sockaddr_in sa;
	unsigned char *addr;
	unsigned char *port;
	char *line;
	char port_string[BUFSIZE];
	int sock;
	socklen_t i;

	if ( (sock = sock_init(&sa, control)) < 0 )
		return -1;


	if ( S_LISTEN(sock, 0) < 0 )
	{
        S_CLOSE(sock);
		LIBNET_DEBUG("listen");
		return -1;
	}

	i = sizeof(sa);

	S_GETSOCKNAME(sock, (struct sockaddr *)&sa, &i);

	addr = (unsigned char *)(&sa.sin_addr.s_addr);
	port = (unsigned char *)(&sa.sin_port);

	sprintf(port_string, "PORT %d,%d,%d,%d,%d,%d\r\n",
			addr[0], addr[1], addr[2], addr[3],
			port[0], (unsigned char)port[1]);

	send_control(control, port_string, NULL);

	if ( !((line = get_line(rsrc, control)) &&
			check_numeric("200", line)) )
	{
		safe_free(line);
        S_CLOSE(sock);
		return -1;
	}
	safe_free(line);

	return sock;
}
boost::shared_ptr< Base<EncodingT> > CppEnumInterpreterAccess<EncodingT>::getOneCppEnum(boost::shared_ptr< Base<EncodingT> > const& identifier)
{
	boost::shared_ptr< Base<EncodingT> > res(new CppEnumInterpreter<EncodingT>());
	clearError();
	try
	{
		int nativeIdentifier;
		if (check_numeric(identifier, nativeIdentifier))
		{
			res.reset(new CppEnumInterpreter<EncodingT>(m_object->getOneCppEnum(nativeIdentifier)));
		}
	}
	catch (std::exception& e)
	{
		setError(e);
	}
	return res;
}
Ejemplo n.º 25
0
static int get_sock(UrlResource *rsrc, int control)
{
    struct sockaddr_in sa;
    unsigned char *addr;
    unsigned char *port;
    char *line;
    char port_string[SNARF_BUFSIZE];
    unsigned int sock;
    socklen_t i;

    if (!(sock = sock_init(rsrc->op, &sa, control))) return 0;

        
    if (listen(sock, 0) < 0) {
        ui_error(rsrc->op, "unable to listen (%s)", strerror(errno));
        return 0;
    }
        
    i = sizeof(sa);

    getsockname(sock, (struct sockaddr *)&sa, &i);

    addr = (unsigned char *)(&sa.sin_addr.s_addr);
    port = (unsigned char *)(&sa.sin_port);

    sprintf(port_string, "PORT %d,%d,%d,%d,%d,%d\r\n", 
            addr[0], addr[1], addr[2], addr[3],
            port[0],(unsigned char)port[1]);

    send_control(control, port_string, NULL);

    if (!((line = get_line(rsrc, control)) && check_numeric("200", line))) {
        nvfree(line);
        return 0;
    }
    nvfree(line);
        
    return sock;

} /* get_sock() */
boost::shared_ptr< Base<EncodingT> > CppEnumInterpreterAccess<EncodingT>::selectOneCppEnum(boost::shared_ptr< Base<EncodingT> > const& identifier,
				const boost::shared_ptr< Base<EncodingT> >& nowait)
{
	boost::shared_ptr< Base<EncodingT> > res(new CppEnumInterpreter<EncodingT>());
	clearError();
	try
	{
		bool nativeNoWait;
		int nativeIdentifier;
		if (check_numeric(identifier, nativeIdentifier) &&
			check_bool(nowait, nativeNoWait))
		{
			res.reset(new CppEnumInterpreter<EncodingT>(m_object->selectOneCppEnum(nativeIdentifier,
				nativeNoWait)));
		}
	}
	catch (std::exception& e)
	{
		setError(e);
	}
	return res;
}
CppClassInterpreter<EncodingT>::CppClassInterpreter(boost::shared_ptr< Base<EncodingT> > const& name,
				boost::shared_ptr< Base<EncodingT> > const& isStruct,
				boost::shared_ptr< Base<EncodingT> > const& isInterface,
				boost::shared_ptr< Base<EncodingT> > const& isAbstract,
				boost::shared_ptr< Base<EncodingT> > const& linesCount,
				boost::shared_ptr< Base<EncodingT> > const& lineNumber,
				boost::shared_ptr< Base<EncodingT> > const& startBlock,
				boost::shared_ptr< Base<EncodingT> > const& lengthBlock)
{
	typename EncodingT::string_t nativeName;
	int nativeIsStruct;
	int nativeIsInterface;
	int nativeIsAbstract;
	int nativeLinesCount;
	int nativeLineNumber;
	int nativeStartBlock;
	int nativeLengthBlock;
	if (check_string<EncodingT>(name, nativeName) &&
		check_numeric(isStruct, nativeIsStruct) &&
		check_numeric(isInterface, nativeIsInterface) &&
		check_numeric(isAbstract, nativeIsAbstract) &&
		check_numeric(linesCount, nativeLinesCount) &&
		check_numeric(lineNumber, nativeLineNumber) &&
		check_numeric(startBlock, nativeStartBlock) &&
		check_numeric(lengthBlock, nativeLengthBlock))
	{
		setValue(boost::shared_ptr< _CppClass<EncodingT> >(new _CppClass<EncodingT>(nativeName,
				nativeIsStruct,
				nativeIsInterface,
				nativeIsAbstract,
				nativeLinesCount,
				nativeLineNumber,
				nativeStartBlock,
				nativeLengthBlock)));
	}
}
Ejemplo n.º 28
0
int ftp_transfer(UrlResource *rsrc, libnet_callback notify)
{
	Url 	*u		= NULL;
	FILE 	*out		= NULL;
	char 	*line		= NULL;
	int 	sock		= -1;
	int 	data_sock	= -1;
	int	passive		= 1;
	int	retval		= 0;
    int msg_code    = 0;

	u = rsrc->url;

	/* first of all, if this is proxied, just pass it off to the
	   http module, since that's how we support proxying. */

	rsrc->proxy = get_proxy("FTP_PROXY");

	if ( rsrc->proxy )
	{
		return http_transfer(rsrc, notify);
	}

	ftp_set_defaults(rsrc, u);

	sock = util_tcp_connect(u->host, u->port);
	if (sock < 0)
	{
        if (rsrc->running)
        {
		    notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)-NET_ERR_CONNECT_FAILED);
        }
        //S_CLOSE(sock);
		return 0;
	}

	if ( !(line = get_line(rsrc, sock)) )
	{
        if (rsrc->running)
        {
            notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)-NET_ERR_FTP_SERVER_ERROR);
        }
        S_CLOSE(sock);
		return 0;
	}

	if ( !check_numeric("220", line) )
	{
		safe_free(line);
        S_CLOSE(sock);
		LIBNET_DEBUG("bad server greeting");        
        if (rsrc->running)
        {
            notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)-NET_ERR_FTP_SERVER_ERROR);
        }
		return 0;
	}
	safe_free(line);


	send_control(sock, "USER ", u->username, "\r\n", NULL);

	if ( !(line = get_line(rsrc, sock)) )
	{
        msg_code = -NET_ERR_LOGIN_FAILED;
        goto cleanup;
	}

	/* do the password dance */
	if ( !check_numeric("230", line) )
	{
		if ( !check_numeric("331", line))
		{
			safe_free(line);
			LIBNET_DEBUG("bad/unexpected response: %s", line);
            msg_code = -NET_ERR_LOGIN_FAILED;
            goto cleanup;
		}
		else
		{
			safe_free(line);

			send_control(sock, "PASS ", u->password, "\r\n", NULL);

			if ( !((line = get_line(rsrc, sock)) &&
					check_numeric("230", line)) )
			{
				safe_free(line);
				LIBNET_DEBUG("login failed");
				msg_code = -NET_ERR_LOGIN_FAILED;
                goto cleanup;
			}
			//safe_free(line);
		}
	}

    safe_free(line);

	/* set binmode */
	send_control(sock, "TYPE I\r\n", NULL);

	if ( !(line = get_line(rsrc, sock)) )
	{      
        msg_code = -NET_ERR_FTP_SERVER_ERROR;
        goto cleanup;
	}
	safe_free(line);
#if 1 // user can't change ftp path to "/" if server don't support it
	if ( u->path && (STRCMP(u->path, "/") != 0))
	{
		/* CWD using relative path */
		char *relative_path = u->path[0] == '/' ? &u->path[1] : &u->path[0];
		send_control(sock, "CWD ", relative_path, "\r\n", NULL);

		if ( !((line = get_line(rsrc, sock)) &&
				check_numeric("250", line)) )
		{
			safe_free(line);
            msg_code = -NET_ERR_OPERATION_NOT_PERMIT;
            goto cleanup;
		}
		safe_free(line);
	}
#endif
	/* finally, the good stuff */

	/* get a socket for reading. try passive first. */

	if ( ! (rsrc->options & OPT_ACTIVE) )
	{
		if ( (data_sock = get_passive_sock(rsrc, sock)) < 0 )
		{          
          msg_code = -NET_ERR_CONNECT_FAILED;
          goto cleanup;
		}
	}


	if ( data_sock < 0 )
	{
		if ( (data_sock = get_sock(rsrc, sock)) < 1 )
		{          
          msg_code = -NET_ERR_CONNECT_FAILED;
          goto cleanup;
		}
		else
			passive = 0;
	}

	if (u->file)
	{
		send_control(sock, "SIZE ", u->file, "\r\n", NULL);
		line = get_line(rsrc, sock);
		if (line && check_numeric("213", line))
		{
			rsrc->outfile_size = ATOI(line + 4);
		}
		else if (line && check_numeric("550", line))
		{
			safe_free(line);
			msg_code = -NET_ERR_FILE_NOT_FOUND;
            goto cleanup;
		}
		else
		{
			rsrc->outfile_size = 0;
		}
		if (line) FREE(line);
	}

	/* handle resume */
	if ( rsrc->outfile_offset && (rsrc->options & OPT_RESUME) )
	{
		char numstring[32]; /* ugly hack */

		sprintf(numstring, "%ld", (long int )rsrc->outfile_offset);
		send_control(sock, "REST ", numstring, "\r\n", NULL);

		if ( !((line = get_line(rsrc, sock)) &&
				check_numeric("350", line)) )
		{
			safe_free(line);
			LIBNET_DEBUG("server does not support FTP resume, try again without -r");
			msg_code = -NET_ERR_OPERATION_NOT_PERMIT;
            goto cleanup;
		}
		safe_free(line);
	}

	if (u->file)
		send_control(sock, "RETR ", u->file, "\r\n", NULL);
	else
		send_control(sock, "NLST\r\n", NULL);

	if ( !((line = get_line(rsrc, sock)) &&
			(check_numeric("150", line) || check_numeric("125", line))) )
	{
		safe_free(line);
        msg_code = -NET_ERR_OPERATION_NOT_PERMIT;
        goto cleanup;
	}

    LIBNET_DEBUG("ftp reply(RETR): %s\n", line);
	if ( !passive )
		data_sock = S_ACCEPT(data_sock, NULL, NULL);

	/*        rsrc->outfile_size = guess_file_size(line); */

	safe_free(line);

	if (rsrc->outfile)
	{
		if (get_file_size(rsrc->outfile) > 0)
			out = fopen(rsrc->outfile, "rb+");
		else
			out = fopen(rsrc->outfile, "wb");

		if ( !out )
		{
			//report(ERR, "opening %s: %s", rsrc->outfile,
			//	   strerror(errno));
            msg_code = -NET_ERR_FILE_SAVE_ERROR;
            goto cleanup;
		}
	}

	retval = dump_data(rsrc, data_sock, out, notify);

    if (rsrc->running)
    {
    	line = get_line(rsrc, sock); /* 226 Transfer complete */
        LIBNET_DEBUG("ftp(done): %s\n", line);
    	safe_free(line);
    }

cleanup:
    
	send_control(sock, "QUIT\r\n", NULL);
	line = get_line(rsrc, sock); /* 221 Goodbye */    
    LIBNET_DEBUG("ftp(QUIT): %s\n", line);
	safe_free(line);
    
	if (out)
		fclose(out);

    if (data_sock >= 0)
    {
        S_CLOSE(data_sock);
    }
    
	if (sock >= 0)
    {
        S_CLOSE(sock);
	}
	
#ifndef WIN32
	if (rsrc->outfile)
		fs_sync(rsrc->outfile);
#endif

    if (msg_code < 0 && rsrc->running)
    { 
		notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)msg_code);
    }
	return retval;
}
Ejemplo n.º 29
0
int get_passive_sock(UrlResource *rsrc, int control)
{
	unsigned char *addr;
	struct sockaddr_in sa;
	int sock;
	int x;
	char *line, *orig_line;

	send_control(control, "PASV\r\n", NULL);

	if ( !((line = get_line(rsrc, control)) &&
			check_numeric("227", line)) )
	{
		safe_free(line);
		return -1;
	}

	orig_line = line;

	if ( strlen(line) < 4 )
	{
		safe_free(line);
		return -1;
	}

	if ( (sock = sock_init(&sa, control)) < 0 )
	{
        safe_free(orig_line);
		return -1;
	}

	/* skip the numeric response */
	line += 4;

	/* then find the digits */

	while ( !(isdigit(*line)) )
		line++;

	sa.sin_family = AF_INET;
	addr = (unsigned char *) & sa.sin_addr;

	for (x = 0; x < 4; x++)
	{
		addr[x] = ATOI(line);
		line = strchr(line, ',') + 1;
	}

	addr = (unsigned char *) & sa.sin_port ;
	addr[0] = ATOI(line);
	line = strchr(line, ',') + 1;
	addr[1] = ATOI(line);

	if ( S_CONNECT(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0 )
	{
        S_CLOSE(sock);
		safe_free(orig_line);
		LIBNET_DEBUG("connect");
		return -1;
	}

	safe_free(orig_line);
	return sock;
}
Ejemplo n.º 30
0
int ftp_transfer(UrlResource *rsrc)
{
    Url  *u         = NULL;
    char *line      = NULL;
    int   sock      = 0;
    int   data_sock = 0;
    int   passive   = 1;
    int   retval    = 0;
    
    u = rsrc->url;

    /*
     * first of all, if this is proxied, just pass it off to the
     * http module, since that's how we support proxying.
     */

    rsrc->proxy = get_proxy("FTP_PROXY");

    if (rsrc->proxy && (rsrc->proxy[0] != '\0')) {
        return http_transfer(rsrc);
    }

    ftp_set_defaults(rsrc, u);

    if (!(sock = tcp_connect(rsrc->op, u->host, u->port)))
        return FALSE;

    if (!(line = get_line(rsrc, sock)))
        return FALSE;

    if (!check_numeric("220", line)) {
        ui_error(rsrc->op, "bad ftp server greeting: %s", line);
        nvfree(line);
        return FALSE;
    }
    
    send_control(sock, "USER ", u->username, "\r\n", NULL);

    if (!(line = get_line(rsrc, sock))) return FALSE;
    
    /* do the password dance */
    if (!check_numeric("230", line)) {
        if (!check_numeric("331", line)) {
            ui_error(rsrc->op, "bad/unexpected response: %s", line);
            nvfree(line);
            return FALSE;
        } else {
            nvfree(line);

            send_control(sock, "PASS ", u->password, "\r\n", NULL);
                        
            if (!((line = get_line(rsrc, sock)) &&
                  check_numeric("230", line)) ) {
                nvfree(line);
                ui_error(rsrc->op, "login failed");
                return FALSE;
            }
            nvfree(line);
        }
    }
        
    /* set binmode */
    send_control(sock, "TYPE I\r\n", NULL);

    if (!(line = get_line(rsrc, sock))) return 0;
    nvfree(line);

    if (u->path) {
        send_control(sock, "CWD ", u->path, "\r\n", NULL);
        
        if (!((line = get_line(rsrc, sock)) &&
              check_numeric("250", line))) {
            nvfree(line);
            close_quit(sock);
            return 0;
        }
        nvfree(line);
    }
        
    /* finally, the good stuff */
    
    /* get a socket for reading. try passive first. */
        
    if ((data_sock = get_passive_sock(rsrc, sock)) == -1) {
        return FALSE;
    }
    
    if (!data_sock) {
        if ((data_sock = get_sock(rsrc, sock)) < 1)
            return 0;
        else
            passive = 0;
    }

    if (u->file) {
        send_control(sock, "SIZE ", u->file, "\r\n", NULL);
        line = get_line(rsrc, sock);
        if (line && check_numeric("213", line)) {
            rsrc->outfile_size = atoi(line + 3);
        } else {
            rsrc->outfile_size = 0;
        }
    }
    
    if (u->file)
        send_control(sock, "RETR ", u->file, "\r\n", NULL);
    else
        send_control(sock, "NLST\r\n", NULL);

    if (!((line = get_line(rsrc, sock)) &&
          (check_numeric("150", line) || check_numeric("125", line)))) {
        nvfree(line);
        close_quit(sock);
        return 0;
    }

    if (!passive) 
        data_sock = accept(data_sock, NULL, NULL);

    nvfree(line);

    retval = dump_data(rsrc, data_sock);
    
    line = get_line(rsrc, sock); /* 226 Transfer complete */
    nvfree(line);
    send_control(sock, "QUIT\r\n", NULL);
    line = get_line(rsrc, sock); /* 221 Goodbye */
    nvfree(line);
    
    close(sock);
    close(data_sock);
    return retval;

} /* ftp_transfer() */