2 * $Id: sock.c,v 0.0.0.1 2003/08/21
4 * DEBUG: section 6 Socket
6 * -------------------------------------------------------------------
8 * Open Real-Time Ethernet
10 * Copyright (C) 2001-2006
11 * Department of Control Engineering FEE CTU Prague, Czech Republic
12 * http://dce.felk.cvut.cz
13 * http://www.ocera.org
15 * Author: Petr Smolik petr.smolik@wo.cz
17 * Project Responsible: Zdenek Hanzalek
18 * --------------------------------------------------------------------
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
34 /*********************************************************************/
37 #if defined(SOCK_BSD) || defined (SOCK_RTLWIP)
39 #elif defined (SOCK_WIN)
40 WORD wVersionRequested;
42 #ifdef SOCK_WIN_PHARLAP
43 wVersionRequested = MAKEWORD(1, 1);
45 wVersionRequested = MAKEWORD(2, 0);
47 return WSAStartup(wVersionRequested, &wsaData);
51 /*********************************************************************/
59 /*********************************************************************/
61 sock_init_udp(sock_t *sock) {
62 sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
63 if (sock->fd < 0) return -1;
67 /*********************************************************************/
69 sock_cleanup(sock_t *sock) {
72 #elif defined(SOCK_RTLWIP)
73 close_socket_np(sock->fd);
74 #elif defined(SOCK_WIN)
75 closesocket(sock->fd);
79 /*********************************************************************/
81 sock_setsockopt(sock_t *sock,int level,int optname,const char *optval, int optlen) {
82 if (setsockopt(sock->fd, level, optname,(void *)optval, optlen)) {
89 /*********************************************************************/
91 sock_getsockopt(sock_t *sock,int level,int optname,char *optval, int *optlen) {
92 if (getsockopt(sock->fd, level, optname,(void *)optval, (socklen_t *)optlen)) {
99 /*********************************************************************/
101 sock_bind(sock_t *sock,uint16_t port, IPAddress listen) {
102 struct sockaddr_in name;
105 name.sin_family = AF_INET;
106 name.sin_port = htons(port);
107 name.sin_addr.s_addr = htonl(listen);
109 #ifndef CONFIG_ORTE_RTL_ONETD
112 &name, sizeof(name)) < 0) {
117 if (getsockname(sock->fd,
118 #ifndef CONFIG_ORTE_RTL_ONETD
122 #ifndef CONFIG_ORTE_RTL_ONETD
131 sock->port=ntohs(name.sin_port);
135 /*********************************************************************/
137 sock_recvfrom(sock_t *sock, void *buf, int max_len,struct sockaddr_in *des,int des_len) {
138 return recvfrom(sock->fd, buf, max_len, 0,
139 #ifndef CONFIG_ORTE_RTL_ONETD
142 des,(socklen_t *)&des_len);
145 /*********************************************************************/
147 sock_sendto(sock_t *sock, void *buf, int len,struct sockaddr_in *des,int des_len) {
148 return sendto(sock->fd, buf, len, 0,
149 #ifndef CONFIG_ORTE_RTL_ONETD
155 /*********************************************************************/
157 sock_ioctl(sock_t *sock, long cmd, unsigned long *arg) {
158 return ioctl(sock->fd, cmd, arg);
161 /*********************************************************************/
163 sock_get_local_interfaces(sock_t *sock,ORTEIFProp *IFProp,char *IFCount) {
164 #if defined(SOCK_BSD)
166 char buf[MAX_INTERFACES*sizeof(struct ifreq)];
169 ifc.ifc_len = sizeof(buf);
172 if (ioctl(sock->fd, SIOCGIFCONF, &ifc) < 0) return -1;
173 for (ptr = buf; ptr < (buf + ifc.ifc_len);ptr += sizeof(struct ifreq)) {
174 struct ifreq* ifr = (struct ifreq*) ptr;
175 struct sockaddr addr;
176 memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
177 ioctl(sock->fd, SIOCGIFFLAGS, ifr);
178 if ((ifr->ifr_flags & IFF_UP) && !(ifr->ifr_flags & IFF_LOOPBACK)) {
180 IFProp->ifFlags=ifr->ifr_flags;
181 IFProp->ipAddress=ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr);
186 #elif defined(SOCK_RTLWIP)
187 /* loopback iface is recognized if it has this address */
188 char ip_address [] = "127.0.0.1";
189 struct in_addr loopaddr;
193 if (inet_aton(ip_address, &loopaddr) != 0) return -1;
195 for (i = 0; i < NIC_TABLE_SIZE; i++) {
196 if (nic_table [i].nic_struct != NULL) {
197 if (nic_table[i].ipad.s_addr != loopaddr.s_addr) {
199 IFProp->ifFlags=0; //RT-Linux doesn't flags
200 IFProp->ipAddress=ntohl(nic_table[i].ipad.s_addr);
206 #elif defined(SOCK_WIN_PHARLAP)
211 EK_TCPSLIPSTATUS slip;
217 while (hDev = EtsTCPIterateDeviceList(hDev)) {
218 pCfg = EtsTCPGetDeviceCfg(hDev);
220 if (pCfg->nwIPAddress == 0x0100007F) // 127.0.0.1 localhost
223 status.eth.length = sizeof(EK_TCPETHSTATUS);
224 EtsTCPGetDeviceStatus(hDev, &status);
225 if (status.eth.DevStatus.Flags & ETS_TCP_DEV_ONLINE) {
226 IFProp->ifFlags = IFF_UP;
227 IFProp->ipAddress = ntohl(pCfg->nwIPAddress);
232 #elif defined(SOCK_WIN)
233 INTERFACE_INFO InterfaceList[MAX_INTERFACES];
234 struct sockaddr_in* pAddress;
238 if (WSAIoctl(sock->fd,SIO_GET_INTERFACE_LIST,NULL,0,
239 InterfaceList, sizeof(InterfaceList),
240 &len, NULL, NULL)==SOCKET_ERROR) return -1;
241 len=len/sizeof(INTERFACE_INFO);
244 pAddress = (struct sockaddr_in*)&(InterfaceList[i].iiAddress);
245 nFlags = InterfaceList[i].iiFlags;
246 if ((nFlags & IFF_UP) && !(nFlags & IFF_LOOPBACK)) {
247 IFProp->ifFlags=nFlags;
248 IFProp->ipAddress=ntohl(pAddress->sin_addr.s_addr);