static int SetupInterfaceList(mDNS *const m) { mDNSBool foundav4 = mDNSfalse; int err = 0; struct ifi_info *intfList = get_ifi_info(AF_INET, mDNStrue); struct ifi_info *firstLoopback = NULL; assert(m != NULL); debugf("SetupInterfaceList"); if (intfList == NULL) err = ENOENT; #ifdef mDNSIPv6Support if (err == 0) /* Link the IPv6 list to the end of the IPv4 list */ { struct ifi_info **p = &intfList; while (*p) p = &(*p)->ifi_next; *p = get_ifi_info(AF_INET6, mDNStrue); } #endif if (err == 0) { struct ifi_info *i = intfList; while (i) { if ( ((i->ifi_addr->sa_family == AF_INET) #ifdef mDNSIPv6Support || (i->ifi_addr->sa_family == AF_INET6) #endif ) && (i->ifi_flags & IFF_UP) && !(i->ifi_flags & IFF_POINTOPOINT) ) { if (i->ifi_flags & IFF_LOOPBACK) { if (firstLoopback == NULL) firstLoopback = i; } else { if (SetupOneInterface(m, i->ifi_addr, i->ifi_name) == 0) if (i->ifi_addr->sa_family == AF_INET) foundav4 = mDNStrue; } } i = i->ifi_next; } // If we found no normal interfaces but we did find a loopback interface, register the // loopback interface. This allows self-discovery if no interfaces are configured. // Temporary workaround: Multicast loopback on IPv6 interfaces appears not to work. // In the interim, we skip loopback interface only if we found at least one v4 interface to use // if ( (m->HostInterfaces == NULL) && (firstLoopback != NULL) ) if ( !foundav4 && firstLoopback ) (void) SetupOneInterface(m, firstLoopback->ifi_addr, firstLoopback->ifi_name); } // Clean up. if (intfList != NULL) free_ifi_info(intfList); return err; }
/* * Init multihomed socket. Will bind a socket for each interface + each * broadcast address + the wildcard addres + each multicast address in * multicastGroups. */ CWBool CWNetworkInitSocketServerMultiHomed(CWMultiHomedSocket * sockPtr, int port, char **multicastGroups, int multicastGroupsCount) { struct ifi_info *ifi, *ifihead; CWNetworkLev4Address wildaddr; int yes = 1; CWSocket sock; CWMultiHomedInterface *p; CWList interfaceList = CW_LIST_INIT; CWListElement *el = NULL; int i; if (sockPtr == NULL) return CWErrorRaise(CW_ERROR_WRONG_ARG, NULL); sockPtr->count = 0; /* * note: if get_ifi_info is called with AF_INET6 on an host that doesn't * support IPv6, it'll simply act like if it was called with AF_INET. * Consider aliases as different interfaces (last arg of get_ifi_info is 1). * Why? Just to increase the funny side of the thing. */ #ifdef CW_DEBUGGING /* for each network interface... */ for (ifihead = ifi = get_ifi_info((gNetworkPreferredFamily == CW_IPv6) ? AF_INET6 : AF_INET, 1); ifi != NULL; ifi = ifi->ifi_next) { #else /* for each network interface... */ for (ifihead = ifi = get_ifi_info((gNetworkPreferredFamily == CW_IPv6) ? AF_INET6 : AF_INET, 0); ifi != NULL; ifi = ifi->ifi_next) { #endif /* bind a unicast address */ if ((sock = socket(ifi->ifi_addr->sa_family, SOCK_DGRAM, 0)) < 0) { free_ifi_info(ifihead); CWNetworkRaiseSystemError(CW_ERROR_CREATING); } /* reuse address */ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); /* bind address */ sock_set_port_cw(ifi->ifi_addr, htons(port)); if (bind (sock, (struct sockaddr *)ifi->ifi_addr, CWNetworkGetAddressSize((CWNetworkLev4Address *) ifi->ifi_addr)) < 0) { close(sock); CWUseSockNtop(ifi->ifi_addr, CWDebugLog("failed %s", str); ); continue; /* CWNetworkRaiseSystemError(CW_ERROR_CREATING); */ } CWUseSockNtop(ifi->ifi_addr, CWLog("bound %s (%d, %s)", str, ifi->ifi_index, ifi->ifi_name); );
int main(int argc, char **argv) { int sockfd; struct ifi_info *ifi; unsigned char *ptr; struct arpreq arpreq; struct sockaddr_in *sin; sockfd = Socket(AF_INET, SOCK_DGRAM, 0); for (ifi = get_ifi_info(AF_INET, 0); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: ", (char *)Sock_ntop(ifi->ifi_addr, sizeof(struct sockaddr_in))); sin = (struct sockaddr_in *)&arpreq.arp_pa; memcpy(sin, ifi->ifi_addr, sizeof(struct sockaddr_in)); if (ioctl(sockfd, SIOCGARP, &arpreq) < 0) { err_ret("ioctl SIOCGARP"); continue; } ptr = &arpreq.arp_ha.sa_data[0]; printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); } exit(0); }
int main(int argc,char *argv[]) { int sockfd; const int on = 1; pid_t pid; struct sockaddr_in *sa,cliaddr,wildaddr; struct ifi_info *ifi,*ifihead; for(ifi = ifihead = get_ifi_info(AF_INET,1);ifi != NULL; ifi=ifi->ifi_next){ sa = (struct sockaddr_in*)ifi->ifi_addr; sa->sin_port = htons(SERV_PORT); sa->sin_family = AF_INET; sockfd = Socket(AF_INET,SOCK_DGRAM,0); setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); Bind(sockfd,(SA*)sa,sizeof(*sa)); if((pid = fork()) == 0){ mydg_echo(sockfd,*cliaddr,sizeof(cliaddr),(SA*)sa); exit(0); } if(ifi->ifi_flags & IFF_BROADCAST){ sockfd = Socket(AF_INET,SOCK_DAGRAM,0); setsockopt(sockfd,SOL_SOCKET,SOCK_DGRAM,&on,sizeof(on)); sa = (struct sockaddr_in *)ifi->ifi_brdaddr; sa ->sin_port = htons(SERV_PORT); sa->sin_family = AF_INET; if(Bind(sockfd,sa,sizeof(*sa)) < 0){ if(errno == EINUSEADDR){ err_quit("EINUSERADDR:for %s\n",Sock_ntop(sa,sizeof(*sa)); }else{ err_quit("error bind for %s\n",Sock_ntop(sa,sizeof(*sa))); } }
struct ifi_info * Get_ifi_info(int family, int doaliases) { struct ifi_info *ifi; if ( (ifi = get_ifi_info(family, doaliases)) == NULL) err_quit("get_ifi_info error"); return(ifi); }
u_char * find_nodeID(void) { int i; struct sockaddr *sa; char *ptr; u_char *myNodeIDtmp = 0; struct ifi_info *ifi, *ifihead; /* * Get interface info for all inet4 interfaces * and don't return aliases. */ for (ifihead = ifi = get_ifi_info(AF_INET, 0); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: <", ifi->ifi_name); if (ifi->ifi_flags & IFF_UP) printf("UP "); if (ifi->ifi_flags & IFF_BROADCAST) printf("BCAST "); if (ifi->ifi_flags & IFF_MULTICAST) printf("MCAST "); if (ifi->ifi_flags & IFF_LOOPBACK) printf("LOOP "); if (ifi->ifi_flags & IFF_POINTOPOINT) printf("P2P "); printf(">\n"); if ( (i = ifi->ifi_hlen) > 0) { ptr = ifi->ifi_haddr; do { printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++); } while (--i > 0); } /* * We update myNodeIDtmp in block separate from above * since the above is just a debug clause and may be * compiled out eventually. -lkw */ if ( ifi->ifi_hlen > 0) { myNodeIDtmp = max_haddr(myNodeIDtmp, ifi->ifi_haddr); } if ( (sa = ifi->ifi_addr) != NULL) printf(" IP addr: %s\n", sock_ntop(sa, sa->sa_len)); if ( (sa = ifi->ifi_brdaddr) != NULL) printf(" broadcast addr: %s\n", sock_ntop(sa, sa->sa_len)); if ( (sa = ifi->ifi_dstaddr) != NULL) printf(" destination addr: %s\n", sock_ntop(sa, sa->sa_len)); } fprintf(stderr, "My node id:"); print_nodeID(myNodeIDtmp); return myNodeIDtmp; }
void KSGNetUtil::GetLocalIP(std::list<std::string>&ip_list) { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; u_char *ptr; char * ip_addr; int i, family, doaliases; family = AF_INET; doaliases = 1; for(ifi = ifihead = get_ifi_info(family,doaliases); ifi != NULL; ifi = ifi->ifi_next) { if((sa = ifi->ifi_addr) != NULL) { ip_addr = strdup(sock_ntop(sa,sizeof(*sa))); ip_list.push_back(std::string(ip_addr)); } } free_ifi_info(ifihead); }
int KSGNetUtil::GetMacByIP(const std::string ip, std::string &mac) { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; u_char *ptr; char mac_str[20] = ""; std::string ipaddr; int i,j,ret, family, doaliases; family = AF_INET; doaliases = 1; ret = -1; for(ifi = ifihead = get_ifi_info(family,doaliases); ifi != NULL; ifi = ifi->ifi_next) { if((sa = ifi->ifi_addr) != NULL) { ipaddr = sock_ntop(sa,sizeof(*sa)); if(ipaddr == ip) { if(( i = ifi->ifi_hlen) > 0) { ptr = ifi->ifi_haddr; j = 0; do{ j += sprintf(mac_str+j,"%s%02X",(i == ifi->ifi_hlen) ? " " : "-", *ptr++); }while(--i>0); mac = mac_str; } ret = 0; break; } } } free_ifi_info(ifihead); return ret; }
int main(int argc, const char *argv[]) { struct ifi_info* ifi,* ifihead; struct sockaddr *sa; u_char* ptr; int i, family, doaliases; if(argc != 3) { err_quit("usage : prifinfo <inet4|inet6> <doaliases>"); } if(strcmp(argv[1], "inet4") == 0) { family = AF_INET; } else if(strcmp(argv[1], "inet6") == 0) { family = AF_INET6; } else { err_quit("invalid <address family>"); } doaliases = atoi(argv[2]); for(ifihead = ifi = get_ifi_info(family, doaliases); ifi != NULL; ifi=ifi->ifi_next) { printf("%s:\n", ifi->ifi_name); if(ifi->ifi_index != 0) { printf("(%d) ", ifi->ifi_index); } printf("<"); if(ifi->ifi_flags & IFF_UP) printf("UP "); if(ifi->ifi_flags & IFF_BROADCAST) printf("BCAST "); if(ifi->ifi_flags & IFF_MULTICAST) printf("MCAST "); if(ifi->ifi_flags & IFF_LOOPBACK) printf("LOOP "); if(ifi->ifi_flags & IFF_POINTOPOINT) printf("P2P "); printf(">\n"); if(( i = ifi->ifi_hlen ) > 0) { ptr = ifi->ifi_haddr; do{ printf("%s%x", (i == ifi->ifi_hlen)? " ":":", *ptr++); }while(--i > 0); printf("\n"); } if(ifi->ifi_mtu != 0) { printf("MTU: %d\n",ifi->ifi_mtu); } if((sa = ifi->ifi_addr) != NULL) { printf(" IP addr : %s\n", Sock_ntop_host(sa, sizeof(*sa))); } if((sa = ifi->ifi_brdaddr) != NULL) { printf(" broadcast addr: %s\n",Sock_ntop_host(sa, sizeof(*sa))); } if((sa = ifi->ifi_dstaddr) != NULL) { printf(" destination addr: %s\n", Sock_ntop_host(sa, sizeof(*sa))); } } free_ifi_info(ifihead); exit(0); return 0; }
const char *RemoteLink( const char *parms, bool server ) { unsigned short port; #ifndef __RDOS__ struct servent *sp; #endif #ifdef SERVER #if !defined( __RDOS__ ) trp_socklen length; char buff[128]; #endif _DBG_NET(("SERVER: Calling into RemoteLink\r\n")); #if defined(__NT__) || defined(__WINDOWS__) { WSADATA data; if( WSAStartup( 0x101, &data ) != 0 ) { return( TRP_ERR_unable_to_initialize_TCPIP ); } } #endif port = 0; #ifdef __RDOS__ while( isdigit( *parms ) ) { port = port * 10 + (*parms - '0'); ++parms; } if( port == 0 ) port = DEFAULT_PORT; wait_handle = RdosCreateWait( ); listen_handle = RdosCreateTcpListen( port, 1, SOCKET_BUFFER ); RdosAddWaitForTcpListen( wait_handle, listen_handle, (int)(&listen_handle) ); #else if( *parms == '\0' ) parms = "tcplink"; sp = getservbyname( parms, "tcp" ); if( sp != NULL ) { port = sp->s_port; } else { while( isdigit( *parms ) ) { port = port * 10 + (*parms - '0'); ++parms; } if( port == 0 ) port = DEFAULT_PORT; port = htons( port ); } control_socket = socket(AF_INET, SOCK_STREAM, 0); if( !IS_VALID_SOCKET( control_socket ) ) { return( TRP_ERR_unable_to_open_stream_socket ); } #ifdef GUISERVER if( *ServParms == '\0' ) { sprintf( ServParms, "%u", ntohs( port ) ); } #endif /* Name socket using wildcards */ socket_address.sin_family = AF_INET; socket_address.sin_addr.s_addr = INADDR_ANY; socket_address.sin_port = port; if( bind( control_socket, (LPSOCKADDR)&socket_address, sizeof( socket_address ) ) ) { return( TRP_ERR_unable_to_bind_stream_socket ); } /* Find out assigned port number and print it out */ length = sizeof( socket_address ); if( getsockname( control_socket, (LPSOCKADDR)&socket_address, &length ) ) { return( TRP_ERR_unable_to_get_socket_name ); } sprintf( buff, "%s%d", TRP_TCP_socket_number, ntohs( socket_address.sin_port ) ); ServMessage( buff ); _DBG_NET(("TCP: ")); _DBG_NET((buff)); _DBG_NET(("\r\n")); #ifdef LIST_INTERFACES // TODO: need rework to POSIX if_nameindex in <net/if.h> /* Find and print TCP/IP interface addresses, ignore aliases */ { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; ifihead = get_ifi_info( AF_INET, false ); for( ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next ) { /* Ignore loopback interfaces */ if( ifi->flags & IFI_LOOP ) continue; if( (sa = ifi->ifi_addr) != NULL ) { sprintf( buff, "%s%s", TRP_TCP_ip_address, inet_ntoa( ((struct sockaddr_in *)sa)->sin_addr ) ); ServMessage( buff ); } } free_ifi_info( ifihead ); } #endif #endif _DBG_NET(("Start accepting connections\r\n")); /* Start accepting connections */ #ifndef __RDOS__ listen( control_socket, 5 ); #endif #else #ifdef __RDOS__ // Todo: handle connect #else const char *sock; char buff[128]; char *p; #if defined(__NT__) || defined(__WINDOWS__) { WSADATA data; if( WSAStartup( 0x101, &data ) != 0 ) { return( TRP_ERR_unable_to_initialize_TCPIP ); } } #endif /* get port number out of name */ p = buff; for( sock = parms; *sock != '\0'; ++sock ) { if( *sock == ':' ) { ++sock; break; } *p++ = *sock; } *p = '\0'; if( sock[0] == '\0' ) { sp = getservbyname( "tcplink", "tcp" ); } else { sp = getservbyname( sock, "tcp" ); } if( sp != NULL ) { port = sp->s_port; } else { port = 0; while( isdigit( *sock ) ) { port = port * 10 + (*sock - '0'); ++sock; } if( *sock != '\0' ) { return( TRP_ERR_unable_to_parse_port_number ); } if( port == 0 ) port = DEFAULT_PORT; port = htons( port ); } parms = buff; /* Setup for socket connect using parms specified by command line. */ socket_address.sin_family = AF_INET; /* OS/2's TCP/IP gethostbyname doesn't handle numeric addresses */ socket_address.sin_addr.s_addr = inet_addr( parms ); if( socket_address.sin_addr.s_addr == (unsigned long)-1L ) { struct hostent *hp; hp = gethostbyname( parms ); if( hp != 0 ) { memcpy( &socket_address.sin_addr, hp->h_addr, hp->h_length ); } else { return( TRP_ERR_unknown_host ); } } socket_address.sin_port = port; #endif #endif server = server; return( NULL ); }
int main(int argc, char **argv) { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; u_char *ptr; int i, family, doaliases; if (argc != 3) err_quit("Usage: printinfo <inet4|inet6> <doaliases>"); if (strcmp(argv[1], "inet4") == 0) family = AF_INET; else if (strcmp(argv[1], "inet6") == 0) family = AF_INET6; else err_quit("invalid <address-family>"); doaliases = atoi(argv[2]); for (ifihead = ifi = get_ifi_info(family, doaliases); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: ", ifi->ifi_name); if (ifi->ifi_ivalue != 0) printf("(%d\n) ", ifi->ifi_ivalue); printf("flags: <"); if (ifi->ifi_flags & IFF_UP) printf("UP "); if (ifi->ifi_flags & IFF_BROADCAST) printf("BCAST "); if (ifi->ifi_flags & IFF_MULTICAST) printf("MCAST "); if (ifi->ifi_flags & IFF_LOOPBACK) printf("LOOP "); if (ifi->ifi_flags & IFF_POINTOPOINT) printf("P2P "); if (ifi->ifi_flags & IFF_RUNNING) printf("RUNNING "); printf(">\n"); if ((i = ifi->ifi_hlen) > 0) { ptr = ifi->ifi_haddr; do { printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++); } while (--i > 0); printf("\n"); } if (ifi->ifi_mtu != 0) printf(" MTU: %d\n", ifi->ifi_mtu); if ((sa = ifi->ifi_addr) != NULL) printf(" IP addr: %s\n", (char *)Sock_ntop_host(sa, sizeof(*sa))); if ((sa = ifi->ifi_brdaddr) != NULL) printf(" Broadcast addr: %s\n", (char *)Sock_ntop_host(sa, sizeof(*sa))); if ((sa = ifi->ifi_netmask) != NULL) printf(" Netmask: %s\n", (char *)Sock_ntop_host(sa, sizeof(*sa))); if ((sa = ifi->ifi_dstaddr) != NULL) printf(" Destination addr: %s\n", (char *)Sock_ntop_host(sa, sizeof(*sa))); } free_ifi_info(ifihead); exit(0); }