2 * $Id: sock.c,v 0.0.0.1 2003/08/21
4 * DEBUG: section 6 Socket
5 * AUTHOR: Petr Smolik petr.smolik@wo.cz
7 * ORTE - OCERA Real-Time Ethernet http://www.ocera.org/
8 * --------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
24 /*********************************************************************/
27 #if defined(SOCK_BSD) || defined (SOCK_RTLWIP)
29 #elif defined (SOCK_WIN)
30 WORD wVersionRequested;
32 wVersionRequested = MAKEWORD(2, 0);
33 return WSAStartup(wVersionRequested, &wsaData);
37 /*********************************************************************/
45 /*********************************************************************/
47 sock_init_udp(sock_t *sock) {
48 sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
49 if (sock->fd < 0) return -1;
53 /*********************************************************************/
55 sock_cleanup(sock_t *sock) {
58 #elif defined(SOCK_RTLWIP)
59 close_socket_np(sock->fd);
60 #elif defined(SOCK_WIN)
61 closesocket(sock->fd);
65 /*********************************************************************/
67 sock_setsockopt(sock_t *sock,int level,int optname,const char *optval, int optlen) {
68 if (setsockopt(sock->fd, level, optname,(void *)optval, optlen)) {
75 /*********************************************************************/
77 sock_getsockopt(sock_t *sock,int level,int optname,char *optval, int *optlen) {
78 if (getsockopt(sock->fd, level, optname,(void *)optval, optlen)) {
85 /*********************************************************************/
87 sock_bind(sock_t *sock,uint16_t port) {
88 struct sockaddr_in name;
91 name.sin_family = AF_INET;
92 name.sin_port = htons(port);
93 name.sin_addr.s_addr = htonl(INADDR_ANY);
95 #ifndef CONFIG_ORTE_RTL_ONETD
98 &name, sizeof(name)) < 0) {
103 if (getsockname(sock->fd,
104 #ifndef CONFIG_ORTE_RTL_ONETD
108 #ifndef CONFIG_ORTE_RTL_ONETD
117 sock->port=ntohs(name.sin_port);
121 /*********************************************************************/
123 sock_recvfrom(sock_t *sock, void *buf, int max_len,struct sockaddr_in *des,int des_len) {
124 return recvfrom(sock->fd, buf, max_len, 0,
125 #ifndef CONFIG_ORTE_RTL_ONETD
131 /*********************************************************************/
133 sock_sendto(sock_t *sock, void *buf, int len,struct sockaddr_in *des,int des_len) {
134 return sendto(sock->fd, buf, len, 0,
135 #ifndef CONFIG_ORTE_RTL_ONETD
141 /*********************************************************************/
143 sock_ioctl(sock_t *sock, long cmd, unsigned long *arg) {
144 return ioctl(sock->fd, cmd, arg);
147 /*********************************************************************/
149 sock_get_local_interfaces(sock_t *sock,ORTEIFProp *IFProp,char *IFCount) {
150 #if defined(SOCK_BSD)
152 char buf[MAX_INTERFACES*sizeof(struct ifreq)];
155 ifc.ifc_len = sizeof(buf);
158 if (ioctl(sock->fd, SIOCGIFCONF, &ifc) < 0) return -1;
159 for (ptr = buf; ptr < (buf + ifc.ifc_len);ptr += sizeof(struct ifreq)) {
160 struct ifreq* ifr = (struct ifreq*) ptr;
161 struct sockaddr addr;
162 memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
163 ioctl(sock->fd, SIOCGIFFLAGS, ifr);
164 if ((ifr->ifr_flags & IFF_UP) && !(ifr->ifr_flags & IFF_LOOPBACK)) {
166 IFProp->ifFlags=ifr->ifr_flags;
167 IFProp->ipAddress=ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr);
172 #elif defined(SOCK_RTLWIP)
173 /* loopback iface is recognized if it has this address */
174 char ip_address [] = "127.0.0.1";
175 struct in_addr loopaddr;
179 if (inet_aton(ip_address, &loopaddr) != 0) return -1;
181 for (i = 0; i < NIC_TABLE_SIZE; i++) {
182 if (nic_table [i].nic_struct != NULL) {
183 if (nic_table[i].ipad.s_addr != loopaddr.s_addr) {
185 IFProp->ifFlags=0; //RT-Linux doesn't flags
186 IFProp->ipAddress=ntohl(nic_table[i].ipad.s_addr);
192 #elif defined(SOCK_WIN)
193 INTERFACE_INFO InterfaceList[MAX_INTERFACES];
194 struct sockaddr_in* pAddress;
198 if (WSAIoctl(sock->fd,SIO_GET_INTERFACE_LIST,NULL,0,
199 InterfaceList, sizeof(InterfaceList),
200 &len, NULL, NULL)==SOCKET_ERROR) return -1;
201 len=len/sizeof(INTERFACE_INFO);
204 pAddress = (struct sockaddr_in*)&(InterfaceList[i].iiAddress);
205 nFlags = InterfaceList[i].iiFlags;
206 if ((nFlags & IFF_UP) && !(nFlags & IFF_LOOPBACK)) {
207 IFProp->ifFlags=nFlags;
208 IFProp->ipAddress=ntohl(pAddress->sin_addr.s_addr);