Revision 537b41f5

b/include/block/nbd.h
62 62
#define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)
63 63

  
64 64
ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read);
65
int tcp_socket_incoming(const char *address, uint16_t port);
66
int unix_socket_outgoing(const char *path);
67
int unix_socket_incoming(const char *path);
68

  
69 65
int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
70 66
                          off_t *size, size_t *blocksize);
71 67
int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize);
b/nbd.c
188 188
    return ret;
189 189
}
190 190

  
191
static void combine_addr(char *buf, size_t len, const char* address,
192
                         uint16_t port)
193
{
194
    /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
195
    if (strstr(address, ":")) {
196
        snprintf(buf, len, "[%s]:%u", address, port);
197
    } else {
198
        snprintf(buf, len, "%s:%u", address, port);
199
    }
200
}
201

  
202
int tcp_socket_incoming(const char *address, uint16_t port)
203
{
204
    char address_and_port[128];
205
    Error *local_err = NULL;
206

  
207
    combine_addr(address_and_port, 128, address, port);
208
    int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
209

  
210
    if (local_err != NULL) {
211
        qerror_report_err(local_err);
212
        error_free(local_err);
213
    }
214
    return fd;
215
}
216

  
217
int unix_socket_incoming(const char *path)
218
{
219
    Error *local_err = NULL;
220
    int fd = unix_listen(path, NULL, 0, &local_err);
221

  
222
    if (local_err != NULL) {
223
        qerror_report_err(local_err);
224
        error_free(local_err);
225
    }
226
    return fd;
227
}
228

  
229
int unix_socket_outgoing(const char *path)
230
{
231
    Error *local_err = NULL;
232
    int fd = unix_connect(path, &local_err);
233

  
234
    if (local_err != NULL) {
235
        qerror_report_err(local_err);
236
        error_free(local_err);
237
    }
238
    return fd;
239
}
240

  
241 191
/* Basic flow for negotiation
242 192

  
243 193
   Server         Client
b/qemu-nbd.c
20 20
#include "block/block.h"
21 21
#include "block/nbd.h"
22 22
#include "qemu/main-loop.h"
23
#include "qemu/sockets.h"
24
#include "qemu/error-report.h"
23 25
#include "block/snapshot.h"
24 26

  
25 27
#include <stdarg.h>
......
201 203
    qemu_notify_event();
202 204
}
203 205

  
206
static void combine_addr(char *buf, size_t len, const char* address,
207
                         uint16_t port)
208
{
209
    /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
210
    if (strstr(address, ":")) {
211
        snprintf(buf, len, "[%s]:%u", address, port);
212
    } else {
213
        snprintf(buf, len, "%s:%u", address, port);
214
    }
215
}
216

  
217
static int tcp_socket_incoming(const char *address, uint16_t port)
218
{
219
    char address_and_port[128];
220
    Error *local_err = NULL;
221

  
222
    combine_addr(address_and_port, 128, address, port);
223
    int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
224

  
225
    if (local_err != NULL) {
226
        qerror_report_err(local_err);
227
        error_free(local_err);
228
    }
229
    return fd;
230
}
231

  
232
static int unix_socket_incoming(const char *path)
233
{
234
    Error *local_err = NULL;
235
    int fd = unix_listen(path, NULL, 0, &local_err);
236

  
237
    if (local_err != NULL) {
238
        qerror_report_err(local_err);
239
        error_free(local_err);
240
    }
241
    return fd;
242
}
243

  
244
static int unix_socket_outgoing(const char *path)
245
{
246
    Error *local_err = NULL;
247
    int fd = unix_connect(path, &local_err);
248

  
249
    if (local_err != NULL) {
250
        qerror_report_err(local_err);
251
        error_free(local_err);
252
    }
253
    return fd;
254
}
255

  
204 256
static void *show_parts(void *arg)
205 257
{
206 258
    char *device = arg;

Also available in: Unified diff