uname()socket()
gethostbyname()bind()
socket()listen()
connect()
accept()

    1  /*
    2   * Scan local tcp ports
    3   * selfck.c
    4   * cc selfck.c -lnsl -o selfck
    5   */
    6
    7  #include <stdio.h>
    8  #include <sys/socket.h>
    9  #include <netinet/in.h>
   10  #include <errno.h>
   11  #include <netdb.h>
   12  #include <signal.h>
   13  #include <string.h>
   14  #include <sys/utsname.h>
   15  #include <stdlib.h>
   16
   17  #define MAXPORT 1024
   18  #define MAXSVCNAME 15
   19
   20  int main()
   21  {
   22      int net, portnum;
   23      struct hostent *host;
   24      struct servent *svc_ent;
   25      struct sockaddr_in sa;
   26      struct utsname  h_name[1];
   27      char   svc_name[MAXSVCNAME];
   28
   29      memset(&sa, 0, sizeof(struct sockaddr_in));
   30      if ( (uname(h_name)) < 0 ){
   31          perror("uname");
   32          exit(1);
   33      }
   34      if ((host = gethostbyname(h_name->nodename)) == NULL){
   35          perror("gethostbyname()");
   36          exit(1);
   37      }
   38      memcpy(&sa.sin_addr, host->h_addr, sizeof(struct in_addr));
   39      sa.sin_family = AF_INET;
   40
   41      for (portnum = 1; portnum <= MAXPORT ; portnum++) {
   42          sa.sin_port = htons(portnum);
   43          if((net = socket(AF_INET, SOCK_STREAM, 0)) < 0){
   44              perror("socket");
   45              exit(1);
   46          }
   47          if (connect(net, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) < 0){
   48              printf("%s  %-5d %s\r", h_name->nodename, portnum, strerror(errno));
   49              fflush(stdout);
   50          } else {
   51              if ((svc_ent = getservbyport( htons(portnum) ,"tcp")) == NULL) {
   52                  sprintf(svc_name,"Unknown");
   53              } else {
   54                  snprintf(svc_name, MAXSVCNAME, "%s", svc_ent->s_name);
   55              }
   56              printf("%s  %-5d < %-15s>   accepted.  \n",h_name->nodename,portnum, svc_name);
   57              if (shutdown(net, 2) < 0) {
   58                  perror("shutdown()");
   59                  exit(1);
   60              }
   61          }
   62          close(net);
   63      }
   64      printf("                                                   \r");
   65      exit(0);
   66  }

selfck.c

% ./selfck
myhost 22 < ssh > accepted.
myhost 53 < domain > accepted.
%


   26      struct utsname  h_name[1];
         struct utsname{
             char sysname[_UTSNAME_SYSNAME_LENGTH];
             char nodename[_UTSNAME_NODENAME_LENGTH];
             char release[_UTSNAME_RELEASE_LENGTH];
             char version[_UTSNAME_VERSION_LENGTH];
             char machine[_UTSNAME_MACHINE_LENGTH];
             char domainname[_UTSNAME_DOMAIN_LENGTH];
         };
   29      memset(&sa, 0, sizeof(struct sockaddr_in)); 
   34      if ((host = gethostbyname(h_name->nodename)) == NULL){
   35          perror("gethostbyname()");
   36          exit(1);
   37      }
   38      memcpy(&sa.sin_addr, host->h_addr, sizeof(struct in_addr));
gethostbyname()
#include <netdb.h>
struct hostent *gethostbyname(const char *name);

          struct hostent {
                  char    *h_name;        /* official name of host */
                  char    **h_aliases;    /* alias list */
                  int     h_addrtype;     /* host address type */
                  int     h_length;       /* length of address */
                  char    **h_addr_list;  /* list of addresses */
          }
   41      for (portnum = 1; portnum <= MAXPORT ; portnum++) {

   43          if((net = socket(AF_INET, SOCK_STREAM, 0)) < 0){
   44              perror("socket");
   45              exit(1);
   46          }
socket()
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);



   47          if (connect(net, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) < 0){
   48              printf("%s  %-5d %s\r", h_name->nodename, portnum, strerror(errno));
   49              fflush(stdout);
   50          } else {
   51              if ((svc_ent = getservbyport( htons(portnum) ,"tcp")) == NULL) {
   52                  sprintf(svc_name,"Unknown");
   53              } else {
   54                  snprintf(svc_name, MAXSVCNAME, "%s", svc_ent->s_name);
   55              }
   56              printf("%s  %-5d < %-15s>   accepted.  \n",h_name->nodename,portnum, svc_name);
   57              if (shutdown(net, 2) < 0) {
   58                  perror("shutdown()");
   59                  exit(1);
   60              }
   61          }
connect()
#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen )



   57              if (shutdown(net, 2) < 0) {
   58                  perror("shutdown()");
   59                  exit(1);
   60              }
   61          }
   62          close(net);
   63      }
shutdown()
#include <sys/socket.h>
int shutdown(int s, int how);




トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS