int main(void)
{
	STARTUPINFO si1;
	PROCESS_INFORMATION pi;
	LPVOID lpMsgBuf;

	// Initialize data
	ZeroMemory( &si1, sizeof(si1) );
	si1.cb = sizeof(si1);
	ZeroMemory( &pi, sizeof(pi) );

	if ( ! CreateProcess( 
		NULL,				// No module name (use command line)
		"print-pid",	// Command line for process
		NULL,				// Process handle not inheritable
		NULL,				// Thread handle not inheritable. 
		FALSE,			// Set handle inheritance to FALSE. 
		0,					// No creation flags. 
		NULL,				// Use parent's environment block. 
		NULL,				// Use parent's starting directory. 
		&si1,				// Pointer to STARTUPINFO structure.
		&pi )			// Pointer to PROCESS_INFORMATION struct
	)
	{
		// Try to format the error message from the last failed call (returns # of TCHARS in message -- 0 if failed)
		if ( FormatMessage( 
					FORMAT_MESSAGE_ALLOCATE_BUFFER |					// source and processing options
					FORMAT_MESSAGE_FROM_SYSTEM |		
					FORMAT_MESSAGE_IGNORE_INSERTS,	
					NULL,														// message source
					GetLastError(),										// message identifier
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),	// language identifier (Default language)
					(LPTSTR) &lpMsgBuf,									// message buffer
					0,															// maximum size of message buffer (ignored with FORMAT_MESSAGE_ALLOCATE_BUFFER set)
					NULL														// array of message inserts
				)
		) 
		{
			// Display the formatted string.
			PrintMessage( (LPSTR)lpMsgBuf );
			// Free the buffer.
			LocalFree( lpMsgBuf );
		}
		
		ErrSys( "CreateProcess failed." );
	}

	printf( "Created child with pid %d\n", pi.dwProcessId );
	/* parent continues concurrently with child */
    Sleep(3000);

	WaitForSingleObject(pi.hProcess, INFINITE);
	Sleep(3000);  // 3 seconds wait to let us see output

	ExitProcess(0);
}
Example #2
0
int main ( int argc, char *argv[] )
{
	STARTUPINFO si1;
	PROCESS_INFORMATION pi;
	DWORD dwChildID = 0;
	char szBuff[BUFFER_SIZE];

	// Initialize data
	ZeroMemory( &si1, sizeof(si1) );
	si1.cb = sizeof(si1);
	ZeroMemory( &pi, sizeof(pi) );

	// Check to see if child (arg2 = childID) or parent (1 arg) 
	if (argc == 2) {
		// Child
		dwChildID = atoi( argv[1] );

		if (dwChildID == 1) {
			ChildsPlay();
			ExitProcess(0);
		} else {
			ErrSys( "Unknown child created." );
			ExitProcess(1);
		}
	} else if (argc != 1) { // Parent has no args (1 = name of program, 2 = name + 1 arg, etc)
		ErrSys("Incorrect number of arguments.");
		ExitProcess(1);
	}

	// Parent

	// Fill in the command line for 1st child
	sprintf_s(szBuff, BUFFER_SIZE, "%s 1", argv[0]);

	/* Start the child process */
	if ( !CreateProcess( 
		NULL,  // No module name (use command line)
		szBuff,  // Command line
		NULL,  // Process handle not inheritable
		NULL,  // Thread handle not inheritable. 
		FALSE,  // Set handle inheritance to FALSE. 
		0,  // No creation flags. 
		NULL,  // Use parent's environment block. 
		NULL,  // Use parent's starting directory. 
		&si1,  // Pointer to STARTUPINFO structure.
		&pi )  // Pointer to PROCESS_INFORMATION struct
	)
	{
		ErrSys( "CreateProcess failed." );
	}

	printf("Created child with pid %d\n", pi.dwProcessId);
	/* parent continues concurrently with child */
			
	Sleep(2000);  // Windows Sleep() is in milliseconds
	
	printf("Shoo away!\n");
	
	/* wait for normal termination of child process */
	WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
	Sleep(2000); //so we can watch the output
	ExitProcess(0);
}
int main(int argc, char *argv[])
{
	STARTUPINFO si1;
	STARTUPINFO si2;
	PROCESS_INFORMATION pi1;
	PROCESS_INFORMATION pi2;
	char *szMessage1 = "Goodbye";
	char *szMessage2 = "World";
	DWORD dwChildID = 0;
	char szBuff[BUFF_SZ];

	// Initialize data
	ZeroMemory( &si1, sizeof(si1) );
	si1.cb = sizeof(si1);
	ZeroMemory( &si2, sizeof(si2) );
	si2.cb = sizeof(si2);
	ZeroMemory( &pi1, sizeof(pi1) );
	ZeroMemory( &pi2, sizeof(pi2) );

	// Check to see if child (arg2 = childID) or parent (1 arg) 
	if (argc == 2) {
		// Child
		dwChildID = atoi( argv[1] );

		if (1 == dwChildID) {
			PrintMessage(szMessage1);
			/*sleep(2);*/
			ExitProcess(0);
		} else if (2 == dwChildID) {
			PrintMessage(szMessage2);
			/*sleep(2);*/
			ExitProcess(0);
		} else {
			ErrSys( "Unknown child created." );
			ExitProcess(1);
		}
	} else if (argc != 1) { // Parent has no args (1 = name of program, 2 = name + 1 arg, etc)
		ErrSys( "Incorrect number of arguments." );
		ExitProcess(1);
	}

	// Parent

	// Fill in the command line for 1st child
	sprintf_s( szBuff, BUFF_SZ, "%s 1", argv[0] );

	/*printf("\n\nbefore CreateProcess\n"); */

	/* Start the child process */
	if ( ! CreateProcess( 
		NULL,  // No module name (use command line)
		szBuff,  // Command line
		NULL,  // Process handle not inheritable
		NULL,  // Thread handle not inheritable. 
		FALSE,  // Set handle inheritance to FALSE. 
		0,  // No creation flags. 
		NULL,  // Use parent's environment block. 
		NULL,  // Use parent's starting directory. 
		&si1,  // Pointer to STARTUPINFO structure.
		&pi1 )  // Pointer to PROCESS_INFORMATION struct
	)
	{
		ErrSys( "CreateProcess failed." );
	}
	
	/*printf("Created 1st child with dwProcessId %d\n", pi1.dwProcessId);*/

	/* parent continues and creates another child */

	// Fill in the command line for 2nd child
	sprintf_s(szBuff, BUFF_SZ, "%s 2", argv[0]);

	/*printf("\n\nbefore CreateProcess\n"); */

	/* Start the child process */
	if ( ! CreateProcess( 
		NULL,  // No module name (use command line)
		szBuff,  // Command line
		NULL,  // Process handle not inheritable
		NULL,  // Thread handle not inheritable. 
		FALSE,  // Set handle inheritance to FALSE. 
		0,  // No creation flags. 
		NULL,  // Use parent's environment block. 
		NULL,  // Use parent's starting directory. 
		&si2,  // Pointer to STARTUPINFO structure.
		&pi2 )  // Pointer to PROCESS_INFORMATION struct
	)
	{
		ErrSys( "CreateProcess failed." );
	}

	/*printf("Created 2nd child with dwProcessId %d\n", pi2.dwProcessId);*/
			
	/*sleep(2);*/    /* parent */
	ExitProcess(0);
}
Example #4
0
void SysWork(char *addr, int trc)
{
    int i=1,end=0,chk;
    u_char *src;                         //печать адресов на экран
    char my_ip[16];                      //для хранения своего адреса
    struct sniff_icmp *icmp,*icmprep;    //заголовки
    struct sniff_ip *ip,*iprep;
    struct sockaddr_in sr_addr;          //адрес, до котоого пингуем
    socklen_t len=sizeof(struct sockaddr_in);
    u_char buf[sizeof(struct sniff_icmp)+sizeof(struct sniff_ip)],
            recbuf[sizeof(struct sniff_icmp)+sizeof(struct sniff_ip)];
//----------------первоначальная настройка-------------------
    atexit(EndSys);
    GetIp("eth0",my_ip);                   //забираем свой адрес

    //пивязываем заголовк к памяти
    ip=(struct sniff_ip*)buf;
    icmp=(struct sniff_icmp*)(buf+sizeof(struct sniff_ip));
    iprep=(struct sniff_ip*)recbuf;
    icmprep=(struct sniff_icmp*)(recbuf+sizeof(struct sniff_ip));

    //заполнение заголовка ip
    ip->ihl=5;
    ip->ver=4;
    ip->tos=0;
    ip->len=sizeof(struct sniff_icmp)+sizeof(struct sniff_ip);
    ip->id=htons(7777);
    ip->frag_off=0;
    if(trc==1) ip->ttl=1;
    else ip->ttl=128;
    ip->protocol=IPPROTO_ICMP;
    ip->ip_dest=inet_addr(addr);
    ip->ip_source=inet_addr(my_ip);
    ip->cs=0;

    //заполнение заголовка icmp
    icmp->type=8;
    icmp->code=0;
    icmp->un.echo.id=0;
    icmp->un.echo.sequence=0;
    icmp->cs=0;
    icmp->cs=CheckSum((unsigned short*)icmp,sizeof(struct sniff_icmp));

    ip->cs=CheckSum((unsigned short*)ip,sizeof(struct sniff_ip));

    //заполнение адреса
    sr_addr.sin_addr.s_addr=inet_addr(addr);
    sr_addr.sin_family=AF_INET;
    //открываем сокет
    rid=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
    if(rid<0)
    {
        ErrSys("socket create\n");
    }

    //не заполнять заголовки автоматически
    chk=setsockopt(rid,IPPROTO_IP,IP_HDRINCL,&i,sizeof(i));
    if(chk<0) ErrSys("setsockopt\n");
//------------------основная работа-------------------
//trace
    if(trc==1)
    {
        puts("tracing...");
        do
        {
            //отправляем
            chk=sendto(rid,buf,sizeof(struct sniff_icmp)+sizeof(struct sniff_ip),0,
                   (struct sockaddr*)&sr_addr,len);
            if(chk<0) ErrSys("send\n");

            //ждем ответа
            chk=recvfrom(rid,recbuf,sizeof(struct sniff_icmp)+sizeof(struct sniff_ip),0,
                     (struct sockaddr*)&sr_addr,&len);
            if(chk<0) ErrSys("recv\n");
            else
            {
                src=(u_char*)&iprep->ip_source;
                printf("%d host: %d.%d.%d.%d, ttl: %d\n",i++,src[0],src[1],src[2],src[3],
                        iprep->ttl);
                //меняем ттл  пересчитываем контольную сумму
                ip->ttl++;
                ip->cs=0;
                ip->cs=CheckSum((unsigned short*)ip,sizeof(struct sniff_ip));
            }
        }
        //продолжаем, пока не получим ответ от искомого адреса
        while(ip->ip_dest!=iprep->ip_source);
    }
//ping
    else
    {
        while(end!=4)
        {
            //отправляем
            chk=sendto(rid,buf,sizeof(struct sniff_icmp)+sizeof(struct sniff_ip),0,
                   (struct sockaddr*)&sr_addr,len);
            if(chk<0) ErrSys("send\n");
            printf("ping sent\n");

            //ждем ответа
            chk=recvfrom(rid,recbuf,sizeof(struct sniff_icmp)+sizeof(struct sniff_ip),0,
                     (struct sockaddr*)&sr_addr,&len);
            if(chk<0) ErrSys("recv\n");
            else
            {
                src=(u_char*)&iprep->ip_source;
                printf("from %d.%d.%d.%d, ttl: %d\n",src[0],src[1],src[2],src[3],iprep->ttl);
                //printf("icmp type %x code %x\n",icmprep->type,icmp->code);
            }
            usleep(200000);
            end++;
        }
    }
}