• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Virtual machine Management Terminal User Interface


Commit MetaInfo

Révisiondd1f8d6aea62bbf5751a0b56270556474147dd8f (tree)
l'heure2022-07-09 17:44:22
AuteurKoine Yuusuke(koinec) <koinec@user...>
CommiterKoine Yuusuke(koinec)

Message de Log

Fix SSH connecting error handling.

Change Summary

Modification

--- a/src/hvctl.c
+++ b/src/hvctl.c
@@ -397,6 +397,8 @@ int
397397 HvConnect *p_hvcon;
398398 SSH_Info t_ssh;
399399 DWord dw_status = HVISOR_STATUS_CONNECTED;
400+ ExecRequestFunc *pfunc_exereq;
401+ ExecCmdFunc pfunc_execmd;
400402
401403 p_hvisor = HVisor_Ref( i_hvid );
402404 if( NULL == p_hvisor ) {
@@ -418,7 +420,7 @@ int
418420
419421 switch( p_hvcon->dw_type ) {
420422 case HVISOR_TYPE_BHYVE_VMBHYVE:
421- p_hvcon->ExecRequest = VmBhyve_ExecRequest;
423+ pfunc_exereq = VmBhyve_ExecRequest;
422424 break;
423425 case HVISOR_TYPE_BHYVE_DIRECTBHYVE:
424426 break;
@@ -434,23 +436,24 @@ int
434436 if( HVISOR_CON_REMOTE_SSH == p_hvisor->b_connection ) {
435437 i_err = SSHCmd_Connect_Phase1( p_hvcon, &t_ssh );
436438
437- p_hvcon->ExecCmd = SSHCmd_ExecCmd;
439+ pfunc_execmd = SSHCmd_ExecCmd;
438440 dw_status = HVISOR_STATUS_CONNECTING;
439441
440442 } else if( HVISOR_CON_LOCALHOST == p_hvisor->b_connection ) {
441443 i_err = LocalCmd_Connect( p_hvcon, &t_ssh );
442444
443- p_hvcon->ExecCmd = LocalCmd_ExecCmd;
445+ pfunc_execmd = LocalCmd_ExecCmd;
444446 dw_status = HVISOR_STATUS_CONNECTED;
445447 }
446448
447449 // Set Connected flag ---
448450 if( 0x00 == i_err ) {
449- p_hvisor = HVisor_Use( i_hvid );
451+ p_hvcon->dw_status |= dw_status;
452+ p_hvcon->ExecRequest = pfunc_exereq;
453+ p_hvcon->ExecCmd = pfunc_execmd;
450454
451- p_hvcon->dw_status |= dw_status;
455+ p_hvisor = HVisor_Use( i_hvid );
452456 p_hvisor->dw_status |= dw_status;
453-
454457 HVisor_Release( p_hvisor );
455458 }
456459
--- a/src/hvisor.h
+++ b/src/hvisor.h
@@ -40,6 +40,7 @@
4040 #define HVCONNECT_ERR_BUFFER 512
4141
4242 typedef int (* ExecRequestFunc)( Request *p_req, Guest *p_guest, void *pv_hvcon );
43+typedef int (* ExecCmdFunc)( void *pv_hvcnt, char *pstr_cmdline );
4344
4445 typedef struct {
4546 int i_id;
@@ -80,7 +81,7 @@ typedef struct {
8081
8182 // Func. Ptr ---
8283 ExecRequestFunc *ExecRequest;
83- int (* ExecCmd)( void *pv_hvcnt, char *pstr_cmdline );
84+ ExecCmdFunc ExecCmd;
8485
8586 } HvConnect;
8687
--- a/src/sshcmd.c
+++ b/src/sshcmd.c
@@ -196,6 +196,7 @@ int
196196 HvConnect *p_hvcon,
197197 SSH_Info *pt_ssh )
198198 {
199+ int i_ret = 0x00;
199200 int i_err;
200201 int i_value;
201202 struct hostent *pt_host;
@@ -209,9 +210,12 @@ int
209210 i_value = 0x01;
210211 i_err = ioctl( p_hvcon->ssh.i_sock, FIONBIO, &i_value );
211212 if( 0 > i_err ) {
212- close( p_hvcon->ssh.i_sock );
213- p_hvcon->ssh.i_sock = -0x01;
214- return -0x01;
213+ i_ret = -0x02;
214+ ErrInfo_Warn( "Can't change non-bloking mode of socket.",
215+ p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
216+ errno, i_ret, 0x00, pt_ssh->str_hostfqdn );
217+
218+ goto goto_SSHCmd_Connect_Phase1_post;
215219 }
216220
217221 p_hvcon->ssh.t_sin.sin_family = AF_INET;
@@ -222,32 +226,41 @@ int
222226 if( 0xffffffff == p_hvcon->ssh.t_sin.sin_addr.s_addr ) {
223227 pt_host =gethostbyname( pt_ssh->str_hostfqdn );
224228 if( NULL == pt_host ) {
229+ i_ret = -0x03;
225230 ErrInfo_Warn( "Can't resolve SSH server hostname.",
226231 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
227- errno, 0x00, 0x00, pt_ssh->str_hostfqdn );
228- return -0x02;
232+ errno, i_ret, 0x00, pt_ssh->str_hostfqdn );
233+
234+ goto goto_SSHCmd_Connect_Phase1_post;
229235 }
230236
231237 if( 4 == pt_host->h_length )
232238 { memcpy( &(p_hvcon->ssh.t_sin.sin_addr), pt_host->h_addr_list[0], pt_host->h_length ); }
233239 else {
240+ i_ret = -0x04;
234241 ErrInfo_Warn( "Invalid IP addr. length",
235242 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
236243 errno, (int)pt_host->h_length, 0x00, pt_ssh->str_hostfqdn );
237- return -0x03;
244+
245+ goto goto_SSHCmd_Connect_Phase1_post;
238246 }
239247 }
240248
241249 i_err = connect( p_hvcon->ssh.i_sock, (struct sockaddr *)&(p_hvcon->ssh.t_sin),
242250 sizeof( struct sockaddr_in));
243251 if(( 0x00 != i_err ) && ( EINPROGRESS != errno )) {
252+ i_ret = -0x05;
244253 ErrInfo_Warn( "Failed connect() for SSH server",
245254 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
246255 errno, i_err, 0x00, pt_ssh->str_hostfqdn );
247- return -0x04;
256+
257+ goto goto_SSHCmd_Connect_Phase1_post;
248258 }
249259
250- return 0x00;
260+goto_SSHCmd_Connect_Phase1_post:
261+ if( 0x00 != i_ret ) { SSHCmd_DisConnect( p_hvcon ); }
262+
263+ return i_ret;
251264 }
252265
253266 /* ===========================================================================*/
@@ -257,15 +270,16 @@ int
257270 HvConnect *p_hvcon,
258271 SSH_Info *pt_ssh )
259272 {
273+ int i_ret = 0x00;
260274 int i_err;
261275 int i_value;
262- socklen_t t_len;
263276 int i_sockerr;
277+ int i_sels;
278+ socklen_t t_len;
264279 fd_set t_rfd;
265280 fd_set t_wfd;
266281 fd_set t_efd;
267282 struct timeval t_tout;
268- int i_sels;
269283
270284 // Check connect(2) status ---
271285 FD_ZERO( &t_rfd );
@@ -282,48 +296,53 @@ int
282296 return 0x01;
283297 }
284298 else if( 0 > i_sels ) {
299+ i_ret = -0x01;
285300 ErrInfo_Warn( "Failed check connecting status for HyperVisor (SSH) server.",
286301 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
287302 errno, i_sels, 0, NULL);
288- return -0x01;
303+ goto goto_SSHCmd_Connect_Phase2_post;
289304 }
290305
291306 t_len = (socklen_t)sizeof( i_sockerr );
292307 i_err = getsockopt( p_hvcon->ssh.i_sock, SOL_SOCKET, SO_ERROR, &i_sockerr, &t_len);
293308 if( 0x00 < i_sockerr ) {
309+ i_ret = -0x02;
294310 ErrInfo_Warn( "Failed connect(2) for HyperVisor (SSH) server.",
295311 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
296- errno, i_sels, 0, NULL);
297- return -0x02;
312+ errno, i_ret, 0, NULL);
313+ goto goto_SSHCmd_Connect_Phase2_post;
298314 }
299315
300316 // Change Blocking mode for connect(2) waiting ---
301317 i_value = 0x00;
302318 i_err = ioctl( p_hvcon->ssh.i_sock, FIONBIO, &i_value );
303319 if( 0 > i_err ) {
304- close( p_hvcon->ssh.i_sock );
305- p_hvcon->ssh.i_sock = -0x01;
306- return -0x01;
320+ i_ret = -0x03;
321+ ErrInfo_Warn( "Can't change non-bloking mode of socket.",
322+ p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
323+ errno, i_ret, 0, NULL);
324+ goto goto_SSHCmd_Connect_Phase2_post;
307325 }
308326
309327 p_hvcon->ssh.pt_session = libssh2_session_init();
310328 if( NULL == p_hvcon->ssh.pt_session ) {
329+ i_ret = -0x04;
311330 ErrInfo_Warn( "Failed libssh2_session_init() for SSH server",
312331 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
313- errno, 0x00, 0x00, pt_ssh->str_hostfqdn );
314- return -0x04;
332+ errno, i_ret, 0x00, pt_ssh->str_hostfqdn );
333+ goto goto_SSHCmd_Connect_Phase2_post;
315334 }
316335
317336 libssh2_session_set_blocking( p_hvcon->ssh.pt_session, 0x00 );
318-
319337 while( LIBSSH2_ERROR_EAGAIN
320338 == (i_err = libssh2_session_handshake(
321339 p_hvcon->ssh.pt_session, p_hvcon->ssh.i_sock)));
322340 if( 0x00 != i_err ) {
341+ i_ret = -0x05;
323342 ErrInfo_Warn( "Failed libssh2_session_handshake() for SSH server",
324343 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
325344 errno, i_err, 0x00, pt_ssh->str_hostfqdn );
326- return -0x05;
345+ goto goto_SSHCmd_Connect_Phase2_post;
327346 }
328347
329348 if( pt_ssh->b_sshtype & HVISOR_SSH_AUTH_PASSWD ) {
@@ -370,20 +389,25 @@ int
370389 pt_ssh->str_hostfqdn )));
371390 }
372391 else {
392+ i_ret = -0x06;
373393 ErrInfo_Warn( "Invalid SSH connection type",
374394 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
375395 0x00, (int)pt_ssh->b_sshtype, 0x00, pt_ssh->str_hostfqdn );
376- return -0x06;
396+ goto goto_SSHCmd_Connect_Phase2_post;
377397 }
378398
379399 if( 0x00 != i_err ) {
400+ i_ret = -0x07;
380401 ErrInfo_Warn( "Failed SSH authentication",
381402 p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE,
382403 0x00, (int)pt_ssh->b_sshtype, 0x00, pt_ssh->str_hostfqdn );
383- return -0x7;
404+ goto goto_SSHCmd_Connect_Phase2_post;
384405 }
385406
386- return 0x00;
407+goto_SSHCmd_Connect_Phase2_post:
408+ if( 0x00 != i_ret ) { SSHCmd_DisConnect( p_hvcon ); }
409+
410+ return i_ret;
387411 }
388412
389413 /* ===========================================================================*/