socket_api.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009, 2011-2015  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 #include <config.h>
00020 
00021 #include <isc/app.h>
00022 #include <isc/magic.h>
00023 #include <isc/mutex.h>
00024 #include <isc/once.h>
00025 #include <isc/socket.h>
00026 #include <isc/util.h>
00027 
00028 static isc_mutex_t createlock;
00029 static isc_once_t once = ISC_ONCE_INIT;
00030 static isc_socketmgrcreatefunc_t socketmgr_createfunc = NULL;
00031 
00032 static void
00033 initialize(void) {
00034         RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
00035 }
00036 
00037 isc_result_t
00038 isc_socket_register(isc_socketmgrcreatefunc_t createfunc) {
00039         isc_result_t result = ISC_R_SUCCESS;
00040 
00041         RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
00042 
00043         LOCK(&createlock);
00044         if (socketmgr_createfunc == NULL)
00045                 socketmgr_createfunc = createfunc;
00046         else
00047                 result = ISC_R_EXISTS;
00048         UNLOCK(&createlock);
00049 
00050         return (result);
00051 }
00052 
00053 isc_result_t
00054 isc_socketmgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
00055                           isc_socketmgr_t **managerp)
00056 {
00057         isc_result_t result;
00058 
00059         LOCK(&createlock);
00060 
00061         REQUIRE(socketmgr_createfunc != NULL);
00062         result = (*socketmgr_createfunc)(mctx, managerp);
00063 
00064         UNLOCK(&createlock);
00065 
00066         if (result == ISC_R_SUCCESS)
00067                 isc_appctx_setsocketmgr(actx, *managerp);
00068 
00069         return (result);
00070 }
00071 
00072 isc_result_t
00073 isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
00074         isc_result_t result;
00075 
00076         if (isc_bind9)
00077                 return (isc__socketmgr_create(mctx, managerp));
00078 
00079         LOCK(&createlock);
00080 
00081         REQUIRE(socketmgr_createfunc != NULL);
00082         result = (*socketmgr_createfunc)(mctx, managerp);
00083 
00084         UNLOCK(&createlock);
00085 
00086         return (result);
00087 }
00088 
00089 void
00090 isc_socketmgr_destroy(isc_socketmgr_t **managerp) {
00091         REQUIRE(managerp != NULL && ISCAPI_SOCKETMGR_VALID(*managerp));
00092 
00093         if (isc_bind9)
00094                 isc__socketmgr_destroy(managerp);
00095         else
00096                 (*managerp)->methods->destroy(managerp);
00097 
00098         ENSURE(*managerp == NULL);
00099 }
00100 
00101 isc_result_t
00102 isc_socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
00103                   isc_socket_t **socketp)
00104 {
00105         REQUIRE(ISCAPI_SOCKETMGR_VALID(manager));
00106 
00107         if (isc_bind9)
00108                 return (isc__socket_create(manager, pf, type, socketp));
00109 
00110         return (manager->methods->socketcreate(manager, pf, type, socketp));
00111 }
00112 
00113 void
00114 isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
00115         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00116         REQUIRE(socketp != NULL && *socketp == NULL);
00117 
00118         if (isc_bind9)
00119                 isc__socket_attach(sock, socketp);
00120         else
00121                 sock->methods->attach(sock, socketp);
00122 
00123         ENSURE(*socketp == sock);
00124 }
00125 
00126 void
00127 isc_socket_detach(isc_socket_t **socketp) {
00128         REQUIRE(socketp != NULL && ISCAPI_SOCKET_VALID(*socketp));
00129 
00130         if (isc_bind9)
00131                 isc__socket_detach(socketp);
00132         else
00133                 (*socketp)->methods->detach(socketp);
00134 
00135         ENSURE(*socketp == NULL);
00136 }
00137 
00138 isc_result_t
00139 isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
00140                 unsigned int options)
00141 {
00142         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00143 
00144         if (isc_bind9)
00145                 return (isc__socket_bind(sock, sockaddr, options));
00146 
00147         return (sock->methods->bind(sock, sockaddr, options));
00148 }
00149 
00150 isc_result_t
00151 isc_socket_sendto(isc_socket_t *sock, isc_region_t *region, isc_task_t *task,
00152                   isc_taskaction_t action, void *arg,
00153                   isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
00154 {
00155         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00156 
00157         if (isc_bind9)
00158                 return (isc__socket_sendto(sock, region, task,
00159                                            action, arg, address, pktinfo));
00160 
00161         return (sock->methods->sendto(sock, region, task, action, arg, address,
00162                                       pktinfo));
00163 }
00164 
00165 isc_result_t
00166 isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr, isc_task_t *task,
00167                    isc_taskaction_t action, void *arg)
00168 {
00169         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00170 
00171         if (isc_bind9)
00172                 return (isc__socket_connect(sock, addr, task, action, arg));
00173 
00174         return (sock->methods->connect(sock, addr, task, action, arg));
00175 }
00176 
00177 isc_result_t
00178 isc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
00179                 isc_task_t *task, isc_taskaction_t action, void *arg)
00180 {
00181         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00182 
00183         if (isc_bind9)
00184                 return (isc__socket_recv(sock, region, minimum,
00185                                          task, action, arg));
00186 
00187         return (sock->methods->recv(sock, region, minimum, task, action, arg));
00188 }
00189 
00190 void
00191 isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
00192         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00193 
00194         if (isc_bind9)
00195                 isc__socket_cancel(sock, task, how);
00196         else
00197                 sock->methods->cancel(sock, task, how);
00198 }
00199 
00200 isc_result_t
00201 isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
00202         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00203 
00204         if (isc_bind9)
00205                 return (isc__socket_getsockname(sock, addressp));
00206 
00207         return (sock->methods->getsockname(sock, addressp));
00208 }
00209 
00210 void
00211 isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
00212         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00213 
00214         if (isc_bind9)
00215                 isc__socket_ipv6only(sock, yes);
00216         else
00217                 sock->methods->ipv6only(sock, yes);
00218 }
00219 
00220 void
00221 isc_socket_dscp(isc_socket_t *sock, isc_dscp_t dscp) {
00222         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00223 
00224         sock->methods->dscp(sock, dscp);
00225 }
00226 
00227 isc_sockettype_t
00228 isc_socket_gettype(isc_socket_t *sock) {
00229         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00230 
00231         if (isc_bind9)
00232                 return (isc__socket_gettype(sock));
00233 
00234         return (sock->methods->gettype(sock));
00235 }
00236 
00237 void
00238 isc_socket_setname(isc_socket_t *sock, const char *name, void *tag) {
00239         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00240 
00241         UNUSED(sock);           /* in case REQUIRE() is empty */
00242         UNUSED(name);
00243         UNUSED(tag);
00244 }
00245 
00246 isc_result_t
00247 isc_socket_fdwatchcreate(isc_socketmgr_t *manager, int fd, int flags,
00248                          isc_sockfdwatch_t callback, void *cbarg,
00249                          isc_task_t *task, isc_socket_t **socketp)
00250 {
00251         REQUIRE(ISCAPI_SOCKETMGR_VALID(manager));
00252 
00253         if (isc_bind9)
00254                 return (isc__socket_fdwatchcreate(manager, fd, flags,
00255                                                   callback, cbarg,
00256                                                   task, socketp));
00257 
00258         return (manager->methods->fdwatchcreate(manager, fd, flags,
00259                                                 callback, cbarg, task,
00260                                                 socketp));
00261 }
00262 
00263 isc_result_t
00264 isc_socket_fdwatchpoke(isc_socket_t *sock, int flags)
00265 {
00266         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00267 
00268         if (isc_bind9)
00269                 return (isc__socket_fdwatchpoke(sock, flags));
00270 
00271         return (sock->methods->fdwatchpoke(sock, flags));
00272 }
00273 
00274 isc_result_t
00275 isc_socket_dup(isc_socket_t *sock, isc_socket_t **socketp) {
00276         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00277         REQUIRE(socketp != NULL && *socketp == NULL);
00278 
00279         if (isc_bind9)
00280                 return (isc__socket_dup(sock, socketp));
00281 
00282         return (sock->methods->dup(sock, socketp));
00283 }
00284 
00285 int
00286 isc_socket_getfd(isc_socket_t *sock) {
00287         REQUIRE(ISCAPI_SOCKET_VALID(sock));
00288 
00289         if (isc_bind9)
00290                 return (isc__socket_getfd(sock));
00291 
00292         return (sock->methods->getfd(sock));
00293 }
00294 
00295 isc_result_t
00296 isc_socket_open(isc_socket_t *sock) {
00297         return (isc__socket_open(sock));
00298 }
00299 
00300 isc_result_t
00301 isc_socket_close(isc_socket_t *sock) {
00302         return (isc__socket_close(sock));
00303 }
00304 
00305 isc_result_t
00306 isc_socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
00307                        unsigned int maxsocks)
00308 {
00309         return (isc__socketmgr_create2(mctx, managerp, maxsocks));
00310 }
00311 
00312 isc_result_t
00313 isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
00314                  unsigned int minimum, isc_task_t *task,
00315                  isc_taskaction_t action, void *arg)
00316 {
00317         return (isc__socket_recvv(sock, buflist, minimum, task, action, arg));
00318 }
00319 
00320 isc_result_t
00321 isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
00322                   unsigned int minimum, isc_task_t *task,
00323                   isc_socketevent_t *event, unsigned int flags)
00324 {
00325         return (isc__socket_recv2(sock, region, minimum, task, event, flags));
00326 }
00327 
00328 isc_result_t
00329 isc_socket_send(isc_socket_t *sock, isc_region_t *region,
00330                  isc_task_t *task, isc_taskaction_t action, void *arg)
00331 {
00332         return (isc__socket_send(sock, region, task, action, arg));
00333 }
00334 
00335 isc_result_t
00336 isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
00337                   isc_task_t *task, isc_taskaction_t action, void *arg)
00338 {
00339         return (isc__socket_sendv(sock, buflist, task, action, arg));
00340 }
00341 
00342 isc_result_t
00343 isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
00344                     isc_task_t *task, isc_taskaction_t action, void *arg,
00345                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
00346 {
00347         return (isc__socket_sendtov(sock, buflist, task, action, arg,
00348                                     address, pktinfo));
00349 }
00350 
00351 isc_result_t
00352 isc_socket_sendtov2(isc_socket_t *sock, isc_bufferlist_t *buflist,
00353                     isc_task_t *task, isc_taskaction_t action, void *arg,
00354                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
00355                     unsigned int flags)
00356 {
00357         return (isc__socket_sendtov2(sock, buflist, task, action, arg,
00358                                      address, pktinfo, flags));
00359 }
00360 
00361 isc_result_t
00362 isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
00363                     isc_task_t *task,
00364                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
00365                     isc_socketevent_t *event, unsigned int flags)
00366 {
00367         return (isc__socket_sendto2(sock, region, task, address, pktinfo,
00368                                     event, flags));
00369 }
00370 
00371 void
00372 isc_socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
00373         isc__socket_cleanunix(sockaddr, active);
00374 }
00375 
00376 isc_result_t
00377 isc_socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
00378                      isc_uint32_t owner, isc_uint32_t group)
00379 {
00380         return (isc__socket_permunix(sockaddr, perm, owner, group));
00381 }
00382 
00383 isc_result_t
00384 isc_socket_filter(isc_socket_t *sock, const char *filter) {
00385         return (isc__socket_filter(sock, filter));
00386 }
00387 
00388 isc_result_t
00389 isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
00390         return (isc__socket_listen(sock, backlog));
00391 }
00392 
00393 isc_result_t
00394 isc_socket_accept(isc_socket_t *sock, isc_task_t *task,
00395                    isc_taskaction_t action, void *arg)
00396 {
00397         return (isc__socket_accept(sock, task, action, arg));
00398 }
00399 
00400 isc_result_t
00401 isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
00402         return (isc__socket_getpeername(sock, addressp));
00403 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha