+static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
+{
+ struct netio_data *nd = td->io_ops->data;
+ struct netio_options *o = td->eo;
+ struct udp_close_msg msg;
+ struct sockaddr *to;
+ socklen_t len;
+ int ret;
+
+ if (is_ipv6(o)) {
+ len = sizeof(nd->addr6);
+ to = (struct sockaddr *) &nd->addr6;
+ } else {
+ len = sizeof(nd->addr);
+ to = (struct sockaddr *) &nd->addr;
+ }
+
+ ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
+ if (ret < 0) {
+ td_verror(td, errno, "recvfrom udp link open");
+ return ret;
+ }
+
+ if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
+ ntohl(msg.cmd) != FIO_LINK_OPEN) {
+ log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
+ ntohl(msg.cmd));
+ return -1;
+ }
+
+ return 0;
+}
+
+static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
+{
+ struct netio_data *nd = td->io_ops->data;
+ struct netio_options *o = td->eo;
+ struct udp_close_msg msg;
+ struct sockaddr *to;
+ socklen_t len;
+ int ret;
+
+ if (is_ipv6(o)) {
+ len = sizeof(nd->addr6);
+ to = (struct sockaddr *) &nd->addr6;
+ } else {
+ len = sizeof(nd->addr);
+ to = (struct sockaddr *) &nd->addr;
+ }
+
+ msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
+ msg.cmd = htonl(FIO_LINK_OPEN);
+
+ ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
+ if (ret < 0) {
+ td_verror(td, errno, "sendto udp link open");
+ return ret;
+ }
+
+ return 0;
+}
+
+static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
+{
+ int ret;
+ struct netio_options *o = td->eo;
+
+ if (o->listen)
+ ret = fio_netio_accept(td, f);
+ else
+ ret = fio_netio_connect(td, f);
+
+ if (ret) {
+ f->fd = -1;
+ return ret;
+ }
+
+ if (is_udp(o)) {
+ if (td_write(td))
+ ret = fio_netio_udp_send_open(td, f);
+ else {
+ int state;
+
+ state = td->runstate;
+ td_set_runstate(td, TD_SETTING_UP);
+ ret = fio_netio_udp_recv_open(td, f);
+ td_set_runstate(td, state);
+ }
+ }
+
+ if (ret)
+ fio_netio_close_file(td, f);
+
+ return ret;
+}
+
+static int fio_fill_addr(struct thread_data *td, const char *host, int af,
+ void *dst, struct addrinfo **res)
+{
+ struct netio_options *o = td->eo;
+ struct addrinfo hints;
+ int ret;
+
+ if (inet_pton(af, host, dst))
+ return 0;
+
+ memset(&hints, 0, sizeof(hints));
+
+ if (is_tcp(o))
+ hints.ai_socktype = SOCK_STREAM;
+ else
+ hints.ai_socktype = SOCK_DGRAM;
+
+ if (is_ipv6(o))
+ hints.ai_family = AF_INET6;
+ else
+ hints.ai_family = AF_INET;
+
+ ret = getaddrinfo(host, NULL, &hints, res);
+ if (ret) {
+ int e = EINVAL;
+ char str[128];
+
+ if (ret == EAI_SYSTEM)
+ e = errno;
+
+ snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
+ td_verror(td, e, str);
+ return 1;
+ }
+
+ return 0;
+}
+