Virtual machine Management Terminal User Interface
Revisión | 5563452411b05d582a74c603c8611cefc6d95c33 (tree) |
---|---|
Tiempo | 2022-07-02 22:36:57 |
Autor | Koine Yuusuke(koinec) <koinec@user...> |
Commiter | Koine Yuusuke(koinec) |
Suppot connect(2) Non-Blocking mode.
@@ -432,7 +432,7 @@ int | ||
432 | 432 | |
433 | 433 | // Connect --- |
434 | 434 | if( HVISOR_CON_REMOTE_SSH == p_hvisor->b_connection ) { |
435 | - i_err = SSHCmd_Connect( p_hvcon, &t_ssh ); | |
435 | + i_err = SSHCmd_Connect_Phase1( p_hvcon, &t_ssh ); | |
436 | 436 | |
437 | 437 | p_hvcon->ExecCmd = SSHCmd_ExecCmd; |
438 | 438 | dw_status = HVISOR_STATUS_CONNECTING; |
@@ -465,29 +465,42 @@ int | ||
465 | 465 | { |
466 | 466 | int i_err = 0x00; |
467 | 467 | HVisor *p_hvisor = NULL; |
468 | + SSH_Info t_ssh; | |
469 | + | |
470 | + p_hvisor = HVisor_Ref( p_hvcon->i_id ); | |
471 | + if( NULL == p_hvisor ) { | |
472 | + return -0x01; | |
473 | + } | |
474 | + | |
475 | + memcpy( &t_ssh, &(p_hvisor->ssh), sizeof( SSH_Info ) ); | |
476 | + | |
477 | + HVisor_Release( p_hvisor ); | |
468 | 478 | |
469 | 479 | // Connect --- |
470 | 480 | if( HVISOR_CON_REMOTE_SSH == p_hvcon->b_connection ) { |
471 | - //i_err = SSHCmd_Connect( p_hvcon, &t_ssh ); | |
481 | + i_err = SSHCmd_Connect_Phase2( p_hvcon, &t_ssh ); | |
482 | + if( 0x01 == i_err ) { | |
483 | + // Not Error -- Continue connect(2) proc. --- | |
484 | + return 0x00; | |
485 | + } | |
472 | 486 | |
473 | 487 | } else if( HVISOR_CON_LOCALHOST == p_hvcon->b_connection ) { |
474 | 488 | // NONE Proc. --- |
475 | 489 | } |
476 | 490 | |
477 | 491 | // Set Connected flag --- |
478 | - if( 0x00 == i_err ) { | |
479 | - p_hvisor = HVisor_Use( p_hvcon->i_id ); | |
480 | - assert( NULL != p_hvisor ); | |
492 | + p_hvisor = HVisor_Use( p_hvcon->i_id ); | |
493 | + assert( NULL != p_hvisor ); | |
481 | 494 | |
495 | + if( 0x00 == i_err ) { | |
482 | 496 | p_hvcon->dw_status |= HVISOR_STATUS_CONNECTED; |
483 | - p_hvcon->dw_status &= ~(HVISOR_STATUS_CONNECTING); | |
484 | - | |
485 | 497 | p_hvisor->dw_status |= HVISOR_STATUS_CONNECTED; |
486 | - p_hvisor->dw_status &= ~(HVISOR_STATUS_CONNECTING); | |
498 | + } | |
487 | 499 | |
488 | - HVisor_Release( p_hvisor ); | |
500 | + p_hvcon->dw_status &= ~(HVISOR_STATUS_CONNECTING); | |
501 | + p_hvisor->dw_status &= ~(HVISOR_STATUS_CONNECTING); | |
489 | 502 | |
490 | - } | |
503 | + HVisor_Release( p_hvisor ); | |
491 | 504 | |
492 | 505 | return i_err; |
493 | 506 | } |
@@ -192,11 +192,12 @@ int | ||
192 | 192 | /* ===========================================================================*/ |
193 | 193 | VMTUI_SSHCMD_EXTERN |
194 | 194 | int |
195 | - SSHCmd_Connect( | |
195 | + SSHCmd_Connect_Phase1( | |
196 | 196 | HvConnect *p_hvcon, |
197 | 197 | SSH_Info *pt_ssh ) |
198 | 198 | { |
199 | 199 | int i_err; |
200 | + int i_value; | |
200 | 201 | struct hostent *pt_host; |
201 | 202 | |
202 | 203 | p_hvcon->ssh.i_sock = socket(AF_INET, SOCK_STREAM, 0x00 ); |
@@ -204,17 +205,27 @@ int | ||
204 | 205 | return -0x01; |
205 | 206 | } |
206 | 207 | |
208 | + // Change Non-Blocking mode for connect(2) waiting --- | |
209 | + i_value = 0x01; | |
210 | + i_err = ioctl( p_hvcon->ssh.i_sock, FIONBIO, &i_value ); | |
211 | + if( 0 > i_err ) { | |
212 | + close( p_hvcon->ssh.i_sock ); | |
213 | + p_hvcon->ssh.i_sock = -0x01; | |
214 | + return -0x01; | |
215 | + } | |
216 | + | |
207 | 217 | p_hvcon->ssh.t_sin.sin_family = AF_INET; |
208 | 218 | p_hvcon->ssh.t_sin.sin_port = htons( pt_ssh->w_port ); |
209 | 219 | p_hvcon->ssh.t_sin.sin_addr.s_addr = inet_addr( pt_ssh->str_hostfqdn ); |
210 | 220 | |
221 | + // Resolv IP addr from HostName ---- | |
211 | 222 | if( 0xffffffff == p_hvcon->ssh.t_sin.sin_addr.s_addr ) { |
212 | 223 | pt_host =gethostbyname( pt_ssh->str_hostfqdn ); |
213 | 224 | if( NULL == pt_host ) { |
214 | 225 | ErrInfo_Warn( "Can't resolve SSH server hostname.", |
215 | 226 | p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE, |
216 | 227 | errno, 0x00, 0x00, pt_ssh->str_hostfqdn ); |
217 | - return -0x01; | |
228 | + return -0x02; | |
218 | 229 | } |
219 | 230 | |
220 | 231 | if( 4 == pt_host->h_length ) |
@@ -223,17 +234,76 @@ int | ||
223 | 234 | ErrInfo_Warn( "Invalid IP addr. length", |
224 | 235 | p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE, |
225 | 236 | errno, (int)pt_host->h_length, 0x00, pt_ssh->str_hostfqdn ); |
226 | - return -0x02; | |
237 | + return -0x03; | |
227 | 238 | } |
228 | 239 | } |
229 | 240 | |
230 | 241 | i_err = connect( p_hvcon->ssh.i_sock, (struct sockaddr *)&(p_hvcon->ssh.t_sin), |
231 | 242 | sizeof( struct sockaddr_in)); |
232 | - if( 0x00 != i_err ) { | |
243 | + if(( 0x00 != i_err ) && ( EINPROGRESS != errno )) { | |
233 | 244 | ErrInfo_Warn( "Failed connect() for SSH server", |
234 | 245 | p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE, |
235 | 246 | errno, i_err, 0x00, pt_ssh->str_hostfqdn ); |
236 | - return -0x03; | |
247 | + return -0x04; | |
248 | + } | |
249 | + | |
250 | + return 0x00; | |
251 | +} | |
252 | + | |
253 | +/* ===========================================================================*/ | |
254 | +VMTUI_SSHCMD_EXTERN | |
255 | +int | |
256 | + SSHCmd_Connect_Phase2( | |
257 | + HvConnect *p_hvcon, | |
258 | + SSH_Info *pt_ssh ) | |
259 | +{ | |
260 | + int i_err; | |
261 | + int i_value; | |
262 | + socklen_t t_len; | |
263 | + int i_sockerr; | |
264 | + fd_set t_rfd; | |
265 | + fd_set t_wfd; | |
266 | + fd_set t_efd; | |
267 | + struct timeval t_tout; | |
268 | + int i_sels; | |
269 | + | |
270 | + // Check connect(2) status --- | |
271 | + FD_ZERO( &t_rfd ); | |
272 | + FD_SET( p_hvcon->ssh.i_sock, &t_rfd ); | |
273 | + FD_ZERO( &t_wfd ); | |
274 | + FD_SET( p_hvcon->ssh.i_sock, &t_wfd ); | |
275 | + FD_ZERO( &t_efd ); | |
276 | + FD_SET( p_hvcon->ssh.i_sock, &t_efd ); | |
277 | + t_tout.tv_sec = 0; | |
278 | + t_tout.tv_usec = 1; | |
279 | + i_sels = select( p_hvcon->ssh.i_sock + 1, &t_rfd, &t_wfd, &t_efd, &t_tout ); | |
280 | + if( 0 == i_sels ) { | |
281 | + // Not Error -- Continue connect(2) proc. --- | |
282 | + return 0x01; | |
283 | + } | |
284 | + else if( 0 > i_sels ) { | |
285 | + ErrInfo_Warn( "Failed check connecting status for HyperVisor (SSH) server.", | |
286 | + p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE, | |
287 | + errno, i_sels, 0, NULL); | |
288 | + return -0x01; | |
289 | + } | |
290 | + | |
291 | + t_len = (socklen_t)sizeof( i_sockerr ); | |
292 | + i_err = getsockopt( p_hvcon->ssh.i_sock, SOL_SOCKET, SO_ERROR, &i_sockerr, &t_len); | |
293 | + if( 0x00 < i_sockerr ) { | |
294 | + ErrInfo_Warn( "Failed connect(2) for HyperVisor (SSH) server.", | |
295 | + p_hvcon->i_id, GUEST_ID_NONE, REQUEST_ID_NONE, | |
296 | + errno, i_sels, 0, NULL); | |
297 | + return -0x02; | |
298 | + } | |
299 | + | |
300 | + // Change Blocking mode for connect(2) waiting --- | |
301 | + i_value = 0x00; | |
302 | + i_err = ioctl( p_hvcon->ssh.i_sock, FIONBIO, &i_value ); | |
303 | + if( 0 > i_err ) { | |
304 | + close( p_hvcon->ssh.i_sock ); | |
305 | + p_hvcon->ssh.i_sock = -0x01; | |
306 | + return -0x01; | |
237 | 307 | } |
238 | 308 | |
239 | 309 | p_hvcon->ssh.pt_session = libssh2_session_init(); |
@@ -37,7 +37,8 @@ | ||
37 | 37 | #endif |
38 | 38 | VMTUI_SSHCMD_EXTERN int SSHCmd_ExecCmd( void *pv_hvcnt, char *pstr_cmdline ); |
39 | 39 | VMTUI_SSHCMD_EXTERN int SSHCmd_DisConnect( HvConnect *p_hvcon ); |
40 | -VMTUI_SSHCMD_EXTERN int SSHCmd_Connect( HvConnect *p_hvcon, SSH_Info *pt_ssh ); | |
40 | +VMTUI_SSHCMD_EXTERN int SSHCmd_Connect_Phase1( HvConnect *p_hvcon, SSH_Info *pt_ssh ); | |
41 | +VMTUI_SSHCMD_EXTERN int SSHCmd_Connect_Phase2( HvConnect *p_hvcon, SSH_Info *pt_ssh ); | |
41 | 42 | VMTUI_SSHCMD_EXTERN int SSHCmd_Init( void ); |
42 | 43 | VMTUI_SSHCMD_EXTERN int SSHCmd_Term( void ); |
43 | 44 |