aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/socket_stream.c
blob: cfa6e3abf0e88538063c58f772b41516b639241c (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* $Id: socket_stream.c,v 1.9 2004/03/05 14:45:34 mjw Exp $ */
/*
 * Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** @file
 * An IOStream implementation using sockets.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "allocate.h"
#include "socket_stream.h"

#define MODULE_NAME "sock"
#define DEBUG 0
//#undef DEBUG
#include "debug.h"

static int socket_read(IOStream *s, void *buf, size_t n);
static int socket_write(IOStream *s, const void *buf, size_t n);
static int socket_error(IOStream *s);
static int socket_close(IOStream *s);
static void socket_free(IOStream *s);
static int socket_flush(IOStream *s);

/** Methods used by a socket IOStream. */
static const IOMethods socket_methods = {
    read:  socket_read,
    write: socket_write,
    error: socket_error,
    close: socket_close,
    free:  socket_free,
    flush: socket_flush,
};

/** Get the socket data.
 * 
 * @param io socket stream
 * @return data
 */
static inline SocketData * socket_data(IOStream *io){
    return (SocketData *)io->data;
}

/** Test if a stream is a socket stream.
 *
 * @param io stream
 * @return 0 if a socket stream, -EINVAL if not
 */
int socket_stream_check(IOStream *io){
    return (io && io->methods == &socket_methods ? 0 : -EINVAL);
}

/** Get the data for a socket stream.
 *
 * @param io stream
 * @param data return value for the data
 * @return 0 if a socket stream, -EINVAL if not
 */
int socket_stream_data(IOStream *io, SocketData **data){
    int err = socket_stream_check(io);
    if(err){
        *data = NULL;
    } else {
        *data = socket_data(io);
    }
    return err;
}

/** Set the destination address for a socket stream.
 *
 * @param io stream
 * @param addr address
 * @return 0 if a socket stream, -EINVAL if not
 */
int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr){
    int err = 0;
    SocketData *data = NULL;
    err = socket_stream_data(io, &data);
    if(!err){
        data->daddr = *addr;
    }
    return err;
}

/** Set the send flags for a socket stream.
 *
 * @param io stream
 * @param flags flags
 * @return 0 if a socket stream, -EINVAL if not
 */
int socket_stream_set_flags(IOStream *io, int flags){
    int err = 0;
    SocketData *data = NULL;
    err = socket_stream_data(io, &data);
    if(!err){
        data->flags = flags;
    }
    return err;
}

/** Write to the underlying socket using sendto.
 *
 * @param stream input
 * @param buf where to put input
 * @param n number of bytes to write
 * @return number of bytes written
 */
static int socket_write(IOStream *s, const void *buf, size_t n){
    SocketData *data = socket_data(s);
    struct sockaddr *daddr = (struct sockaddr *)&data->daddr;
    socklen_t daddr_n = sizeof(data->daddr);
    int k;
    dprintf("> sock=%d addr=%s:%d n=%d\n",
            data->fd, inet_ntoa(data->daddr.sin_addr), ntohs(data->daddr.sin_port), n);
    if(0){
        struct sockaddr_in self = {};
        socklen_t self_n;
        getsockname(data->fd, (struct sockaddr *)&self, &self_n);
        dprintf("> sockname sock=%d %s:%d\n",
                data->fd, inet_ntoa(self.sin_addr), ntohs(self.sin_port));
    }
    k = sendto(data->fd, buf, n, data->flags, daddr, daddr_n);
    dprintf("> sendto=%d\n", k);
    return k;
}

/** Read from the underlying stream using recv();
 *
 * @param stream input
 * @param buf where to put input
 * @param n number of bytes to read
 * @return number of bytes read
 */
static int socket_read(IOStream *s, void *buf, size_t n){
    SocketData *data = socket_data(s);
    int k;
    struct sockaddr *saddr = (struct sockaddr *)&data->saddr;
    socklen_t saddr_n = sizeof(data->saddr);
    k = recvfrom(data->fd, buf, n, data->flags, saddr, &saddr_n);
    return k;
}

/** Print to the underlying socket.
 *
 * @param s socket stream
 * @param msg format to use
 * @param args arguments
 * @return result of the print
 */
static int socket_print(IOStream *s, const char *msg, va_list args){
    SocketData *data = socket_data(s);
    int n;
    n = vsnprintf(data->buf, data->buf_n - 1, msg, args);
    if(0 < n && n < data->buf_n){
        socket_write(s, data->buf, n);
    }
    return n;
}

/** Read a character from the underlying socket
 *
 * @param s socket stream
 * @return character read, IOSTREAM_EOF on end of socket (or error)
 */
static int socket_getc(IOStream *s){
    char b;
    int n, c;
    n = socket_read(s, &b, 1);
    c = (n <= 0 ? IOSTREAM_EOF : b);
    return c;
}

/** Flush the socket (no-op).
 *
 * @param s socket stream
 * @return 0 on success, error code otherwise
 */
static int socket_flush(IOStream *s){
    return 0;
}

/** Check if a socket stream has an error (no-op).
 *
 * @param s socket stream
 * @return 1 if has an error, 0 otherwise
 */
static int socket_error(IOStream *s){
    // Read SOL_SOCKET/SO_ERROR ?
    return 0;
}

/** Close a socket stream.
 *
 * @param s socket stream to close
 * @return result of the close
 */
static int socket_close(IOStream *s){
    SocketData *data = socket_data(s);
    return close(data->fd);
}

/** Free a socket stream.
 *
 * @param s socket stream
 */
static void socket_free(IOStream *s){
    SocketData *data = socket_data(s);
    deallocate(data);
}

/** Create an IOStream for a socket.
 *
 * @param fd socket to wtap
 * @return new IOStream using fd for i/o
 */
IOStream *socket_stream_new(int fd){
    int err = -ENOMEM;
    IOStream *io = NULL;
    SocketData *data = NULL;

    io = ALLOCATE(IOStream);
    if(!io) goto exit;
    io->methods = &socket_methods;
    data = ALLOCATE(SocketData);
    if(!data) goto exit;
    io->data = data;
    data->fd = fd;
    data->buf_n = sizeof(data->buf);
    err = 0;
  exit:
    if(err){
        if(io){
            if(data) deallocate(data);
            deallocate(io);
            io = NULL;
        }
    }
    return io;
}