1 /* $OpenBSD: mio_aucat.c,v 1.4 2011/04/18 23:57:35 ratchov Exp $ */ 2 /* 3 * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <sys/un.h> 21 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <poll.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "aucat.h" 31 #include "debug.h" 32 #include "mio_priv.h" 33 34 struct mio_aucat_hdl { 35 struct mio_hdl mio; 36 struct aucat aucat; 37 int events; 38 }; 39 40 static void mio_aucat_close(struct mio_hdl *); 41 static size_t mio_aucat_read(struct mio_hdl *, void *, size_t); 42 static size_t mio_aucat_write(struct mio_hdl *, const void *, size_t); 43 static int mio_aucat_pollfd(struct mio_hdl *, struct pollfd *, int); 44 static int mio_aucat_revents(struct mio_hdl *, struct pollfd *); 45 46 static struct mio_ops mio_aucat_ops = { 47 mio_aucat_close, 48 mio_aucat_write, 49 mio_aucat_read, 50 mio_aucat_pollfd, 51 mio_aucat_revents, 52 }; 53 54 static struct mio_hdl * 55 mio_xxx_open(const char *str, unsigned mode, int nbio, int isaudio) 56 { 57 struct mio_aucat_hdl *hdl; 58 59 hdl = malloc(sizeof(struct mio_aucat_hdl)); 60 if (hdl == NULL) 61 return NULL; 62 if (!aucat_open(&hdl->aucat, str, mode, isaudio)) 63 goto bad; 64 mio_create(&hdl->mio, &mio_aucat_ops, mode, nbio); 65 if (!aucat_setfl(&hdl->aucat, nbio, &hdl->mio.eof)) 66 goto bad; 67 return (struct mio_hdl *)hdl; 68 bad: 69 free(hdl); 70 return NULL; 71 } 72 73 struct mio_hdl * 74 mio_midithru_open(const char *str, unsigned mode, int nbio) 75 { 76 return mio_xxx_open(str, mode, nbio, 0); 77 } 78 79 struct mio_hdl * 80 mio_aucat_open(const char *str, unsigned mode, int nbio) 81 { 82 return mio_xxx_open(str, mode, nbio, 1); 83 } 84 85 static void 86 mio_aucat_close(struct mio_hdl *sh) 87 { 88 struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh; 89 90 if (!hdl->mio.eof) 91 aucat_setfl(&hdl->aucat, 0, &hdl->mio.eof); 92 aucat_close(&hdl->aucat, hdl->mio.eof); 93 free(hdl); 94 } 95 96 static size_t 97 mio_aucat_read(struct mio_hdl *sh, void *buf, size_t len) 98 { 99 struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh; 100 101 while (hdl->aucat.rstate == RSTATE_MSG) { 102 if (!aucat_rmsg(&hdl->aucat, &hdl->mio.eof)) 103 return 0; 104 } 105 return aucat_rdata(&hdl->aucat, buf, len, &hdl->mio.eof); 106 } 107 108 static size_t 109 mio_aucat_write(struct mio_hdl *sh, const void *buf, size_t len) 110 { 111 struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh; 112 113 return aucat_wdata(&hdl->aucat, buf, len, 1, &hdl->mio.eof); 114 } 115 116 static int 117 mio_aucat_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events) 118 { 119 struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh; 120 121 hdl->events = events; 122 return aucat_pollfd(&hdl->aucat, pfd, events); 123 } 124 125 static int 126 mio_aucat_revents(struct mio_hdl *sh, struct pollfd *pfd) 127 { 128 struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh; 129 int revents = pfd->revents; 130 131 if (revents & POLLIN) { 132 while (hdl->aucat.rstate == RSTATE_MSG) { 133 if (!aucat_rmsg(&hdl->aucat, &hdl->mio.eof)) 134 break; 135 } 136 if (hdl->aucat.rstate != RSTATE_DATA) 137 revents &= ~POLLIN; 138 } 139 if (hdl->mio.eof) 140 return POLLHUP; 141 return revents & (hdl->events | POLLHUP); 142 } 143