summary refs log tree commit diff
path: root/src/rt/rust_uv.c
blob: 3156d2a1fd4f01099e91aebc1811c49c12dcd5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#include <stdlib.h>
#include <assert.h>

#ifdef __WIN32__
// For alloca
#include <malloc.h>
#endif

#ifndef __WIN32__
// for signal
#include <signal.h>
#endif

#include "uv.h"

void*
rust_uv_loop_new() {
// XXX libuv doesn't always ignore SIGPIPE even though we don't need it.
#ifndef __WIN32__
    signal(SIGPIPE, SIG_IGN);
#endif
    return (void*)uv_loop_new();
}

void
rust_uv_loop_set_data(uv_loop_t* loop, void* data) {
    loop->data = data;
}

uv_udp_t*
rust_uv_get_udp_handle_from_send_req(uv_udp_send_t* send_req) {
    return send_req->handle;
}

uv_stream_t*
rust_uv_get_stream_handle_from_connect_req(uv_connect_t* connect) {
    return connect->handle;
}
uv_stream_t*
rust_uv_get_stream_handle_from_write_req(uv_write_t* write_req) {
    return write_req->handle;
}

uv_loop_t*
rust_uv_get_loop_for_uv_handle(uv_handle_t* handle) {
    return handle->loop;
}

void*
rust_uv_get_data_for_uv_loop(uv_loop_t* loop) {
    return loop->data;
}

void
rust_uv_set_data_for_uv_loop(uv_loop_t* loop,
        void* data) {
    loop->data = data;
}

void*
rust_uv_get_data_for_uv_handle(uv_handle_t* handle) {
    return handle->data;
}

void
rust_uv_set_data_for_uv_handle(uv_handle_t* handle, void* data) {
    handle->data = data;
}

void*
rust_uv_get_data_for_req(uv_req_t* req) {
    return req->data;
}

void
rust_uv_set_data_for_req(uv_req_t* req, void* data) {
    req->data = data;
}

int
rust_sockaddr_size() {
    return sizeof(struct sockaddr_storage);
}

struct sockaddr*
rust_malloc_ip4_addr(char *name, int port) {
    struct sockaddr_in *addr = (struct sockaddr_in*) calloc(1, rust_sockaddr_size());
    assert(addr != NULL);
    addr->sin_port = htons(port);
    assert(uv_inet_pton(AF_INET, name, &addr->sin_addr) == 0);
    addr->sin_family = AF_INET;
    return (struct sockaddr*) addr;
}

struct sockaddr*
rust_malloc_ip6_addr(char *name, int port) {
    struct sockaddr_in6 *addr = (struct sockaddr_in6*) calloc(1, rust_sockaddr_size());
    assert(addr != NULL);
    addr->sin6_port = htons(port);
    assert(uv_inet_pton(AF_INET6, name, &addr->sin6_addr) == 0);
    addr->sin6_family = AF_INET6;
    return (struct sockaddr*) addr;
}

unsigned int
rust_ip4_port(struct sockaddr_in* src) {
    return ntohs(src->sin_port);
}
unsigned int
rust_ip6_port(struct sockaddr_in6* src) {
    return ntohs(src->sin6_port);
}

int
rust_is_ipv4_sockaddr(struct sockaddr* addr) {
    return addr->sa_family == AF_INET;
}

int
rust_is_ipv6_sockaddr(struct sockaddr* addr) {
    return addr->sa_family == AF_INET6;
}

uintptr_t
rust_uv_handle_type_max() {
  return UV_HANDLE_TYPE_MAX;
}

uintptr_t
rust_uv_req_type_max() {
  return UV_REQ_TYPE_MAX;
}

int
rust_uv_get_result_from_fs_req(uv_fs_t* req) {
  return req->result;
}
const char*
rust_uv_get_path_from_fs_req(uv_fs_t* req) {
  return req->path;
}
void*
rust_uv_get_ptr_from_fs_req(uv_fs_t* req) {
  return req->ptr;
}
uv_loop_t*
rust_uv_get_loop_from_fs_req(uv_fs_t* req) {
  return req->loop;
}

uv_loop_t*
rust_uv_get_loop_from_getaddrinfo_req(uv_getaddrinfo_t* req) {
  return req->loop;
}

void
rust_uv_populate_uv_stat(uv_fs_t* req_in, uv_stat_t* stat_out) {
  stat_out->st_dev = req_in->statbuf.st_dev;
  stat_out->st_mode = req_in->statbuf.st_mode;
  stat_out->st_nlink = req_in->statbuf.st_nlink;
  stat_out->st_uid = req_in->statbuf.st_uid;
  stat_out->st_gid = req_in->statbuf.st_gid;
  stat_out->st_rdev = req_in->statbuf.st_rdev;
  stat_out->st_ino = req_in->statbuf.st_ino;
  stat_out->st_size = req_in->statbuf.st_size;
  stat_out->st_blksize = req_in->statbuf.st_blksize;
  stat_out->st_blocks = req_in->statbuf.st_blocks;
  stat_out->st_flags = req_in->statbuf.st_flags;
  stat_out->st_gen = req_in->statbuf.st_gen;
  stat_out->st_atim.tv_sec = req_in->statbuf.st_atim.tv_sec;
  stat_out->st_atim.tv_nsec = req_in->statbuf.st_atim.tv_nsec;
  stat_out->st_mtim.tv_sec = req_in->statbuf.st_mtim.tv_sec;
  stat_out->st_mtim.tv_nsec = req_in->statbuf.st_mtim.tv_nsec;
  stat_out->st_ctim.tv_sec = req_in->statbuf.st_ctim.tv_sec;
  stat_out->st_ctim.tv_nsec = req_in->statbuf.st_ctim.tv_nsec;
  stat_out->st_birthtim.tv_sec = req_in->statbuf.st_birthtim.tv_sec;
  stat_out->st_birthtim.tv_nsec = req_in->statbuf.st_birthtim.tv_nsec;
}

void
rust_set_stdio_container_flags(uv_stdio_container_t *c, int flags) {
  c->flags = (uv_stdio_flags) flags;
}

void
rust_set_stdio_container_fd(uv_stdio_container_t *c, int fd) {
  c->data.fd = fd;
}

void
rust_set_stdio_container_stream(uv_stdio_container_t *c, uv_stream_t *stream) {
  c->data.stream = stream;
}

int
rust_uv_process_pid(uv_process_t* p) {
  return p->pid;
}

int
rust_uv_guess_handle(int fd) {
  return uv_guess_handle(fd);
}