/* Start talking to ftp server */ result = GET_FTP_RESULT(stream); if (result > 299 || result < 200) { php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); goto connect_errexit; }
开始建立连接,要求第一个响应码在200-299之间,相当于一个Hello招呼,也就是流量包里的:
继续看代码:
1 2 3 4 5 6 7 8 9 10
/* send the user name */ if (resource->user != NULL) { ZSTR_LEN(resource->user) = php_raw_url_decode(ZSTR_VAL(resource->user), ZSTR_LEN(resource->user));
/* if a password is required, send it */ if (result >= 300 && result <= 399) { php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, tmp_line, 0);
if (resource->pass != NULL) { ZSTR_LEN(resource->pass) = php_raw_url_decode(ZSTR_VAL(resource->pass), ZSTR_LEN(resource->pass));
php_stream_printf(stream, "PASS %s\r\n", ZSTR_VAL(resource->pass)); } else { /* if the user has configured who they are, send that as the password */ if (FG(from_address)) { php_stream_printf(stream, "PASS %s\r\n", FG(from_address)); } else { php_stream_write_string(stream, "PASS anonymous\r\n"); } }
/* read the response */ result = GET_FTP_RESULT(stream);
if (result > 299 || result < 200) { php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result); } else { php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result); } } if (result > 299 || result < 200) { goto connect_errexit; }
/* set the connection to be binary */ php_stream_write_string(stream, "TYPE I\r\n"); result = GET_FTP_RESULT(stream); if (result > 299 || result < 200) goto errexit; ... /* set up the passive connection */ portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart);