Overview
Comment: | Add tcpsocket.
TODO: |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
17c1d8f6b0d8986206645647a9d77c5b |
User & Date: | js 2012-09-29 22:34:49 |
Context
2012-09-29
| ||
22:41 | Add cfw_stream_write_string(). check-in: 2121a00059 user: js tags: trunk | |
22:34 | Add tcpsocket. check-in: 17c1d8f6b0 user: js tags: trunk | |
22:12 | Update corefw.h. check-in: cbf96fa23f user: js tags: trunk | |
Changes
Changes to src/Makefile.
︙ | ︙ | |||
10 11 12 13 14 15 16 | file.c \ int.c \ map.c \ object.c \ range.c \ refpool.c \ stream.c \ | | > | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | file.c \ int.c \ map.c \ object.c \ range.c \ refpool.c \ stream.c \ string.c \ tcpsocket.c INCLUDES = ${SRCS:.c=.h} \ corefw.h \ hash.h include ../buildsys.mk |
Changes to src/corefw.h.
︙ | ︙ | |||
11 12 13 14 15 16 17 18 19 | #include "hash.h" #include "int.h" #include "map.h" #include "range.h" #include "refpool.h" #include "stream.h" #include "string.h" #endif | > | 11 12 13 14 15 16 17 18 19 20 | #include "hash.h" #include "int.h" #include "map.h" #include "range.h" #include "refpool.h" #include "stream.h" #include "string.h" #include "tcpsocket.h" #endif |
Changes to src/file.c.
︙ | ︙ | |||
20 21 22 23 24 25 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ | < | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <string.h> #include <fcntl.h> #include <unistd.h> #include "stream.h" #include "file.h" |
︙ | ︙ | |||
125 126 127 128 129 130 131 | file_close(void *ptr) { CFWFile *file = ptr; close(file->fd); } | | | 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | file_close(void *ptr) { CFWFile *file = ptr; close(file->fd); } static struct cfw_stream_ops stream_ops = { .read = file_read, .write = file_write, .eof = file_eof, .close = file_close }; static bool |
︙ | ︙ |
Changes to src/stream.c.
︙ | ︙ | |||
42 43 44 45 46 47 48 49 50 51 52 | cfw_stream_close(ptr); } ssize_t cfw_stream_read(void *ptr, void *buf, size_t len) { CFWStream *stream = ptr; if (stream == NULL || stream->ops == NULL) return -1; | > | > > > | 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | cfw_stream_close(ptr); } ssize_t cfw_stream_read(void *ptr, void *buf, size_t len) { CFWStream *stream = ptr; ssize_t ret; if (stream == NULL || stream->ops == NULL) return -1; if ((ret = stream->ops->read(stream, buf, len)) < -1) ret = -1; return ret; } bool cfw_stream_write(void *ptr, const void *buf, size_t len) { CFWStream *stream = ptr; |
︙ | ︙ |
Added src/tcpsocket.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | /* * Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <string.h> #include <inttypes.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include "stream.h" #include "tcpsocket.h" struct CFWTCPSocket { CFWStream stream; int fd; bool eof; }; static ssize_t sock_read(void *ptr, void *buf, size_t len) { CFWTCPSocket *sock = ptr; ssize_t ret; if ((ret = recv(sock->fd, buf, len, 0)) == 0) sock->eof = true; return ret; } static bool sock_write(void *ptr, const void *buf, size_t len) { CFWTCPSocket *sock = ptr; ssize_t ret; if ((ret = send(sock->fd, buf, len, 0)) < len) return false; return true; } static bool sock_eof(void *ptr) { CFWTCPSocket *sock = ptr; return sock->eof; } static void sock_close(void *ptr) { CFWTCPSocket *sock = ptr; if (sock->fd != -1) close(sock->fd); } static struct cfw_stream_ops stream_ops = { .read = sock_read, .write = sock_write, .eof = sock_eof, .close = sock_close }; static bool ctor(void *ptr, va_list args) { CFWTCPSocket *sock = ptr; sock->fd = -1; sock->stream.ops = &stream_ops; sock->eof = false; return true; } static void dtor(void *ptr) { cfw_stream_close(ptr); } bool cfw_tcpsocket_connect(CFWTCPSocket *sock, const char *host, uint16_t port) { struct addrinfo hints, *res, *res0; char portstr[7]; if (sock->fd != -1) return false; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf(portstr, 7, "%" PRIu16, port); if (getaddrinfo(host, portstr, &hints, &res0)) return false; for (res = res0; res != NULL; res = res->ai_next) { if ((sock->fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) continue; if (connect(sock->fd, res->ai_addr, res->ai_addrlen) == -1) { close(sock->fd); sock->fd = -1; continue; } break; } freeaddrinfo(res0); return (sock->fd != -1); } static CFWClass class = { .name = "CFWTCPSocket", .size = sizeof(CFWTCPSocket), .ctor = ctor, .dtor = dtor }; CFWClass *cfw_tcpsocket = &class; |
Added src/tcpsocket.h.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | /* * Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef __COREFW_TCPSOCKET_H__ #define __COREFW_TCPSOCKET_H__ typedef struct CFWTCPSocket CFWTCPSocket; extern CFWClass *cfw_tcpsocket; extern bool cfw_tcpsocket_connect(CFWTCPSocket*, const char*, uint16_t); #endif |