Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2005, 8:36 AM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Question Getting the process that's opening port x?

Hey guys, currently, I've written a port scanner, and need a little help. If I do a scan on 127.0.0.1 with the start port 10 and the endport 30, then it scans, and I found an open port! Is there anyway to determine what process is using this open port in C++? If you'd like, here is the source... Of what I have so far:
#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <conio.h> // including this for only getch()...
#include <windows.h>
#include "colors.h"

using namespace std;

    static int __BACKGROUND = BLACK;
    static int __FOREGROUND = LIGHTGRAY;

    void textcolor(int color)
    {
         __FOREGROUND = color;
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color + (__BACKGROUND << 4));
    }

    void textbackground(int color)
    {
         __BACKGROUND = color;
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), __FOREGROUND + (color << 4));
    }

int main()
{
    SOCKET ehsocket;
    char ip[50];
    cout << "Enter an Ip address to scan: " << endl;
    cin.getline(ip, 50);
    char astartport[30];
    cout << "Enter the starting port to scan: " << endl;
    cin.getline(astartport, 30);
    char aendport[30];
    cout << "Enter the ending port to scan: " << endl;
    cin.getline(aendport, 30);
    int startport = atoi(astartport);
    int endport = atoi(aendport); 
    WORD sockversion;
    WSADATA wsadata;
    sockversion = MAKEWORD(1, 1);	
    WSAStartup(sockversion, &wsadata);
    cout << "Now scanning..." << endl << endl;
    for (int n = startport; n <= endport; n++)
    {
        SOCKET ehsocket;
        int ehconnect;
        SOCKADDR_IN info;
        info.sin_family = AF_INET;
        info.sin_addr.s_addr = inet_addr(ip); 
        info.sin_port = htons(n);
        ehsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        ehconnect = connect(ehsocket, (SOCKADDR *)&info, sizeof(SOCKADDR_IN));
        if (ehconnect != 0)
        {
           textcolor(RED);
           cout << "Ip: " << ip << "  Port: " << n << "  Status: Closed" << endl;
        }
        else
        {
            textcolor(GREEN);
            cout << "Ip: " << ip << "  Port: " << n << "  Status: Open" << endl;
        }
    }
    textcolor(WHITE);
    cout << endl << "Press any key to exit...";
    getch();
    closesocket(ehsocket);
    WSACleanup();
}
And... Here is "textcolor.h":
//textcolor.h

#ifndef TEXTCOLOR_H
#define TEXTCOLOR_H

void textcolor(int);
void textbackground(int);

typedef enum
{
    BLACK,
    BLUE,
    GREEN,
    CYAN,
    RED,
    MAGENTA,
    BROWN,
    LIGHTGRAY,
    DARKGRAY,
    LIGHTBLUE,
    LIGHTGREEN,
    LIGHTCYAN,
    LIGHTRED,
    LIGHTMAGENTA,
    YELLOW,
    WHITE
} COLORS;

#endif // TEXTCOLOR_H
So, any idea on how I would do this...? Thanks!
layer is offline   Reply With Quote
Old May 12th, 2005, 8:39 AM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Comparing it to this list.
__________________

tempest is offline   Reply With Quote
Old May 12th, 2005, 8:44 AM   #3
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
So there is absolutley no way to do it in C++? I'd just have to run through that list and do a little strcmp() for everyline? Boo
layer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC