2 * $Source: /cvs/cvsroot/d2x/arch/win32/arch_ip.cpp,v $
5 * $Date: 2002-02-15 02:29:33 $
7 * arch_ip.cpp - arch specific udp/ip code. (win32 ver)
8 * added 2000/02/07 Matt Mueller (some code borrowed from ipx_udp.c)
10 * $Log: not supported by cvs2svn $
32 //static int dynamic_socket=0x402;
36 //static unsigned char qhbuf[6];
39 /* Do hostname resolve on name "buf" and return the address in buffer "qhbuf".
41 int arch_ip_queryhost(ip_addr *addr,char *buf,u_short baseport)
48 if ((s=strrchr(buf,':'))) {
51 p=ip_portshift(baseport,s+1);
53 else p=ip_portshift(baseport,NULL);//memset(qhbuf+4,0,2);
54 he=gethostbyname((char *)buf);
57 msg("Error resolving my hostname \"%s\"",buf);
60 if (he->h_addrtype!=AF_INET || he->h_length!=4) {
61 msg("Error parsing resolved my hostname \"%s\"",buf);
64 if (!*he->h_addr_list) {
65 msg("My resolved hostname \"%s\" address list empty",buf);
68 addr->set(4,(u_char*)*he->h_addr_list,p);
69 // memcpy(qhbuf,(*he->h_addr_list),4);
75 /* Like dumpraddr() but for structure "sockaddr_in"
78 static void dumpaddr(struct sockaddr_in *sin)
83 memcpy(qhbuf+0,&sin->sin_addr,4);
84 //ports=htons(((short)ntohs(sin->sin_port))-UDP_BASEPORT);
86 memcpy(qhbuf+4,&ports,2);
92 int ip_sendtoca(ip_addr addr,const void *buf,int len){
93 struct sockaddr_in toaddr;
94 if (addr.addrlen()!=4) return 0;//not handled yet
95 toaddr.sin_family=AF_INET;
96 memcpy(&toaddr.sin_addr,addr.addr,4);
97 toaddr.sin_port=*(unsigned short *)(addr.addr+4);
99 printf(MSGHDR "sendtoca((%d) ",len);
100 //dumpraddr(addr.addr);
104 return sendto(mysock,(char*)buf,len,0,(struct sockaddr *)&toaddr,sizeof(toaddr));
108 int arch_ip_get_my_addr(u_short myport){
110 if (gethostname(buf,sizeof(buf))) FAIL("Error getting my hostname");
113 if (ip.dns(buf,myport)) FAIL("Error querying my own hostname \"%s\"",buf);
117 int arch_ip_open_socket(int port){
118 struct sockaddr_in sin;
120 WORD wVersionRequested;
123 wVersionRequested = MAKEWORD(2, 0);
124 if (WSAStartup( wVersionRequested, &wsaData))
129 if ( LOBYTE( wsaData.wVersion ) != 2 ||
130 HIBYTE( wsaData.wVersion ) != 0 ) {
131 /* We couldn't find a usable WinSock DLL. */
137 if ((mysock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
139 FAIL("socket() creation failed on port %d: %m",port);
141 if (setsockopt(mysock,SOL_SOCKET,SO_BROADCAST,(char*)&val_one,sizeof(val_one))) {
142 if (close(mysock)) msg("close() failed during error recovery: %m");
144 FAIL("setsockopt(SO_BROADCAST) failed: %m");
146 sin.sin_family=AF_INET;
147 sin.sin_addr.s_addr=htonl(INADDR_ANY);
148 sin.sin_port=htons(port);
150 if (bind(mysock,(struct sockaddr *)&sin,sizeof(sin))) {
151 if (close(mysock)) msg("close() failed during error recovery: %m");
153 FAIL("bind() to UDP port %d failed: %m",port);
158 void arch_ip_close_socket(void) {
160 msg("close w/o open");
163 if (closesocket(mysock))
164 msg("close() failed on CloseSocket D1X ip socket %m");
168 int arch_ip_recvfrom(char*outbuf,int outbufsize,struct ipx_recv_data *rd){
169 struct sockaddr_in fromaddr;
170 int fromaddrsize=sizeof(fromaddr);
172 static ip_addr ip_fromaddr;
173 ip_addr *vptr=&ip_fromaddr;
175 if ((size=recvfrom(mysock,outbuf,outbufsize,0,(struct sockaddr *)&fromaddr,&fromaddrsize))<0)
178 if (fromaddr.sin_family!=AF_INET) return -1;
181 printf(MSGHDR "recvfrom((%d-2=%d),",size,size-2);
186 ip_fromaddr.set(4,(u_char*)&fromaddr.sin_addr,fromaddr.sin_port);
188 memcpy(rd->src_node,&vptr,sizeof(ip_addr*));
191 int arch_ip_PacketReady(void) {
192 return ipx_general_PacketReady(mysock);