2 * $Source: /cvs/cvsroot/d2x/arch/linux/arch_ip.cpp,v $
5 * $Date: 2002-02-06 09:22:41 $
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)
10 * $Log: not supported by cvs2svn $
19 #include <netinet/in.h> /* for htons & co. */
24 #include <sys/ioctl.h>
38 /* Do hostname resolve on name "buf" and return the address in buffer "qhbuf".
40 int arch_ip_queryhost(ip_addr *addr,char *buf,u_short baseport)
47 if ((s=strrchr(buf,':'))) {
50 p=ip_portshift(baseport,s+1);
52 else p=ip_portshift(baseport,NULL);//memset(qhbuf+4,0,2);
53 he=gethostbyname((char *)buf);
56 msg("Error resolving my hostname \"%s\"",buf);
59 if (he->h_addrtype!=AF_INET || he->h_length!=4) {
60 msg("Error parsing resolved my hostname \"%s\"",buf);
63 if (!*he->h_addr_list) {
64 msg("My resolved hostname \"%s\" address list empty",buf);
67 addr->set(4,(u_char*)*he->h_addr_list,p);
68 // memcpy(qhbuf,(*he->h_addr_list),4);
74 /* Like dumpraddr() but for structure "sockaddr_in"
77 static void dumpaddr(struct sockaddr_in *sin)
82 memcpy(qhbuf+0,&sin->sin_addr,4);
83 //ports=htons(((short)ntohs(sin->sin_port))-UDP_BASEPORT);
85 memcpy(qhbuf+4,&ports,2);
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);
98 printf(MSGHDR "sendtoca((%d) ",len);
99 //dumpraddr(addr.addr);
103 return sendto(mysock,buf,len,0,(struct sockaddr *)&toaddr,sizeof(toaddr));
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
111 #define MAX_BRDINTERFACES 16
113 /* We require the interface to be UP and RUNNING to accept it.
116 #define IF_REQFLAGS (IFF_UP|IFF_RUNNING)
118 /* We reject any interfaces declared as LOOPBACK type.
120 #define IF_NOTFLAGS (IFF_LOOPBACK)
122 int arch_ip_get_my_addr(u_short myport){
123 unsigned cnt=MAX_BRDINTERFACES,i,j;
124 struct ifconf ifconf;
126 struct sockaddr_in *sinp;
129 if ((sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0)
130 FAIL("Creating socket() failure during broadcast detection: %m");
133 if (ioctl(sock,SIOCGIFCOUNT,&cnt))
134 { /* msg("Getting iterface count error: %m"); */ }
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)) {
142 FAIL("ioctl(SIOCGIFCONF) failure during broadcast detection: %m");
144 cnt=ifconf.ifc_len/sizeof(struct ifreq);
145 // chk(broads=malloc(cnt*sizeof(*broads)));
147 for (i=j=0;i<cnt;i++) {
148 if (ioctl(sock,SIOCGIFFLAGS,ifconf.ifc_req+i)) {
150 FAIL("ioctl(udp,\"%s\",SIOCGIFFLAGS) error: %m",ifconf.ifc_req[i].ifr_name);
152 if (((ifconf.ifc_req[i].ifr_flags&IF_REQFLAGS)!=IF_REQFLAGS)||
153 (ifconf.ifc_req[i].ifr_flags&IF_NOTFLAGS))
155 if (ioctl(sock,SIOCGIFDSTADDR,ifconf.ifc_req+i)) {
157 FAIL("ioctl(udp,\"%s\",SIOCGIFDSTADDR) error: %m",ifconf.ifc_req[i].ifr_name);
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));
164 printf(MSGHDR"added if ");
168 ip_my_addrs.add(addr);
173 /*int arch_ip_get_my_addr(u_short myport){
175 if (gethostname(buf,sizeof(buf))) FAIL("Error getting my hostname");
178 if (ip.dns(buf,myport)) FAIL("Error querying my own hostname \"%s\"",buf);
182 int arch_ip_open_socket(int port){
183 struct sockaddr_in sin;
185 if ((mysock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
187 FAIL("socket() creation failed on port %d: %m",port);
189 if (setsockopt(mysock,SOL_SOCKET,SO_BROADCAST,&val_one,sizeof(val_one))) {
190 if (close(mysock)) msg("close() failed during error recovery: %m");
192 FAIL("setsockopt(SO_BROADCAST) failed: %m");
194 sin.sin_family=AF_INET;
195 sin.sin_addr.s_addr=htonl(INADDR_ANY);
196 sin.sin_port=htons(port);
198 if (bind(mysock,(struct sockaddr *)&sin,sizeof(sin))) {
199 if (close(mysock)) msg("close() failed during error recovery: %m");
201 FAIL("bind() to UDP port %d failed: %m",port);
206 void arch_ip_close_socket(void) {
208 msg("close w/o open");
212 msg("close() failed on CloseSocket D1X ip socket %m");
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);
219 static ip_addr ip_fromaddr;
220 ip_addr *vptr=&ip_fromaddr;
222 if ((size=recvfrom(mysock,outbuf,outbufsize,0,(struct sockaddr *)&fromaddr,&fromaddrsize))<0)
225 if (fromaddr.sin_family!=AF_INET) return -1;
228 printf(MSGHDR "recvfrom((%d-2=%d),",size,size-2);
233 ip_fromaddr.set(4,(u_char*)&fromaddr.sin_addr,fromaddr.sin_port);
235 memcpy(rd->src_node,&vptr,sizeof(ip_addr*));
238 int arch_ip_PacketReady(void) {
239 return ipx_general_PacketReady(mysock);