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_RTL)
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_RTL)
59 close_socket_np(sock->fd);
60 #elif defined(SOCK_WIN)
61 closesocket(sock->fd);
65 /*********************************************************************/
67 sock_setsockopt(sock_t *sock,int optname,const char *optval, int optlen) {
68 if (setsockopt(sock->fd, IPPROTO_IP, optname,(void *)&optval, optlen)) {
75 /*********************************************************************/
77 sock_getsockopt(sock_t *sock,int optname,char *optval, int *optlen) {
78 if (getsockopt(sock->fd, IPPROTO_IP, 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);
94 if (bind(sock->fd, (struct sockaddr *)&name, sizeof(name)) < 0) {
99 if (getsockname(sock->fd,(struct sockaddr *)&name, &size ) < 0) {
103 sock->port=ntohs(name.sin_port);
107 /*********************************************************************/
109 sock_recvfrom(sock_t *sock, void *buf, int max_len,struct sockaddr_in *des,int des_len) {
110 return recvfrom(sock->fd, buf, max_len, 0,(struct sockaddr*)des,&des_len);
113 /*********************************************************************/
115 sock_sendto(sock_t *sock, void *buf, int len,struct sockaddr_in *des,int des_len) {
116 return sendto(sock->fd, buf, len, 0,(struct sockaddr*)des,des_len);
119 /*********************************************************************/
121 sock_ioctl(sock_t *sock, long cmd, unsigned long *arg) {
122 return ioctl(sock->fd, cmd, arg);
125 /*********************************************************************/
127 sock_get_local_interfaces(sock_t *sock,ORTEIFProp *IFProp,char *IFCount) {
128 #if defined(SOCK_BSD)
130 char buf[MAX_INTERFACES*sizeof(struct ifreq)];
133 ifc.ifc_len = sizeof(buf);
136 if (ioctl(sock->fd, SIOCGIFCONF, &ifc) < 0) return -1;
137 for (ptr = buf; ptr < (buf + ifc.ifc_len);ptr += sizeof(struct ifreq)) {
138 struct ifreq* ifr = (struct ifreq*) ptr;
139 struct sockaddr addr;
140 memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
141 ioctl(sock->fd, SIOCGIFFLAGS, ifr);
142 if ((ifr->ifr_flags & IFF_UP) && !(ifr->ifr_flags & IFF_LOOPBACK)) {
144 IFProp->ifFlags=ifr->ifr_flags;
145 IFProp->ipAddress=ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr);
150 #elif defined(SOCK_RTL)
151 /* loopback iface is recognized if it has this address */
152 char ip_address [] = "127.0.0.1";
153 struct in_addr loopaddr;
157 if (inet_aton(ip_address, &loopaddr) != 0) return -1;
159 for (i = 0; i < NIC_TABLE_SIZE; i++) {
160 if (nic_table [i].nic_struct != NULL) {
161 if (nic_table[i].ipad.s_addr != loopaddr.s_addr) {
163 IFProp->ifFlags=0; //RT-Linux doesn't flags
164 IFProp->ipAddress=ntohl(nic_table[i].ipad.s_addr);
170 #elif defined(SOCK_WIN)
171 INTERFACE_INFO InterfaceList[MAX_INTERFACES];
172 struct sockaddr_in* pAddress;
176 if (WSAIoctl(sock->fd,SIO_GET_INTERFACE_LIST,NULL,0,
177 InterfaceList, sizeof(InterfaceList),
178 &len, NULL, NULL)==SOCKET_ERROR) return -1;
179 len=len/sizeof(INTERFACE_INFO);
182 pAddress = (struct sockaddr_in*)&(InterfaceList[i].iiAddress);
183 nFlags = InterfaceList[i].iiFlags;
184 if ((nFlags & IFF_UP) && !(nFlags & IFF_LOOPBACK)) {
185 IFProp->ifFlags=nFlags;
186 IFProp->ipAddress=ntohl(pAddress->sin_addr.s_addr);