extern int trans_net(unsigned int clientaddr, unsigned int *addr, unsigned short *port) { t_elem const *curr; t_trans *entry; char temp1[32]; char temp2[32]; char temp3[32]; char temp4[32]; #ifdef DEBUG_TRANS eventlog(eventlog_level_debug, __FUNCTION__, "checking %s for client %s ...", addr_num_to_addr_str(*addr, *port), addr_num_to_ip_str(clientaddr)); #endif if (trans_head) { LIST_TRAVERSE_CONST(trans_head, curr) { if (!(entry = (t_trans*)elem_get_data(curr))) { eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); continue; } #ifdef DEBUG_TRANS eventlog(eventlog_level_debug, __FUNCTION__, "against entry -> %s output %s network %s", addr_get_addr_str(entry->input, temp1, sizeof(temp1)), addr_get_addr_str(entry->output, temp2, sizeof(temp2)), netaddr_get_addr_str(entry->network, temp3, sizeof(temp3))); #endif if (addr_get_ip(entry->input) != *addr || addr_get_port(entry->input) != *port) { #ifdef DEBUG_TRANS eventlog(eventlog_level_debug, __FUNCTION__, "entry does match input address"); #endif continue; } if (netaddr_contains_addr_num(entry->network, clientaddr) == 0) { #ifdef DEBUG_TRANS eventlog(eventlog_level_debug, __FUNCTION__, "client is not in the correct network"); #endif continue; } #ifdef DEBUG_TRANS eventlog(eventlog_level_debug, __FUNCTION__, "%s translated to %s", addr_num_to_addr_str(*addr, *port), addr_get_addr_str(entry->output, temp4, sizeof(temp4))); #endif *addr = addr_get_ip(entry->output); *port = addr_get_port(entry->output); return 1; /* match found in list */ } } #ifdef DEBUG_TRANS eventlog(eventlog_level_debug, __FUNCTION__, "no match found for %s (not translated)", addr_num_to_addr_str(*addr, *port)); #endif return 0; /* no match found in list */ }
extern int tracker_set_servers(char const * servers) { t_addr const * addr; t_elem const * curr; char temp[32]; if (track_servers && addrlist_destroy(track_servers)<0) eventlog(eventlog_level_error,__FUNCTION__,"unable to destroy tracker list"); if (!servers) { track_servers = NULL; return 0; } if (!(track_servers = addrlist_create(servers,INADDR_LOOPBACK,BNETD_TRACK_PORT))) { eventlog(eventlog_level_error,__FUNCTION__,"could not create tracking server list"); return -1; } LIST_TRAVERSE_CONST(track_servers,curr) { addr = elem_get_data(curr); if (!addr_get_addr_str(addr,temp,sizeof(temp))) strcpy(temp,"x.x.x.x:x"); eventlog(eventlog_level_info,__FUNCTION__,"tracking packets will be sent to %s",temp); }
extern int tracker_send_report(t_addrlist const * laddrs) { t_addr const * addrl; t_elem const * currl; t_addr const * addrt; t_elem const * currt; t_trackpacket packet; struct utsname utsbuf; struct sockaddr_in tempaddr; t_laddr_info * laddr_info; char tempa[64]; char tempb[64]; if (addrlist_get_length(track_servers)>0) { std::memset(&packet,0,sizeof(packet)); bn_short_nset(&packet.packet_version,(unsigned short)TRACK_VERSION); /* packet.port is set below */ bn_int_nset(&packet.flags, 0); std::strncpy((char *)packet.server_location, prefs_get_location(), sizeof(packet.server_location)); bn_byte_set(&packet.server_location[sizeof(packet.server_location)-1],'\0'); std::strncpy((char *)packet.software, PVPGN_SOFTWARE, sizeof(packet.software)); bn_byte_set(&packet.software[sizeof(packet.software)-1],'\0'); std::strncpy((char *)packet.version, PVPGN_VERSION, sizeof(packet.version)); bn_byte_set(&packet.version[sizeof(packet.version)-1],'\0'); std::strncpy((char *)packet.server_desc, prefs_get_description(), sizeof(packet.server_desc)); bn_byte_set(&packet.server_desc[sizeof(packet.server_desc)-1],'\0'); std::strncpy((char *)packet.server_url, prefs_get_url(), sizeof(packet.server_url)); bn_byte_set(&packet.server_url[sizeof(packet.server_url)-1],'\0'); std::strncpy((char *)packet.contact_name, prefs_get_contact_name(), sizeof(packet.contact_name)); bn_byte_set(&packet.contact_name[sizeof(packet.contact_name)-1],'\0'); std::strncpy((char *)packet.contact_email, prefs_get_contact_email(), sizeof(packet.contact_email)); bn_byte_set(&packet.contact_email[sizeof(packet.contact_email)-1],'\0'); bn_int_nset(&packet.users,connlist_login_get_length()); bn_int_nset(&packet.channels,channellist_get_length()); bn_int_nset(&packet.games,gamelist_get_length()); bn_int_nset(&packet.uptime,server_get_uptime()); bn_int_nset(&packet.total_logins,connlist_total_logins()); bn_int_nset(&packet.total_games,gamelist_total_games()); if (uname(&utsbuf)<0) { eventlog(eventlog_level_warn,__FUNCTION__,"could not get platform info (uname: %s)",pstrerror(errno)); std::strncpy((char *)packet.platform,"",sizeof(packet.platform)); } else { std::strncpy((char *)packet.platform, utsbuf.sysname, sizeof(packet.platform)); bn_byte_set(&packet.platform[sizeof(packet.platform)-1],'\0'); } LIST_TRAVERSE_CONST(laddrs,currl) { addrl = (t_addr*)elem_get_data(currl); if (!(laddr_info = (t_laddr_info*)addr_get_data(addrl).p)) { eventlog(eventlog_level_error,__FUNCTION__,"address data is NULL"); continue; } if (laddr_info->type!=laddr_type_bnet) continue; /* don't report IRC, telnet, and other non-game ports */ bn_short_nset(&packet.port,addr_get_port(addrl)); LIST_TRAVERSE_CONST(track_servers,currt) { addrt = (t_addr*)elem_get_data(currt); std::memset(&tempaddr,0,sizeof(tempaddr)); tempaddr.sin_family = PSOCK_AF_INET; tempaddr.sin_port = htons(addr_get_port(addrt)); tempaddr.sin_addr.s_addr = htonl(addr_get_ip(addrt)); if (!addr_get_addr_str(addrl,tempa,sizeof(tempa))) std::strcpy(tempa,"x.x.x.x:x"); if (!addr_get_addr_str(addrt,tempb,sizeof(tempb))) std::strcpy(tempa,"x.x.x.x:x"); /* eventlog(eventlog_level_debug,__FUNCTION__,"sending tracking info from %s to %s",tempa,tempb); */ if (psock_sendto(laddr_info->usocket,&packet,sizeof(packet),0,(struct sockaddr *)&tempaddr,(psock_t_socklen)sizeof(tempaddr))<0) eventlog(eventlog_level_warn,__FUNCTION__,"could not send tracking information from %s to %s (psock_sendto: %s)",tempa,tempb,pstrerror(errno)); }
extern void gametrans_net(unsigned int vaddr, unsigned short vport, unsigned int laddr, unsigned short lport, unsigned int * addr, unsigned short * port) { const t_elem *curr; t_gametrans *entry; #ifdef DEBUGGAMETRANS char temp1[32]; char temp2[32]; char temp3[32]; char temp4[32]; #endif #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","checking client %s (viewer on %s connected to %s)...", addr_num_to_addr_str(*addr,*port), addr_num_to_addr_str(vaddr,vport), addr_num_to_addr_str(laddr,lport)); #else (void)vport; #endif if (gametrans_head) { LIST_TRAVERSE_CONST(gametrans_head,curr) { if (!(entry = elem_get_data(curr))) { eventlog(eventlog_level_error,"gametrans_net","found NULL entry in list"); continue; } #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","against entry viewerint=%s client=%s output=%s viewerex=%s", addr_get_addr_str(entry->viewer,temp1,sizeof(temp1)), addr_get_addr_str(entry->client,temp2,sizeof(temp2)), addr_get_addr_str(entry->output,temp3,sizeof(temp3)), netaddr_get_addr_str(entry->exclude,temp4,sizeof(temp4))); #endif if (addr_get_ip(entry->viewer)!=0 && addr_get_ip(entry->viewer)!=laddr) { #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","viewer is not on right interface IP"); #endif continue; } if (addr_get_port(entry->viewer)!=0 && addr_get_port(entry->viewer)!=lport) { #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","view is not on right interface port"); #endif continue; } if (addr_get_ip(entry->client)!=0 && addr_get_ip(entry->client)!=*addr) { #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","client is not on the right IP"); #endif continue; } if (addr_get_port(entry->client)!=0 && addr_get_port(entry->client)!=*port) { #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","client is not on the right port"); #endif continue; } if (netaddr_contains_addr_num(entry->exclude,vaddr)==1) { #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","viewer is in the excluded network"); #endif continue; } *addr = addr_get_ip(entry->output); *port = addr_get_port(entry->output); #ifdef DEBUGGAMETRANS eventlog(eventlog_level_debug,"gametrans_net","did translation"); #endif return; } } }
static int sd_udpinput(t_addr *const curr_laddr, const t_laddr_info *laddr_info, int ssocket, int usocket) { int err; psock_t_socklen errlen; t_packet * upacket; if (laddr_info == NULL) { (void)ssocket; } err = 0; errlen = sizeof(err); if (psock_getsockopt(usocket,PSOCK_SOL_SOCKET,PSOCK_SO_ERROR,&err,&errlen)<0) { eventlog(eventlog_level_error,"sd_udpinput","[%d] unable to read socket error (psock_getsockopt: %s)",usocket,strerror(psock_errno())); return -1; } if (errlen && err) /* if it was an error, there is no packet to read */ { eventlog(eventlog_level_error,"sd_udpinput","[%d] async UDP socket error notification (psock_getsockopt: %s)",usocket,strerror(err)); return -1; } if (!(upacket = packet_create(packet_class_udp))) { eventlog(eventlog_level_error,"sd_udpinput","could not allocate raw packet for input"); return -1; } { struct sockaddr_in fromaddr; psock_t_socklen fromlen; int len; fromlen = sizeof(fromaddr); if ((len = psock_recvfrom(usocket,packet_get_raw_data_build(upacket,0),MAX_PACKET_SIZE,0,(struct sockaddr *)&fromaddr,&fromlen))<0) { if ( #ifdef PSOCK_EINTR psock_errno()!=PSOCK_EINTR && #endif #ifdef PSOCK_EAGAIN psock_errno()!=PSOCK_EAGAIN && #endif #ifdef PSOCK_EWOULDBLOCK psock_errno()!=PSOCK_EWOULDBLOCK && #endif 1) eventlog(eventlog_level_error,"sd_udpinput","could not recv UDP datagram (psock_recvfrom: %s)",strerror(psock_errno())); packet_del_ref(upacket); return -1; } if (fromaddr.sin_family!=PSOCK_AF_INET) { eventlog(eventlog_level_error,"sd_udpinput","got UDP datagram with bad address family %d",(int)fromaddr.sin_family); packet_del_ref(upacket); return -1; } packet_set_size(upacket,len); if (hexstrm) { char tempa[32]; if (!addr_get_addr_str(curr_laddr,tempa,sizeof(tempa))) strcpy(tempa,"x.x.x.x:x"); fprintf(hexstrm,"%d: recv class=%s[0x%02x] type=%s[0x%04x] from=%s to=%s length=%u\n", usocket, packet_get_class_str(upacket),(unsigned int)packet_get_class(upacket), packet_get_type_str(upacket,packet_dir_from_client),packet_get_type(upacket), addr_num_to_addr_str(ntohl(fromaddr.sin_addr.s_addr),ntohs(fromaddr.sin_port)), tempa, packet_get_size(upacket)); hexdump(hexstrm,packet_get_raw_data(upacket,0),packet_get_size(upacket)); } handle_udp_packet(usocket,ntohl(fromaddr.sin_addr.s_addr),ntohs(fromaddr.sin_port),upacket); packet_del_ref(upacket); } return 0; }
static int sd_accept(t_addr const * curr_laddr, t_laddr_info const * laddr_info, int ssocket, int usocket) { char tempa[32]; int csocket; struct sockaddr_in caddr; psock_t_socklen caddr_len; unsigned int raddr; unsigned short rport; if (!addr_get_addr_str(curr_laddr,tempa,sizeof(tempa))) strcpy(tempa,"x.x.x.x:x"); /* accept the connection */ memset(&caddr,0,sizeof(caddr)); /* not sure if this is needed... modern systems are ok anyway */ caddr_len = sizeof(caddr); if ((csocket = psock_accept(ssocket,(struct sockaddr *)&caddr,&caddr_len))<0) { /* BSD, POSIX error for aborted connections, SYSV often uses EAGAIN or EPROTO */ if ( #ifdef PSOCK_EWOULDBLOCK psock_errno()==PSOCK_EWOULDBLOCK || #endif #ifdef PSOCK_ECONNABORTED psock_errno()==PSOCK_ECONNABORTED || #endif #ifdef PSOCK_EPROTO psock_errno()==PSOCK_EPROTO || #endif 0) eventlog(eventlog_level_error,"sd_accept","client aborted connection on %s (psock_accept: %s)",tempa,strerror(psock_errno())); else /* EAGAIN can mean out of resources _or_ connection aborted :( */ if ( #ifdef PSOCK_EINTR psock_errno()!=PSOCK_EINTR && #endif 1) eventlog(eventlog_level_error,"sd_accept","could not accept new connection on %s (psock_accept: %s)",tempa,strerror(psock_errno())); return -1; } #ifdef HAVE_POLL if (csocket>=BNETD_MAX_SOCKETS) /* This check is a bit too strict (csocket is probably * greater than the number of connections) but this makes * life easier later. */ { eventlog(eventlog_level_error,"sd_accept","csocket is beyond range allowed by BNETD_MAX_SOCKETS for poll() (%d>=%d)",csocket,BNETD_MAX_SOCKETS); psock_close(csocket); return -1; } #else # ifdef FD_SETSIZE if (csocket>=FD_SETSIZE) /* fd_set size is determined at compile time */ { eventlog(eventlog_level_error,"sd_accept","csocket is beyond range allowed by FD_SETSIZE for select() (%d>=%d)",csocket,FD_SETSIZE); psock_close(csocket); return -1; } # endif #endif if (ipbanlist_check(inet_ntoa(caddr.sin_addr))!=0) { eventlog(eventlog_level_info,"sd_accept","[%d] connection from banned address %s denied (closing connection)",csocket,inet_ntoa(caddr.sin_addr)); psock_close(csocket); return -1; } eventlog(eventlog_level_info,"sd_accept","[%d] accepted connection from %s on %s",csocket,addr_num_to_addr_str(ntohl(caddr.sin_addr.s_addr),ntohs(caddr.sin_port)),tempa); if (prefs_get_use_keepalive()) { int val=1; if (psock_setsockopt(csocket,PSOCK_SOL_SOCKET,PSOCK_SO_KEEPALIVE,&val,(psock_t_socklen)sizeof(val))<0) eventlog(eventlog_level_error,"sd_accept","[%d] could not set socket option SO_KEEPALIVE (psock_setsockopt: %s)",csocket,strerror(psock_errno())); /* not a fatal error */ } { struct sockaddr_in rsaddr; psock_t_socklen rlen; memset(&rsaddr,0,sizeof(rsaddr)); /* not sure if this is needed... modern systems are ok anyway */ rlen = sizeof(rsaddr); if (psock_getsockname(csocket,(struct sockaddr *)&rsaddr,&rlen)<0) { eventlog(eventlog_level_error,"sd_accept","[%d] unable to determine real local port (psock_getsockname: %s)",csocket,strerror(psock_errno())); /* not a fatal error */ raddr = addr_get_ip(curr_laddr); rport = addr_get_port(curr_laddr); } else { if (rsaddr.sin_family!=PSOCK_AF_INET) { eventlog(eventlog_level_error,"sd_accept","local address returned with bad address family %d",(int)rsaddr.sin_family); /* not a fatal error */ raddr = addr_get_ip(curr_laddr); rport = addr_get_port(curr_laddr); } else { raddr = ntohl(rsaddr.sin_addr.s_addr); rport = ntohs(rsaddr.sin_port); } } } if (psock_ctl(csocket,PSOCK_NONBLOCK)<0) { eventlog(eventlog_level_error,"sd_accept","[%d] could not set TCP socket to non-blocking mode (closing connection) (psock_ctl: %s)",csocket,strerror(psock_errno())); psock_close(csocket); return -1; } { t_connection * c; if (!(c = conn_create(csocket,usocket,raddr,rport,addr_get_ip(curr_laddr),addr_get_port(curr_laddr),ntohl(caddr.sin_addr.s_addr),ntohs(caddr.sin_port)))) { eventlog(eventlog_level_error,"sd_accept","[%d] unable to create new connection (closing connection)",csocket); psock_close(csocket); return -1; } eventlog(eventlog_level_debug,"sd_accept","[%d] client connected to a %s listening address",csocket,laddr_type_get_str(laddr_info->type)); switch (laddr_info->type) { case laddr_type_irc: conn_set_class(c,conn_class_irc); conn_set_state(c,conn_state_connected); break; case laddr_type_telnet: conn_set_class(c,conn_class_telnet); conn_set_state(c,conn_state_connected); break; case laddr_type_bnet: default: /* We have to wait for an initial "magic" byte on bnet connections to * tell us exactly what connection class we are dealing with. */ break; } } return 0; }