Tag: linux
Hilfe für das Speicherlayout
12. November 2012 04:56

Dieses kleine Programm zeigt eindrucksvoll das Speicherlayout unter Linux.

                    /*
 	-Useful program to show the address layout for beginners-

 	tuxwave.net 2010
	Have phun.
 */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

void reversefunc(int blah);

int main(int argc, char *argv[], char *envp[])
{
	int i = 0;
	static int foo1, foo2 = 1000;
	char * chunk1 = (char *)malloc(sizeof(char)*1024);
	char * chunk2 = (char *)malloc(sizeof(char)*4096);

	printf("Equiv: long long int -> quadword || long int -> doubleword || short int -> word || char -> byte\n");
	printf("Size of: long long int: %d long int: %d short int: %d char: %d\n\n", sizeof(long long int), sizeof(long int), sizeof(short int), sizeof(char));

	printf("Size of: float: %d\n", sizeof(float));
	printf("Size of: long double: %d double: %d\n\n", sizeof(long double), sizeof(double));

	printf("Adress of main() is: %#x\n", &main);
	printf("Adress of automatic i (integer) in main() is: %#x\n", &i);
	reversefunc(0);
	printf("Adress of static uninitialized foo1 (integer) in main() is: %#x\n", &foo1);
	printf("Adress of static initialized foo2 (integer) in main() is: %#x\n", &foo2);
	printf("Adress of malloc chunk1 (char *) in main() is: %#x\n", chunk1);
	printf("Adress of malloc chunk2 (char *) in main() is: %#x\n", chunk2);
	printf("Adress of glibc function strcpy() is: %#x\n", &strcpy);
	printf("Adress of systemcall wrapper write() is: %#x\n", &write);

	printf("\n\n"
	".text: code segment (functions)\n"
	".data: initialized data (initialized global variables, initialized static variables)\n"
	".bss: uninitialized data (static variables and global variables)\n"
	"heap: dynamic extension of bss contents (malloc, sbrk)\n"
	"stack: automatic local variables, function parameters, register contents of cpu\n"
	);
}

void reversefunc(int blah)
{
	if(blah != 5)
	{
		printf("Address of int argument blah in reversefunc() is %#x\n", &blah);
		blah++;
		reversefunc(blah);
	}
	return;
}

                    
        
Download
epoll Syscall Demo
12. November 2012 04:50

Demonstration Code für den epoll Syscall.

                    /*
	This is a demonstration code, for coding practice.
	August 2010 tuxwave.net

	epoll
	but needs to be looked in further
	there are possibilities to optimize :)

	test it like using this:
	(just for example put this into an script and make thousands background processes, but then do not use while :))
	while true;do echo foo|nc -q 1 localhost 8080; done 

 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(int argc, char *argv, char *envp[])
{
	struct sockaddr_in6 server_address;
	server_address.sin6_family = AF_INET6;
	server_address.sin6_addr = in6addr_any;
	server_address.sin6_port = htons(8080);
	int server_fd;
	int flags;

	int client_fd, client_len;
	struct sockaddr_in6 client_address;
	client_len = sizeof(client_address);

	if((server_fd = socket(PF_INET6, SOCK_STREAM, 0)) == -1)
	{
		fprintf(stderr, "Error creating socket\n");
		exit(EXIT_FAILURE);
	}
	if((bind(server_fd, (struct sockaddr *)&server_address, sizeof(server_address))) == -1)
	{
		perror("Could not bind to socket");
		exit(EXIT_FAILURE);
	}

	flags = fcntl(server_fd, F_GETFL);
	flags |= O_NONBLOCK;
	fcntl(server_fd, F_SETFL, flags);
	listen(server_fd, 10000);
	
	int epollfd = epoll_create(10000);
	struct epoll_event ev, *events;
	memset(&ev, 0, sizeof(struct epoll_event));
	events = malloc(sizeof(struct epoll_event)*10000);
	memset(events, 0, sizeof(struct epoll_event)*10000);
	ev.events = EPOLLIN;
	ev.data.fd = server_fd;
	if((epoll_ctl(epollfd, EPOLL_CTL_ADD, server_fd, &ev)) == -1)
	{
		perror("epoll");
		close(server_fd);
		exit(EXIT_FAILURE);
	}
	int statechanged;
	int i;
	while(1)
	{
		statechanged = epoll_wait(epollfd, events, 10000, -1);
		for(i = 0;i < statechanged; i++)
		{
			int fd = events[i].data.fd;
			if(fd == server_fd)
			{
				if((client_fd = accept(server_fd, (struct sockaddr *) &client_address, &client_len)) != -1)
				{
					char addr[100];

					struct epoll_event cev;
					memset(&cev, 0, sizeof(struct epoll_event));
					cev.events = EPOLLIN;
					cev.data.fd = client_fd;

					if((epoll_ctl(epollfd, EPOLL_CTL_ADD, client_fd, &cev)) == -1)
					{
						perror("epoll");
						close(server_fd);
						exit(EXIT_FAILURE);
					}
					printf("%d accepted %s and added to epoll\n", client_fd, inet_ntop(AF_INET6, &client_address.sin6_addr, addr, 100));
				}
				continue;
			}
			else
			{
				int nread;
				ioctl(fd, FIONREAD, &nread);	
				if(nread > 0)
				{
					char buffer[1024];
					memset(buffer, '\0', 1024);
					recv(fd, buffer, 1023, 0);
					printf("Received: %s\n", buffer);
					if(strstr(buffer, "keks") != 0)
					{
						memset(buffer, '\0', 1024);
						strncpy(buffer, "Hier ist dein Keks\n", 19);
						send(fd, buffer, strlen(buffer), 0);
					}
					else if(strstr(buffer, "end") != 0)
					{
						if((epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL)) == -1)
						{
							perror("epoll");
							close(server_fd);
							exit(EXIT_FAILURE);
						}
						close(fd);
						printf("Removing client on fd %d\n", fd);
						continue;
					}
					else
					{
						send(fd, buffer, strlen(buffer), 0);
					}
					printf("Served client on fd %d\n", fd);				
				}
				else
				{
						if((epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL)) == -1)
						{
							perror("epoll");
							close(server_fd);
							exit(EXIT_FAILURE);
						}
						close(fd);
						printf("Removing client on fd %d\n", fd);
				}
			}
		}
	}
	exit(EXIT_SUCCESS);
}

                    
        
Download