Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | |
| 3 | (c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29 |
| 4 | www.systec-electronic.com |
| 5 | |
| 6 | Project: openPOWERLINK |
| 7 | |
| 8 | Description: Wrapper for BSD socket API for Linux kernel |
| 9 | |
| 10 | License: |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without |
| 13 | modification, are permitted provided that the following conditions |
| 14 | are met: |
| 15 | |
| 16 | 1. Redistributions of source code must retain the above copyright |
| 17 | notice, this list of conditions and the following disclaimer. |
| 18 | |
| 19 | 2. Redistributions in binary form must reproduce the above copyright |
| 20 | notice, this list of conditions and the following disclaimer in the |
| 21 | documentation and/or other materials provided with the distribution. |
| 22 | |
| 23 | 3. Neither the name of SYSTEC electronic GmbH nor the names of its |
| 24 | contributors may be used to endorse or promote products derived |
| 25 | from this software without prior written permission. For written |
| 26 | permission, please contact info@systec-electronic.com. |
| 27 | |
| 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 29 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 30 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 31 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 32 | COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 33 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 34 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 36 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 37 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 38 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 39 | POSSIBILITY OF SUCH DAMAGE. |
| 40 | |
| 41 | Severability Clause: |
| 42 | |
| 43 | If a provision of this License is or becomes illegal, invalid or |
| 44 | unenforceable in any jurisdiction, that shall not affect: |
| 45 | 1. the validity or enforceability in that jurisdiction of any other |
| 46 | provision of this License; or |
| 47 | 2. the validity or enforceability in other jurisdictions of that or |
| 48 | any other provision of this License. |
| 49 | |
| 50 | ------------------------------------------------------------------------- |
| 51 | |
| 52 | $RCSfile: SocketLinuxKernel.c,v $ |
| 53 | |
| 54 | $Author: D.Krueger $ |
| 55 | |
| 56 | $Revision: 1.3 $ $Date: 2008/04/17 21:36:32 $ |
| 57 | |
| 58 | $State: Exp $ |
| 59 | |
| 60 | Build Environment: |
| 61 | Dev C++ and GNU-Compiler for m68k |
| 62 | |
| 63 | ------------------------------------------------------------------------- |
| 64 | |
| 65 | Revision History: |
| 66 | |
| 67 | 2006/08/25 d.k.: start of implementation |
| 68 | |
| 69 | ****************************************************************************/ |
| 70 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 71 | #include <linux/net.h> |
| 72 | #include <linux/in.h> |
| 73 | #include "SocketLinuxKernel.h" |
| 74 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 75 | /***************************************************************************/ |
| 76 | /* */ |
| 77 | /* */ |
| 78 | /* G L O B A L D E F I N I T I O N S */ |
| 79 | /* */ |
| 80 | /* */ |
| 81 | /***************************************************************************/ |
| 82 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 83 | //--------------------------------------------------------------------------- |
| 84 | // const defines |
| 85 | //--------------------------------------------------------------------------- |
| 86 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 87 | //--------------------------------------------------------------------------- |
| 88 | // local types |
| 89 | //--------------------------------------------------------------------------- |
| 90 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 91 | //--------------------------------------------------------------------------- |
| 92 | // modul globale vars |
| 93 | //--------------------------------------------------------------------------- |
| 94 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 95 | //--------------------------------------------------------------------------- |
| 96 | // local function prototypes |
| 97 | //--------------------------------------------------------------------------- |
| 98 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 99 | //--------------------------------------------------------------------------- |
| 100 | // Kernel Module specific Data Structures |
| 101 | //--------------------------------------------------------------------------- |
| 102 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 103 | //=========================================================================// |
| 104 | // // |
| 105 | // P U B L I C F U N C T I O N S // |
| 106 | // // |
| 107 | //=========================================================================// |
| 108 | |
| 109 | //--------------------------------------------------------------------------- |
| 110 | // |
| 111 | // Function: |
| 112 | // |
| 113 | // Description: |
| 114 | // |
| 115 | // |
| 116 | // |
| 117 | // Parameters: |
| 118 | // |
| 119 | // |
| 120 | // Returns: |
| 121 | // |
| 122 | // |
| 123 | // State: |
| 124 | // |
| 125 | //--------------------------------------------------------------------------- |
| 126 | |
| 127 | SOCKET socket(int af, int type, int protocol) |
| 128 | { |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 129 | int rc; |
| 130 | SOCKET socket; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 131 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 132 | rc = sock_create_kern(af, type, protocol, &socket); |
| 133 | if (rc < 0) { |
| 134 | socket = NULL; |
| 135 | goto Exit; |
| 136 | } |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 137 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 138 | Exit: |
| 139 | return socket; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int bind(SOCKET socket_p, const struct sockaddr *addr, int addrlen) |
| 143 | { |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 144 | int rc; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 145 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 146 | rc = socket_p->ops->bind(socket_p, (struct sockaddr *)addr, addrlen); |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 147 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 148 | return rc; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int closesocket(SOCKET socket_p) |
| 152 | { |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 153 | sock_release(socket_p); |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 154 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 155 | return 0; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 158 | int recvfrom(SOCKET socket_p, char *buf, int len, int flags, |
| 159 | struct sockaddr *from, int *fromlen) |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 160 | { |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 161 | int rc; |
| 162 | struct msghdr msg; |
| 163 | struct kvec iov; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 164 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 165 | msg.msg_control = NULL; |
| 166 | msg.msg_controllen = 0; |
| 167 | msg.msg_name = from; // will be struct sock_addr |
| 168 | msg.msg_namelen = *fromlen; |
| 169 | iov.iov_len = len; |
| 170 | iov.iov_base = buf; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 171 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 172 | rc = kernel_recvmsg(socket_p, &msg, &iov, 1, iov.iov_len, 0); |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 173 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 174 | return rc; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 177 | int sendto(SOCKET socket_p, const char *buf, int len, int flags, |
| 178 | const struct sockaddr *to, int tolen) |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 179 | { |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 180 | int rc; |
| 181 | struct msghdr msg; |
| 182 | struct kvec iov; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 183 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 184 | msg.msg_control = NULL; |
| 185 | msg.msg_controllen = 0; |
| 186 | msg.msg_name = (struct sockaddr *)to; // will be struct sock_addr |
| 187 | msg.msg_namelen = tolen; |
| 188 | msg.msg_flags = 0; |
| 189 | iov.iov_len = len; |
| 190 | iov.iov_base = (char *)buf; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 191 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 192 | rc = kernel_sendmsg(socket_p, &msg, &iov, 1, len); |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 193 | |
Greg Kroah-Hartman | 833dfbe | 2008-12-19 17:11:52 -0800 | [diff] [blame] | 194 | return rc; |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Daniel Krueger | 9d7164c | 2008-12-19 11:41:57 -0800 | [diff] [blame] | 197 | // EOF |