]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/utils/SocketFxns.c
SDOCM00115428 Incorrect return status from NameServer_delete
[ipc/ipcdev.git] / linux / src / utils / SocketFxns.c
1 /*
2  * Copyright (c) 2012-2015 Texas Instruments Incorporated - http://www.ti.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  *  @file       SocketFxns.c
34  *
35  *  @brief      Shared socket functions.
36  */
38 /* Standard headers */
39 #include <ti/ipc/Std.h>
41 /* Socket Headers */
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
49 /* Socket Protocol Family */
50 #include <net/rpmsg.h>
52 /* For PRINTVERBOSE* */
53 #include <_lad.h>
55 /* For MultiProc id to remoteproc index map */
56 #include <_MultiProc.h>
58 /*  Traces in this file are controlled via _SocketFxns_verbose
59  *
60  *  Caution! This file is used by both LAD and TransportRpmsg. The
61  *  PRINTVERBOSE macros use printf(), which sends its output to
62  *  STDOUT (fd = 0). However, LAD closes STDOUT and by unfortunate
63  *  chance, the file descriptor zero (fd = 0) is reused for the outbound
64  *  pipe to the first client. This means all trace messages go into
65  *  the pipe and corrupt the data on the client side.
67  *  If you want to enable this trace for LAD, in addition to setting
68  *  _SocketFxns_versbose to TRUE below, you must also comment out the
69  *  three calls to close() in linux/src/daemon/lad.c:
70  *
71  *      close(STDIN_FILENO);
72  *      close(STDOUT_FILENO);
73  *      close(STDERR_FILENO);
74  *
75  *  If you only want to enable trace in the client (i.e. calls from
76  *  TransportRpmsg), then simply set the environment variable IPC_DEBUG
77  *  (to either 2 or 3) before you run your program.
78  *
79  *  IPC_DEBUG=3 ./app_host
80  */
81 Bool _SocketFxns_verbose = FALSE;
82 #define verbose _SocketFxns_verbose
84 int ConnectSocket(int sock, UInt16 procId, int dst)
85 {
86     int                   err;
87     struct sockaddr_rpmsg srcAddr, dstAddr;
88     socklen_t             len;
89     UInt16                clusterId;
91     /* map procId to clusterId (rprocList[] indexed by clusterId) */
92     clusterId = procId - _MultiProc_cfg.baseIdOfCluster;
94     /* connect to remote service */
95     memset(&dstAddr, 0, sizeof(dstAddr));
96     dstAddr.family     = AF_RPMSG;
97     /* convert MultiProc 'clusterId' to remoteproc index */
98     dstAddr.vproc_id   = _MultiProc_cfg.rprocList[clusterId];
99     dstAddr.addr       = dst;
101     len = sizeof(struct sockaddr_rpmsg);
102     err = connect(sock, (struct sockaddr *)&dstAddr, len);
103     if (err < 0) {
104         /* don't hard-printf since this is no longer fatal */
105         PRINTVERBOSE2("connect failed: %s (%d)\n", strerror(errno), errno);
106         return (-1);
107     }
109     /* let's see what local address we got */
110     err = getsockname(sock, (struct sockaddr *)&srcAddr, &len);
111     if (err < 0) {
112         printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
113         return (-1);
114     }
116     PRINTVERBOSE3("Connected over sock: %d\n\tdst vproc_id: %d, dst addr: %d\n",
117                   sock, dstAddr.vproc_id, dstAddr.addr)
118     PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",
119                   srcAddr.vproc_id, srcAddr.addr)
121     return(0);
124 int SocketBindAddr(int fd, UInt16 procId, UInt32 localAddr)
126     int         err;
127     socklen_t    len;
128     struct sockaddr_rpmsg srcAddr;
129     UInt16 clusterId;
131     /* map procId to clusterId (rprocList[] indexed by clusterId) */
132     clusterId = procId - _MultiProc_cfg.baseIdOfCluster;
134     /* Now bind to the source address.   */
135     memset(&srcAddr, 0, sizeof(srcAddr));
136     srcAddr.family = AF_RPMSG;
137     /* We bind the remote clusterId, but local address! */
138     srcAddr.vproc_id   = _MultiProc_cfg.rprocList[clusterId];
139     srcAddr.addr  = localAddr;
141     len = sizeof(struct sockaddr_rpmsg);
142     err = bind(fd, (struct sockaddr *)&srcAddr, len);
143     if (err >= 0) {
144         PRINTVERBOSE3("socket_bind_addr: bound sock: %d\n\tto dst "
145                       "vproc_id: %d, src addr: %d\n",
146                       fd, srcAddr.vproc_id, srcAddr.addr)
148         /* let's see what local address we got */
149         err = getsockname(fd, (struct sockaddr *)&srcAddr, &len);
150         if (err < 0) {
151             printf("getsockname failed: %s (%d)\n", strerror(errno), errno);
152         }
153         else {
154             PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",
155                           srcAddr.vproc_id, srcAddr.addr)
156         }
157     }
159     return (err);