1 /*
2 * Copyright (c) 2012, Texas Instruments Incorporated
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 * @brief Shared socket functions.
35 *
36 */
38 /* Standard headers */
39 #include <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 /* SysLink Socket Protocol Family */
50 #include <net/rpmsg.h>
52 /* For PRINTVERBOSE* */
53 #include <_lad.h>
55 static Bool verbose = FALSE;
57 int ConnectSocket(int sock, UInt16 procId, int dst)
58 {
59 int err;
60 struct sockaddr_rpmsg srcAddr, dstAddr;
61 socklen_t len;
63 /* connect to remote service */
64 memset(&dstAddr, 0, sizeof(dstAddr));
65 dstAddr.family = AF_RPMSG;
66 /* rpmsg "vproc_id" is one less than the MultiProc ID: */
67 dstAddr.vproc_id = procId - 1;
68 dstAddr.addr = dst;
70 len = sizeof(struct sockaddr_rpmsg);
71 err = connect(sock, (struct sockaddr *)&dstAddr, len);
72 if (err < 0) {
73 printf("connect failed: %s (%d)\n", strerror(errno), errno);
74 return (-1);
75 }
77 /* let's see what local address we got */
78 err = getsockname(sock, (struct sockaddr *)&srcAddr, &len);
79 if (err < 0) {
80 printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
81 return (-1);
82 }
84 PRINTVERBOSE3("Connected over sock: %d\n\tdst vproc_id: %d, dst addr: %d\n",
85 sock, dstAddr.vproc_id, dstAddr.addr)
86 PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",
87 srcAddr.vproc_id, srcAddr.addr)
89 return(0);
90 }
92 int SocketBindAddr(int fd, UInt16 rprocId, UInt32 localAddr)
93 {
94 int err;
95 socklen_t len;
96 struct sockaddr_rpmsg srcAddr;
98 /* Now bind to the source address. */
99 memset(&srcAddr, 0, sizeof(srcAddr));
100 srcAddr.family = AF_RPMSG;
101 /* We bind the remote proc ID, but local address! */
102 srcAddr.vproc_id = (rprocId - 1);
103 srcAddr.addr = localAddr;
105 len = sizeof(struct sockaddr_rpmsg);
106 err = bind(fd, (struct sockaddr *)&srcAddr, len);
107 if (err >= 0) {
108 PRINTVERBOSE3("socket_bind_addr: bound sock: %d\n\tto dst "
109 "vproc_id: %d, src addr: %d\n",
110 fd, srcAddr.vproc_id, srcAddr.addr)
112 /* let's see what local address we got */
113 err = getsockname(fd, (struct sockaddr *)&srcAddr, &len);
114 if (err < 0) {
115 printf("getsockname failed: %s (%d)\n", strerror(errno), errno);
116 }
117 else {
118 PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",
119 srcAddr.vproc_id, srcAddr.addr)
120 }
121 }
123 return (err);
124 }