Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | #include <ucommon/ucommon.h> |
| 19 | |
| 20 | using namespace UCOMMON_NAMESPACE; |
| 21 | |
| 22 | static shell::flagopt helpflag('h',"--help", _TEXT("display this list")); |
| 23 | static shell::flagopt althelp('?', NULL, NULL); |
| 24 | static shell::flagopt udp('u', "--udp", _TEXT("lookup udp service")); |
| 25 | |
| 26 | PROGRAM_MAIN(argc, argv) |
| 27 | { |
| 28 | unsigned type = SOCK_STREAM; |
| 29 | unsigned port; |
| 30 | char buffer[256]; |
| 31 | |
| 32 | shell::bind("sockaddr"); |
| 33 | shell args(argc, argv); |
| 34 | |
| 35 | if(is(helpflag) || is(althelp)) { |
| 36 | printf("%s\n", _TEXT("Usage: sockaddr [options] arguments...")); |
| 37 | printf("%s\n\n", _TEXT("Echo command line arguments")); |
| 38 | printf("%s\n", _TEXT("Options:")); |
| 39 | shell::help(); |
| 40 | printf("\n%s\n", _TEXT("Report bugs to dyfet@gnu.org")); |
| 41 | PROGRAM_EXIT(0); |
| 42 | } |
| 43 | |
| 44 | const char *host = args[0]; |
| 45 | const char *service = args[1]; |
| 46 | |
| 47 | if(!host || !service) |
| 48 | shell::errexit(1, "use: sockaddr [options] host service\n"); |
| 49 | |
| 50 | if(udp) |
| 51 | type = SOCK_DGRAM; |
| 52 | |
| 53 | Socket::address addr(host, service, type); |
| 54 | linked_pointer<struct sockaddr> ap = addr; |
| 55 | |
| 56 | if(!is(ap)) |
| 57 | shell::errexit(2, "*** sockaddr: %s/%s: %s\n", |
| 58 | host, service, _TEXT("could not find")); |
| 59 | |
| 60 | while(is(ap)) { |
| 61 | port = 0; |
| 62 | switch(ap->sa_family) { |
| 63 | case AF_INET: |
| 64 | port = ntohs(ap.in()->sin_port); |
| 65 | break; |
| 66 | #ifdef AF_INET6 |
| 67 | case AF_INET6: |
| 68 | port = ntohs(ap.in6()->sin6_port); |
| 69 | break; |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | if(port) { |
| 74 | Socket::query(*ap, buffer, sizeof(buffer)); |
| 75 | printf("%s/%u\n", buffer, port); |
| 76 | } |
| 77 | |
| 78 | ap.next(); |
| 79 | } |
| 80 | |
| 81 | PROGRAM_EXIT(0); |
| 82 | } |
| 83 | |