//////////////////////////////////////////////////////////////// Function name : ConvertAddress Return type : static in_addr Argument : char* pNetAddress Description : Converts from ip address or hostname to socket address data. NOTE: Uses static structures so only valid until next call. (This is gethostbyname's approach so we have just extended it). /////////////////////////////
| 49 | // |
| 50 | ///////////////////////////////////////////////////////////////////// |
| 51 | static in_addr* ConvertAddress(char const* pNetAddress) |
| 52 | { |
| 53 | static in_addr address ; |
| 54 | |
| 55 | assert(pNetAddress) ; |
| 56 | |
| 57 | // Try it as aaa.bbb.ccc.ddd first |
| 58 | address.s_addr = inet_addr(pNetAddress) ; |
| 59 | |
| 60 | // Check if this method worked |
| 61 | if (address.s_addr != INADDR_NONE) |
| 62 | { |
| 63 | return &address ; |
| 64 | } |
| 65 | |
| 66 | // Then try it as a hostname |
| 67 | hostent* pHost = gethostbyname(pNetAddress) ; |
| 68 | |
| 69 | // Check if this method worked |
| 70 | if (pHost != NULL) |
| 71 | { |
| 72 | return (in_addr*) *pHost->h_addr_list ; |
| 73 | } |
| 74 | |
| 75 | return NULL ; |
| 76 | } |
| 77 | |
| 78 | const char* kLocalHost = "127.0.0.1" ; // Special IP address meaning "this machine" |
| 79 |