2 * $Source: /cvs/cvsroot/d2x/arch/win32/arch_ip.cpp,v $
5 * $Date: 2002-02-15 06:41:41 $
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 $
11 * Revision 1.1 2002/02/15 02:29:33 bradleyb
35 //static int dynamic_socket=0x402;
39 //static unsigned char qhbuf[6];
42 /* Do hostname resolve on name "buf" and return the address in buffer "qhbuf".
44 int arch_ip_queryhost(ip_addr *addr,char *buf,u_short baseport)
51 if ((s=strrchr(buf,':'))) {
54 p=ip_portshift(baseport,s+1);
56 else p=ip_portshift(baseport,NULL);//memset(qhbuf+4,0,2);
57 he=gethostbyname((char *)buf);
60 msg("Error resolving my hostname \"%s\"",buf);
63 if (he->h_addrtype!=AF_INET || he->h_length!=4) {
64 msg("Error parsing resolved my hostname \"%s\"",buf);
67 if (!*he->h_addr_list) {
68 msg("My resolved hostname \"%s\" address list empty",buf);
71 addr->set(4,(u_char*)*he->h_addr_list,p);
72 // memcpy(qhbuf,(*he->h_addr_list),4);
78 /* Like dumpraddr() but for structure "sockaddr_in"
81 static void dumpaddr(struct sockaddr_in *sin)
86 memcpy(qhbuf+0,&sin->sin_addr,4);
87 //ports=htons(((short)ntohs(sin->sin_port))-UDP_BASEPORT);
89 memcpy(qhbuf+4,&ports,2);
95 int ip_sendtoca(ip_addr addr,const void *buf,int len){
96 struct sockaddr_in toaddr;
97 if (addr.addrlen()!=4) return 0;//not handled yet
98 toaddr.sin_family=AF_INET;
99 memcpy(&toaddr.sin_addr,addr.addr,4);
100 toaddr.sin_port=*(unsigned short *)(addr.addr+4);
102 con_printf(CON_DEBUG, MSGHDR "sendtoca((%d) ",len);
103 //dumpraddr(addr.addr);
107 return sendto(mysock,(char*)buf,len,0,(struct sockaddr *)&toaddr,sizeof(toaddr));
111 int arch_ip_get_my_addr(u_short myport){
113 if (gethostname(buf,sizeof(buf))) FAIL("Error getting my hostname");
116 if (ip.dns(buf,myport)) FAIL("Error querying my own hostname \"%s\"",buf);
120 int arch_ip_open_socket(int port){
121 struct sockaddr_in sin;
123 WORD wVersionRequested;
126 wVersionRequested = MAKEWORD(2, 0);
127 if (WSAStartup( wVersionRequested, &wsaData))
132 if ( LOBYTE( wsaData.wVersion ) != 2 ||
133 HIBYTE( wsaData.wVersion ) != 0 ) {
134 /* We couldn't find a usable WinSock DLL. */
140 if ((mysock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
142 FAIL("socket() creation failed on port %d: %m",port);
144 if (setsockopt(mysock,SOL_SOCKET,SO_BROADCAST,(char*)&val_one,sizeof(val_one))) {
145 if (close(mysock)) msg("close() failed during error recovery: %m");
147 FAIL("setsockopt(SO_BROADCAST) failed: %m");
149 sin.sin_family=AF_INET;
150 sin.sin_addr.s_addr=htonl(INADDR_ANY);
151 sin.sin_port=htons(port);
153 if (bind(mysock,(struct sockaddr *)&sin,sizeof(sin))) {
154 if (close(mysock)) msg("close() failed during error recovery: %m");
156 FAIL("bind() to UDP port %d failed: %m",port);
161 void arch_ip_close_socket(void) {
163 msg("close w/o open");
166 if (closesocket(mysock))
167 msg("close() failed on CloseSocket D1X ip socket %m");
171 int arch_ip_recvfrom(char*outbuf,int outbufsize,struct ipx_recv_data *rd){
172 struct sockaddr_in fromaddr;
173 int fromaddrsize=sizeof(fromaddr);
175 static ip_addr ip_fromaddr;
176 ip_addr *vptr=&ip_fromaddr;
178 if ((size=recvfrom(mysock,outbuf,outbufsize,0,(struct sockaddr *)&fromaddr,&fromaddrsize))<0)
181 if (fromaddr.sin_family!=AF_INET) return -1;
184 con_printf(CON_DEBUG, MSGHDR "recvfrom((%d-2=%d),",size,size-2);
186 con_printf(CON_DEBUG(").\n");
189 ip_fromaddr.set(4,(u_char*)&fromaddr.sin_addr,fromaddr.sin_port);
191 memcpy(rd->src_node,&vptr,sizeof(ip_addr*));
194 int arch_ip_PacketReady(void) {
195 return ipx_general_PacketReady(mysock);