@@ -120,27 +120,33 @@ int vsock_bind(unsigned int cid, unsigned int port, int type)
120
120
return fd ;
121
121
}
122
122
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 )
125
124
{
126
- struct sockaddr_vm sa_server = {
125
+ struct sockaddr_vm sa = {
127
126
.svm_family = AF_VSOCK ,
128
127
.svm_cid = cid ,
129
128
.svm_port = port ,
130
129
};
131
-
132
- int client_fd , ret ;
133
-
134
- client_fd = vsock_bind (VMADDR_CID_ANY , bind_port , type );
130
+ int ret ;
135
131
136
132
timeout_begin (TIMEOUT );
137
133
do {
138
- ret = connect (client_fd , (struct sockaddr * )& sa_server , sizeof (sa_server ));
134
+ ret = connect (fd , (struct sockaddr * )& sa , sizeof (sa ));
139
135
timeout_check ("connect" );
140
136
} while (ret < 0 && errno == EINTR );
141
137
timeout_end ();
142
138
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 )) {
144
150
perror ("connect" );
145
151
exit (EXIT_FAILURE );
146
152
}
@@ -151,17 +157,6 @@ int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_po
151
157
/* Connect to <cid, port> and return the file descriptor. */
152
158
int vsock_connect (unsigned int cid , unsigned int port , int type )
153
159
{
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 ;
165
160
int fd ;
166
161
167
162
control_expectln ("LISTENING" );
@@ -172,20 +167,14 @@ int vsock_connect(unsigned int cid, unsigned int port, int type)
172
167
exit (EXIT_FAILURE );
173
168
}
174
169
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 )) {
183
171
int old_errno = errno ;
184
172
185
173
close (fd );
186
174
fd = -1 ;
187
175
errno = old_errno ;
188
176
}
177
+
189
178
return fd ;
190
179
}
191
180
0 commit comments