summaryrefslogtreecommitdiffstats
path: root/src/socket-client.c
blob: 9a8c56078673843816f6128935664e8a6bb420fe (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
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "socket-client.h"
#include "util.h"

struct pa_socket_client {
    struct pa_mainloop_api *mainloop;
    int fd;

    void *io_source, *fixed_source;
    void (*callback)(struct pa_socket_client*c, struct pa_iochannel *io, void *userdata);
    void *userdata;
};

static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) {
    struct pa_socket_client *c;
    assert(m);

    c = malloc(sizeof(struct pa_socket_client));
    assert(c);
    c->mainloop = m;
    c->fd = -1;
    c->io_source = c->fixed_source = NULL;
    c->callback = NULL;
    c->userdata = NULL;
    return c;
}

static void do_call(struct pa_socket_client *c) {
    struct pa_iochannel *io;
    int error, lerror;
    assert(c && c->callback);

    lerror = sizeof(error);
    if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
        fprintf(stderr, "getsockopt(): %s\n", strerror(errno));
        goto failed;
    }

    if (lerror != sizeof(error)) {
        fprintf(stderr, "getsocktop() returned invalid size.\n");
        goto failed;
    }

    if (error != 0) {
        fprintf(stderr, "connect(): %s\n", strerror(error));
        goto failed;
    }
        
    io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
    assert(io);
    c->fd = -1;
    c->callback(c, io, c->userdata);

    return;
    
failed:
    close(c->fd);
    c->fd = -1;
    c->callback(c, NULL, c->userdata);
    return;
}

static void connect_fixed_cb(struct pa_mainloop_api *m, void *id, void *userdata) {
    struct pa_socket_client *c = userdata;
    assert(m && c && c->fixed_source == id);
    m->cancel_fixed(m, c->fixed_source);
    c->fixed_source = NULL;
    do_call(c);
}

static void connect_io_cb(struct pa_mainloop_api*m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
    struct pa_socket_client *c = userdata;
    assert(m && c && c->io_source == id && fd >= 0 && events == PA_MAINLOOP_API_IO_EVENT_OUTPUT);
    m->cancel_io(m, c->io_source);
    c->io_source = NULL;
    do_call(c);
}

static int do_connect(struct pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
    int r;
    assert(c && sa && len);
    
    pa_make_nonblock_fd(c->fd);
    
    if ((r = connect(c->fd, sa, len)) < 0) {
        if (r != EINPROGRESS) {
            fprintf(stderr, "connect(): %s\n", strerror(errno));
            return -1;
        }

        c->io_source = c->mainloop->source_io(c->mainloop, c->fd, PA_MAINLOOP_API_IO_EVENT_OUTPUT, connect_io_cb, c);
        assert(c->io_source);
    } else {
        c->fixed_source = c->mainloop->source_fixed(c->mainloop, connect_fixed_cb, c);
        assert(c->fixed_source);
    }

    return 0;
}

struct pa_socket_client* pa_socket_client_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
    struct pa_socket_client *c;
    struct sockaddr_in sa;
    assert(m && address && port);

    c = pa_socket_client_new(m);
    assert(c);

    if ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        fprintf(stderr, "socket(): %s\n", strerror(errno));
        goto fail;
    }

    pa_socket_tcp_low_delay(c->fd);

    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    sa.sin_addr.s_addr = htonl(address);

    if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
        goto fail;
    
    return c;

fail:
    pa_socket_client_free(c);
    return NULL;
}

struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
    struct pa_socket_client *c;
    struct sockaddr_un sa;
    assert(m && filename);
    
    c = pa_socket_client_new(m);
    assert(c);

    if ((c->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
        fprintf(stderr, "socket(): %s\n", strerror(errno));
        goto fail;
    }

    pa_socket_low_delay(c->fd);

    sa.sun_family = AF_LOCAL;
    strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
    sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
    
    if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
        goto fail;
    
    return c;

fail:
    pa_socket_client_free(c);
    return NULL;
}
    
void pa_socket_client_free(struct pa_socket_client *c) {
    assert(c && c->mainloop);
    if (c->io_source)
        c->mainloop->cancel_io(c->mainloop, c->io_source);
    if (c->fixed_source)
        c->mainloop->cancel_fixed(c->mainloop, c->fixed_source);
    if (c->fd >= 0)
        close(c->fd);
    free(c);
}

void pa_socket_client_set_callback(struct pa_socket_client *c, void (*on_connection)(struct pa_socket_client *c, struct pa_iochannel*io, void *userdata), void *userdata) {
    assert(c);
    c->callback = on_connection;
    c->userdata = userdata;
}