/** * iax ping tool * Cristian Draghici, cristian.draghici@gmail.com * * Tool to ping an IAX target (checks network connectivity). * hacked from the perl ping tool availble on http://www.voip-info.org/wiki-IAX * 12 October 2005 * * Copyright (c) 2005, Modulo Consulting S.R.L. All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * - Neither the name of the MODULO Consulting S.R.L. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include int iaxping(char * target_host) { struct hostent *source_hostent, *target_hostent; unsigned long source_hostaddr, target_hostaddr; struct sockaddr_in source_addr, target_addr; struct sockaddr from_address; socklen_t addr_length = sizeof(from_address); memset(&from_address, 0, sizeof(from_address)); ssize_t retval; int result; unsigned char pingPacket[12] = { 0x80, 0x00, //iax packet type full 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //timestamp 0x00, 0x00, //inbound_seq 0x06, //IAX control frame 0x1E }; //POKE char receiveBuffer[1024]; int mySocket = socket(AF_INET, SOCK_DGRAM, 17); char hostname[1024]; if(gethostname(hostname, 1023)) { fprintf(stderr, "Error: gethostname returned error: %s\n", strerror(errno)); return 1; } if((source_hostent = gethostbyname(hostname)) == NULL) { fprintf(stderr, "Can't resolve address for local host\n"); return -1; } source_hostaddr = *((unsigned long *)source_hostent->h_addr); source_addr.sin_family = AF_INET; source_addr.sin_addr.s_addr = source_hostaddr; source_addr.sin_port = htons(4569); result = bind(mySocket, (struct sockaddr *)&source_addr, sizeof(source_addr)); if(result) { fprintf(stderr, "Failed bind 4569[%s]. Unless your firewall permits UDP access to dynamic ports iaxping will not work\n", strerror(errno)); } if((target_hostent = gethostbyname(target_host)) == NULL) { fprintf(stderr, "Can't resolve address for %s\n", target_host); return -1; } target_hostaddr = *((unsigned long *)target_hostent->h_addr); target_addr.sin_family = AF_INET; target_addr.sin_addr.s_addr = target_hostaddr; target_addr.sin_port = htons(4569); retval = sendto(mySocket, pingPacket, 12, 0, (struct sockaddr *)&target_addr, sizeof(target_addr)); if(retval != 12) { fprintf(stderr, "Did not manage to send 12 bytes of data.\n"); } while(1) { retval = recvfrom(mySocket, &receiveBuffer, 1024, 0, &from_address, &addr_length); if(retval != -1) { fprintf(stdout, "Reply from: %s\n", inet_ntoa(((struct in_addr)((struct sockaddr_in *)&from_address)->sin_addr))); } else { fprintf(stderr, "Error while trying to receive \n"); } } } int main(int argc, char * argv[]) { if(argc < 2) { printf("%s\n" , "IAX Ping Tool\n\nUsage: iaxping target\n"); return 0; } printf("Sending IAX ping to %s\n", argv[1]); return iaxping(argv[1]); }