@@ -96,33 +96,43 @@ void vsock_wait_remote_close(int fd)
96
96
close (epollfd );
97
97
}
98
98
99
- /* Bind to <bind_port >, connect to <cid, port> and return the file descriptor. */
100
- int vsock_bind_connect (unsigned int cid , unsigned int port , unsigned int bind_port , int type )
99
+ /* Create socket <type >, bind to <cid, port> and return the file descriptor. */
100
+ int vsock_bind (unsigned int cid , unsigned int port , int type )
101
101
{
102
- struct sockaddr_vm sa_client = {
103
- .svm_family = AF_VSOCK ,
104
- .svm_cid = VMADDR_CID_ANY ,
105
- .svm_port = bind_port ,
106
- };
107
- struct sockaddr_vm sa_server = {
102
+ struct sockaddr_vm sa = {
108
103
.svm_family = AF_VSOCK ,
109
104
.svm_cid = cid ,
110
105
.svm_port = port ,
111
106
};
107
+ int fd ;
112
108
113
- int client_fd , ret ;
114
-
115
- client_fd = socket (AF_VSOCK , type , 0 );
116
- if (client_fd < 0 ) {
109
+ fd = socket (AF_VSOCK , type , 0 );
110
+ if (fd < 0 ) {
117
111
perror ("socket" );
118
112
exit (EXIT_FAILURE );
119
113
}
120
114
121
- if (bind (client_fd , (struct sockaddr * )& sa_client , sizeof (sa_client ))) {
115
+ if (bind (fd , (struct sockaddr * )& sa , sizeof (sa ))) {
122
116
perror ("bind" );
123
117
exit (EXIT_FAILURE );
124
118
}
125
119
120
+ return fd ;
121
+ }
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 )
125
+ {
126
+ struct sockaddr_vm sa_server = {
127
+ .svm_family = AF_VSOCK ,
128
+ .svm_cid = cid ,
129
+ .svm_port = port ,
130
+ };
131
+
132
+ int client_fd , ret ;
133
+
134
+ client_fd = vsock_bind (VMADDR_CID_ANY , bind_port , type );
135
+
126
136
timeout_begin (TIMEOUT );
127
137
do {
128
138
ret = connect (client_fd , (struct sockaddr * )& sa_server , sizeof (sa_server ));
@@ -192,28 +202,9 @@ int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
192
202
/* Listen on <cid, port> and return the file descriptor. */
193
203
static int vsock_listen (unsigned int cid , unsigned int port , int type )
194
204
{
195
- union {
196
- struct sockaddr sa ;
197
- struct sockaddr_vm svm ;
198
- } addr = {
199
- .svm = {
200
- .svm_family = AF_VSOCK ,
201
- .svm_port = port ,
202
- .svm_cid = cid ,
203
- },
204
- };
205
205
int fd ;
206
206
207
- fd = socket (AF_VSOCK , type , 0 );
208
- if (fd < 0 ) {
209
- perror ("socket" );
210
- exit (EXIT_FAILURE );
211
- }
212
-
213
- if (bind (fd , & addr .sa , sizeof (addr .svm )) < 0 ) {
214
- perror ("bind" );
215
- exit (EXIT_FAILURE );
216
- }
207
+ fd = vsock_bind (cid , port , type );
217
208
218
209
if (listen (fd , 1 ) < 0 ) {
219
210
perror ("listen" );
0 commit comments