@@ -120,27 +120,33 @@ int vsock_bind(unsigned int cid, unsigned int port, int type)
120120 return fd ;
121121}
122122
123- /* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
124- int vsock_bind_connect (unsigned int cid , unsigned int port , unsigned int bind_port , int type )
123+ int vsock_connect_fd (int fd , unsigned int cid , unsigned int port )
125124{
126- struct sockaddr_vm sa_server = {
125+ struct sockaddr_vm sa = {
127126 .svm_family = AF_VSOCK ,
128127 .svm_cid = cid ,
129128 .svm_port = port ,
130129 };
131-
132- int client_fd , ret ;
133-
134- client_fd = vsock_bind (VMADDR_CID_ANY , bind_port , type );
130+ int ret ;
135131
136132 timeout_begin (TIMEOUT );
137133 do {
138- ret = connect (client_fd , (struct sockaddr * )& sa_server , sizeof (sa_server ));
134+ ret = connect (fd , (struct sockaddr * )& sa , sizeof (sa ));
139135 timeout_check ("connect" );
140136 } while (ret < 0 && errno == EINTR );
141137 timeout_end ();
142138
143- if (ret < 0 ) {
139+ return ret ;
140+ }
141+
142+ /* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
143+ int vsock_bind_connect (unsigned int cid , unsigned int port , unsigned int bind_port , int type )
144+ {
145+ int client_fd ;
146+
147+ client_fd = vsock_bind (VMADDR_CID_ANY , bind_port , type );
148+
149+ if (vsock_connect_fd (client_fd , cid , port )) {
144150 perror ("connect" );
145151 exit (EXIT_FAILURE );
146152 }
@@ -151,17 +157,6 @@ int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_po
151157/* Connect to <cid, port> and return the file descriptor. */
152158int vsock_connect (unsigned int cid , unsigned int port , int type )
153159{
154- union {
155- struct sockaddr sa ;
156- struct sockaddr_vm svm ;
157- } addr = {
158- .svm = {
159- .svm_family = AF_VSOCK ,
160- .svm_port = port ,
161- .svm_cid = cid ,
162- },
163- };
164- int ret ;
165160 int fd ;
166161
167162 control_expectln ("LISTENING" );
@@ -172,20 +167,14 @@ int vsock_connect(unsigned int cid, unsigned int port, int type)
172167 exit (EXIT_FAILURE );
173168 }
174169
175- timeout_begin (TIMEOUT );
176- do {
177- ret = connect (fd , & addr .sa , sizeof (addr .svm ));
178- timeout_check ("connect" );
179- } while (ret < 0 && errno == EINTR );
180- timeout_end ();
181-
182- if (ret < 0 ) {
170+ if (vsock_connect_fd (fd , cid , port )) {
183171 int old_errno = errno ;
184172
185173 close (fd );
186174 fd = -1 ;
187175 errno = old_errno ;
188176 }
177+
189178 return fd ;
190179}
191180
0 commit comments