LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2009, 02:58 AM   #1
please
Member
 
Registered: Apr 2007
Posts: 195

Rep: Reputation: 30
Please Add New option


Hi ALL

Please add some option in following file as ping.
If add -c and -I option, It is OK.

I am hopping your help

Please










/*
* tcping.c
*
* Copyright (c) 2002-2008 Marc Kirchner <mail(at)marc(dash)kirchner(dot)de>
*
* tcping is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* tcping is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ms++. If not, see <http://www.gnu.org/licenses/>.
*
* tcping does a nonblocking connect to test if a port is reachable.
* Its exit codes are:
* -1 an error occured
* 0 port is open
* 1 port is closed
* 2 user timeout
*/

#define VERSION 1.3.5

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netdb.h>

void usage();

int main (int argc, char *argv[]) {

int sockfd;
struct sockaddr_in addr;
struct hostent *host;
int error = 0;
int ret;
socklen_t errlen;
struct timeval timeout;
fd_set fdrset, fdwset;
int verbose=1;
int c;
char *cptr;
long timeout_sec=0, timeout_usec=0;
int port=0;

if (argc < 3) {
usage(argv[0]);
}

while((c = getopt(argc, argv, "qt:u:")) != -1) {
switch(c) {
case 'q':
verbose = 0;
break;
case 't':
cptr = NULL;
timeout_sec = strtol(optarg, &cptr, 10);
if (cptr == optarg)
usage(argv[0]);
break;
case 'u':
cptr = NULL;
timeout_usec = strtol(optarg, &cptr, 10);
if (cptr == optarg)
usage(argv[0]);
break;
default:
usage(argv[0]);
break;
}
}

sockfd = socket (AF_INET, SOCK_STREAM, 0);

memset(&addr, 0, sizeof(addr));

if ((host = gethostbyname(argv[optind])) == NULL) {
if (verbose)
#ifdef HAVE_HSTRERROR
fprintf(stderr, "error: %s\n", hstrerror(h_errno));
#else
fprintf(stderr, "error: host not found");
#endif
exit(-1);
}

memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
addr.sin_family = host->h_addrtype; /* always AF_INET */
if (argv[optind+1]) {
cptr = NULL;
port = strtol(argv[optind+1], &cptr, 10);
if (cptr == argv[optind+1])
usage(argv[0]);
} else {
usage(argv[0]);
}
addr.sin_port = htons(port);

fcntl(sockfd, F_SETFL, O_NONBLOCK);
if ((ret = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr))) != 0) {
if (errno != EINPROGRESS) {
#ifdef HAVE_SOLARIS
/* solaris immediately returns ECONNREFUSED on local ports */
if (errno == ECONNREFUSED) {
if (verbose)
fprintf(stdout, "%s port %s closed.\n", argv[optind], argv[optind+1]);
close(sockfd);
return(1);
} else {
#endif
if (verbose)
fprintf(stderr, "error: %s port %s: %s\n", argv[optind], argv[optind+1], strerror(errno));
return (-1);
#ifdef HAVE_SOLARIS
}
#endif
}

FD_ZERO(&fdrset);
FD_SET(sockfd, &fdrset);
fdwset = fdrset;

timeout.tv_sec=timeout_sec + timeout_usec / 1000000;
timeout.tv_usec=timeout_usec % 1000000;

if ((ret = select(sockfd+1, &fdrset, &fdwset, NULL, timeout.tv_sec+timeout.tv_usec > 0 ? &timeout : NULL)) == 0) {
/* timeout */
close(sockfd);
if (verbose)
fprintf(stdout, "%s port %s user timeout.\n", argv[optind], argv[optind+1]);
return(2);
}
if (FD_ISSET(sockfd, &fdrset) || FD_ISSET(sockfd, &fdwset)) {
errlen = sizeof(error);
if ((ret=getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &errlen)) != 0) {
/* getsockopt error */
if (verbose)
fprintf(stderr, "error: %s port %s: getsockopt: %s\n", argv[optind], argv[optind+1], strerror(errno));
close(sockfd);
return(-1);
}
if (error != 0) {
if (verbose)
fprintf(stdout, "%s port %s closed.\n", argv[optind], argv[optind+1]);
close(sockfd);
return(1);
}
} else {
if (verbose)
fprintf(stderr, "error: select: sockfd not set\n");
exit(-1);
}
}
/* OK, connection established */
close(sockfd);
if (verbose)
fprintf(stdout, "%s port %s open.\n", argv[optind], argv[optind+1]);
return 0;
}

void usage(char *prog) {
fprintf(stderr, "error: Usage: %s [-q] [-t timeout_sec] [-u timeout_usec] <host> <port>\n", prog);
exit(-1);
}
 
Old 01-02-2009, 03:07 AM   #2
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
Please,

What are you trying to achieve?
This looks like a homework exercise to me.

If it isn't homework, what should the options you want do? Is there any reason why you can't do it yourself.

What is wrong with the standard ping program available on all Linux distributions (and Windows for that matter).

--Ian
 
Old 01-02-2009, 04:11 AM   #3
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Hi sir

I am not a programmer.My ISP denied Ping that is why.

Please help me sir

I am hoping your help
 
Old 01-02-2009, 04:39 AM   #4
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
Please,

That program, while it is a "ping" program is different to the standard ping program.

Normal ping sends a number of ICMP echo request packets and awaits the reply. It then reports on the statistics, such as average round trip time.

The tcping.c program you have posted above is simply checking connectivity to a given TCP port. It creates a socket, and attempts to connect to the specified port. It then interprets the result as either open or closed.

Sample output from tcping.c:
Code:
$ ./a.out 192.168.1.1 80
192.168.1.1 port 80 open.
Have you tried compiling and running it? All you need to do is paste it into a file (tcping.c) and then run "gcc tcping.c". You can then run a.out which is the executable.

My understanding of the options you want implemented is that -c specifies the number of packets to send and -I specifies the interface to use. As per my explanation above, there is no point in implementing -c in tcping.c, as you would simply be checking connectivity to the same port multiple times.

You say that your ISP has blocked Ping? Why have they done this? Have you tried contacting them and finding out why? I find it difficult to believe that they have blocked Ping. ICMP traffic is essential for the operation of the internet, and so they can't block it.

--Ian

Last edited by IBall; 01-02-2009 at 04:42 AM.
 
Old 01-04-2009, 09:58 PM   #5
please
Member
 
Registered: Apr 2007
Posts: 195

Original Poster
Rep: Reputation: 30
Hi Sir IBALL

Thanks for your explain

I already contacted My ISP.They denied for DOS virus attack.
They can`t open ping for any reason

I tired with arping but this is can not helped me

Thanks
 
Old 01-05-2009, 09:00 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by please View Post
Hi Sir IBALL

Thanks for your explain

I already contacted My ISP.They denied for DOS virus attack.
They can`t open ping for any reason

I tired with arping but this is can not helped me

Thanks
Then get a different ISP. Nothing much can be done with this, unless the network further upstream lets you do what you want...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help - my Add/Remove Porgrams option is gone silvercastle Ubuntu 4 08-14-2006 07:30 AM
How do I add a Shell option to grub DiSGuiZ Linux - Newbie 4 04-15-2005 03:28 PM
how to add floppy option in grub? JIV Linux - Software 3 05-03-2003 08:51 PM
want to add lilo option to ntloader onlyhuman9 Linux - Software 1 09-21-2001 02:49 AM
ADD A PREVIEW OPTION Stephanie General 0 05-02-2001 09:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration