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 #ifdef SOCK_WIN_PHARLAP
33 wVersionRequested = MAKEWORD(1, 1);
35 wVersionRequested = MAKEWORD(2, 0);
37 return WSAStartup(wVersionRequested, &wsaData);
41 /*********************************************************************/
49 /*********************************************************************/
51 sock_init_udp(sock_t *sock) {
52 sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
53 if (sock->fd < 0) return -1;
57 /*********************************************************************/
59 sock_cleanup(sock_t *sock) {
62 #elif defined(SOCK_RTLWIP)
63 close_socket_np(sock->fd);
64 #elif defined(SOCK_WIN)
65 closesocket(sock->fd);
69 /*********************************************************************/
71 sock_setsockopt(sock_t *sock,int level,int optname,const char *optval, int optlen) {
72 if (setsockopt(sock->fd, level, optname,(void *)optval, optlen)) {
79 /*********************************************************************/
81 sock_getsockopt(sock_t *sock,int level,int optname,char *optval, int *optlen) {
82 if (getsockopt(sock->fd, level, optname,(void *)optval, optlen)) {
89 /*********************************************************************/
91 sock_bind(sock_t *sock,uint16_t port) {
92 struct sockaddr_in name;
95 name.sin_family = AF_INET;
96 name.sin_port = htons(port);
97 name.sin_addr.s_addr = htonl(INADDR_ANY);
99 #ifndef CONFIG_ORTE_RTL_ONETD
102 &name, sizeof(name)) < 0) {
107 if (getsockname(sock->fd,
108 #ifndef CONFIG_ORTE_RTL_ONETD
112 #ifndef CONFIG_ORTE_RTL_ONETD
121 sock->port=ntohs(name.sin_port);
125 /*********************************************************************/
127 sock_recvfrom(sock_t *sock, void *buf, int max_len,struct sockaddr_in *des,int des_len) {
128 return recvfrom(sock->fd, buf, max_len, 0,
129 #ifndef CONFIG_ORTE_RTL_ONETD
135 /*********************************************************************/
137 sock_sendto(sock_t *sock, void *buf, int len,struct sockaddr_in *des,int des_len) {
138 return sendto(sock->fd, buf, len, 0,
139 #ifndef CONFIG_ORTE_RTL_ONETD
145 /*********************************************************************/
147 sock_ioctl(sock_t *sock, long cmd, unsigned long *arg) {
148 return ioctl(sock->fd, cmd, arg);
151 /*********************************************************************/
153 sock_get_local_interfaces(sock_t *sock,ORTEIFProp *IFProp,char *IFCount) {
154 #if defined(SOCK_BSD)
156 char buf[MAX_INTERFACES*sizeof(struct ifreq)];
159 ifc.ifc_len = sizeof(buf);
162 if (ioctl(sock->fd, SIOCGIFCONF, &ifc) < 0) return -1;
163 for (ptr = buf; ptr < (buf + ifc.ifc_len);ptr += sizeof(struct ifreq)) {
164 struct ifreq* ifr = (struct ifreq*) ptr;
165 struct sockaddr addr;
166 memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
167 ioctl(sock->fd, SIOCGIFFLAGS, ifr);
168 if ((ifr->ifr_flags & IFF_UP) && !(ifr->ifr_flags & IFF_LOOPBACK)) {
170 IFProp->ifFlags=ifr->ifr_flags;
171 IFProp->ipAddress=ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr);
176 #elif defined(SOCK_RTLWIP)
177 /* loopback iface is recognized if it has this address */
178 char ip_address [] = "127.0.0.1";
179 struct in_addr loopaddr;
183 if (inet_aton(ip_address, &loopaddr) != 0) return -1;
185 for (i = 0; i < NIC_TABLE_SIZE; i++) {
186 if (nic_table [i].nic_struct != NULL) {
187 if (nic_table[i].ipad.s_addr != loopaddr.s_addr) {
189 IFProp->ifFlags=0; //RT-Linux doesn't flags
190 IFProp->ipAddress=ntohl(nic_table[i].ipad.s_addr);
196 #elif defined(SOCK_WIN_PHARLAP)
201 EK_TCPSLIPSTATUS slip;
207 while (hDev = EtsTCPIterateDeviceList(hDev)) {
208 pCfg = EtsTCPGetDeviceCfg(hDev);
210 if (pCfg->nwIPAddress == 0x0100007F) // 127.0.0.1 localhost
213 status.eth.length = sizeof(EK_TCPETHSTATUS);
214 EtsTCPGetDeviceStatus(hDev, &status);
215 if (status.eth.DevStatus.Flags & ETS_TCP_DEV_ONLINE) {
216 IFProp->ifFlags = IFF_UP;
217 IFProp->ipAddress = ntohl(pCfg->nwIPAddress);
222 #elif defined(SOCK_WIN)
223 INTERFACE_INFO InterfaceList[MAX_INTERFACES];
224 struct sockaddr_in* pAddress;
228 if (WSAIoctl(sock->fd,SIO_GET_INTERFACE_LIST,NULL,0,
229 InterfaceList, sizeof(InterfaceList),
230 &len, NULL, NULL)==SOCKET_ERROR) return -1;
231 len=len/sizeof(INTERFACE_INFO);
234 pAddress = (struct sockaddr_in*)&(InterfaceList[i].iiAddress);
235 nFlags = InterfaceList[i].iiFlags;
236 if ((nFlags & IFF_UP) && !(nFlags & IFF_LOOPBACK)) {
237 IFProp->ifFlags=nFlags;
238 IFProp->ipAddress=ntohl(pAddress->sin_addr.s_addr);