]> icculus.org git repositories - btb/d2x.git/blob - arch/linux/arch_ip.cpp
copied from d1x
[btb/d2x.git] / arch / linux / arch_ip.cpp
1 /*
2  * $Source: /cvs/cvsroot/d2x/arch/linux/arch_ip.cpp,v $
3  * $Revision: 1.1 $
4  * $Author: bradleyb $
5  * $Date: 2002-02-06 09:22:41 $
6  *
7  * arch_ip.cpp - arch specific udp/ip code.  (linux ver)
8  * added 2000/02/07 Matt Mueller (some code borrowed from ipx_udp.c)
9  *
10  * $Log: not supported by cvs2svn $
11  *
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include <string.h>
19 #include <netinet/in.h> /* for htons & co. */
20 #include <unistd.h>
21 #include <netdb.h>
22 #include <stdlib.h>
23 #include <net/if.h>
24 #include <sys/ioctl.h>
25 #include <ctype.h>
26 extern "C" {
27 #include "ipx_drv.h"
28 #include "pstypes.h"
29 #include "mono.h"
30 //#include "multi.h"
31 }
32
33 #include "ip_base.h"
34 #include "ipclient.h"
35
36 static int mysock=-1;
37
38 /* Do hostname resolve on name "buf" and return the address in buffer "qhbuf".
39  */
40 int arch_ip_queryhost(ip_addr *addr,char *buf,u_short baseport)
41 {
42         struct hostent *he;
43         char *s;
44         char c=0;
45         u_short p;
46
47         if ((s=strrchr(buf,':'))) {
48                 c=*s;
49                 *s='\0';
50                 p=ip_portshift(baseport,s+1);
51         }
52         else p=ip_portshift(baseport,NULL);//memset(qhbuf+4,0,2);
53         he=gethostbyname((char *)buf);
54         if (s) *s=c;
55         if (!he) {
56                 msg("Error resolving my hostname \"%s\"",buf);
57                 return -1; //(NULL);
58         }
59         if (he->h_addrtype!=AF_INET || he->h_length!=4) {
60                 msg("Error parsing resolved my hostname \"%s\"",buf);
61                 return -1;//(NULL);
62         }
63         if (!*he->h_addr_list) {
64                 msg("My resolved hostname \"%s\" address list empty",buf);
65                 return -1;//(NULL);
66         }
67         addr->set(4,(u_char*)*he->h_addr_list,p);
68 //      memcpy(qhbuf,(*he->h_addr_list),4);
69 //      return(qhbuf);
70         return 0;
71 }
72
73 #ifdef UDPDEBUG
74 /* Like dumpraddr() but for structure "sockaddr_in"
75  */
76
77 static void dumpaddr(struct sockaddr_in *sin)
78 {
79         unsigned short ports;
80         u_char qhbuf[6];
81
82         memcpy(qhbuf+0,&sin->sin_addr,4);
83         //ports=htons(((short)ntohs(sin->sin_port))-UDP_BASEPORT);
84         ports=sin->sin_port;
85         memcpy(qhbuf+4,&ports,2);
86         dumpraddr(qhbuf);
87 }
88 #endif
89
90
91 int ip_sendtoca(ip_addr addr,const void *buf,int len){
92         struct sockaddr_in toaddr;
93         if (addr.addrlen()!=4) return 0;//not handled yet
94         toaddr.sin_family=AF_INET;
95         memcpy(&toaddr.sin_addr,addr.addr,4);
96         toaddr.sin_port=*(unsigned short *)(addr.addr+4);
97 #ifdef UDPDEBUG
98         printf(MSGHDR "sendtoca((%d) ",len);
99         //dumpraddr(addr.addr);
100         addr.dump();
101         puts(").");
102 #endif
103         return sendto(mysock,buf,len,0,(struct sockaddr *)&toaddr,sizeof(toaddr));
104 }
105
106 /* Find as much as MAX_BRDINTERFACES during local iface autoconfiguration.
107  * Note that more interfaces can be added during manual configuration
108  * or host-received-packet autoconfiguration
109  */
110
111 #define MAX_BRDINTERFACES 16
112
113 /* We require the interface to be UP and RUNNING to accept it.
114  */
115
116 #define IF_REQFLAGS (IFF_UP|IFF_RUNNING)
117
118 /* We reject any interfaces declared as LOOPBACK type.
119  */
120 #define IF_NOTFLAGS (IFF_LOOPBACK)
121
122 int arch_ip_get_my_addr(u_short myport){
123         unsigned cnt=MAX_BRDINTERFACES,i,j;
124         struct ifconf ifconf;
125         int sock;
126         struct sockaddr_in *sinp;
127         ip_addr addr;
128
129         if ((sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0)
130                 FAIL("Creating socket() failure during broadcast detection: %m");
131
132 #ifdef SIOCGIFCOUNT
133         if (ioctl(sock,SIOCGIFCOUNT,&cnt))
134         { /* msg("Getting iterface count error: %m"); */ }
135         else
136                 cnt=cnt*2+2;
137 #endif
138
139         chk(ifconf.ifc_req=(struct ifreq *)alloca((ifconf.ifc_len=cnt*sizeof(struct ifreq))));
140         if (ioctl(sock,SIOCGIFCONF,&ifconf)||ifconf.ifc_len%sizeof(struct ifreq)) {
141                 close(sock);
142                 FAIL("ioctl(SIOCGIFCONF) failure during broadcast detection: %m");
143         }
144         cnt=ifconf.ifc_len/sizeof(struct ifreq);
145         //      chk(broads=malloc(cnt*sizeof(*broads)));
146         //      broadsize=cnt;
147         for (i=j=0;i<cnt;i++) {
148                 if (ioctl(sock,SIOCGIFFLAGS,ifconf.ifc_req+i)) {
149                         close(sock);
150                         FAIL("ioctl(udp,\"%s\",SIOCGIFFLAGS) error: %m",ifconf.ifc_req[i].ifr_name);
151                 }
152                 if (((ifconf.ifc_req[i].ifr_flags&IF_REQFLAGS)!=IF_REQFLAGS)||
153                                 (ifconf.ifc_req[i].ifr_flags&IF_NOTFLAGS))
154                         continue;
155                 if (ioctl(sock,SIOCGIFDSTADDR,ifconf.ifc_req+i)) {
156                         close(sock);
157                         FAIL("ioctl(udp,\"%s\",SIOCGIFDSTADDR) error: %m",ifconf.ifc_req[i].ifr_name);
158                 }
159
160                 sinp=(struct sockaddr_in *)&ifconf.ifc_req[i].ifr_netmask  ;
161                 if (sinp->sin_family!=AF_INET) continue;
162                 addr.set(4,(ubyte*)&sinp->sin_addr,htons(myport));
163 #ifdef UDPDEBUG
164                 printf(MSGHDR"added if ");
165                 addr.dump();
166                 printf("\n");
167 #endif
168                 ip_my_addrs.add(addr);
169         }
170         return(0);
171 }
172
173 /*int arch_ip_get_my_addr(u_short myport){
174         char buf[256];
175         if (gethostname(buf,sizeof(buf))) FAIL("Error getting my hostname");
176
177         ip_addr ip;
178         if (ip.dns(buf,myport)) FAIL("Error querying my own hostname \"%s\"",buf);
179         ip_my_addrs.add(ip);
180         return 0;
181 }*/
182 int arch_ip_open_socket(int port){
183         struct sockaddr_in sin;
184         int val_one=1;
185         if ((mysock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
186                 mysock = -1;
187                 FAIL("socket() creation failed on port %d: %m",port);
188         }
189         if (setsockopt(mysock,SOL_SOCKET,SO_BROADCAST,&val_one,sizeof(val_one))) {
190                 if (close(mysock)) msg("close() failed during error recovery: %m");
191                 mysock=-1;
192                 FAIL("setsockopt(SO_BROADCAST) failed: %m");
193         }
194         sin.sin_family=AF_INET;
195         sin.sin_addr.s_addr=htonl(INADDR_ANY);
196         sin.sin_port=htons(port);
197
198         if (bind(mysock,(struct sockaddr *)&sin,sizeof(sin))) {
199                 if (close(mysock)) msg("close() failed during error recovery: %m");
200                 mysock=-1;
201                 FAIL("bind() to UDP port %d failed: %m",port);
202         }
203
204         return 0;
205 }
206 void arch_ip_close_socket(void) {
207         if (mysock<0) {
208                 msg("close w/o open");
209                 return;
210         }
211         if (close(mysock))
212                 msg("close() failed on CloseSocket D1X ip socket %m");
213         mysock=-1;
214 }
215 int arch_ip_recvfrom(char*outbuf,int outbufsize,struct ipx_recv_data *rd){
216         struct sockaddr_in fromaddr;
217         socklen_t fromaddrsize=sizeof(fromaddr);
218         int size;
219         static ip_addr ip_fromaddr;
220         ip_addr *vptr=&ip_fromaddr;
221
222         if ((size=recvfrom(mysock,outbuf,outbufsize,0,(struct sockaddr *)&fromaddr,&fromaddrsize))<0)
223                 return -1;
224
225         if (fromaddr.sin_family!=AF_INET) return -1;
226
227 #ifdef UDPDEBUG
228         printf(MSGHDR "recvfrom((%d-2=%d),",size,size-2);
229         dumpaddr(&fromaddr);
230         puts(").");
231 #endif
232
233         ip_fromaddr.set(4,(u_char*)&fromaddr.sin_addr,fromaddr.sin_port);
234
235         memcpy(rd->src_node,&vptr,sizeof(ip_addr*));
236         return size;
237 }
238 int arch_ip_PacketReady(void) {
239         return ipx_general_PacketReady(mysock);
240 }