"Political freedom is a society's safety valve, allowing the passionately critical a nonviolent way to express their dissatisfaction with the status quo." --David Cole
Before you begin reading, this was exclusively tested on Linux. I am unsure of the differences between Windows and Linux sockets, so I can't say accurately how applicable this will be on Windows. Were someone to elucidate me on such matters, I would be overjoyed to incorporate such material into this article.
I wrote this simple tutorial to provide some working code to people who are in a hurry to get up and running with C sockets. Sockets can be intimidating to newcomers to C, but hopefully the annotations and code henceforth will help out at least a bit to some people.
One confusing aspect about sockets is the fact that many tutorials online use the older sockaddr_in struct and gethostbyname() for holding server information and some do not. Some use the newer addrinfo struct with getaddrinfo() and others do not. And rarely is this discrepancy even mentioned by people writing with far more experience than I in the field. What's a beginner to do?!
The code that I made (with due credit to one author in particular for his very helpful tutorial in this endeavor) uses the latter (newer) form for gathering information about the destination host.
And in case the annotations within the code are insufficient for discerning what is happening in the following code:
1) the function con_socket() connects to a host name specified by argv[1].
2) the main() function calls con_socket() and returns a descriptor to be used for send()/recv().
3) the return value (sockfd) of con_socket() is then used in send() to send a message to the host specified by argv[1]. An important note here is that if you're sending a message with spaces within it, its probably a good idea to enclose argv[2] within quotation marks.
It is likely that this code will not be directly helpful to many people, but with simple modifications it could be the basis for a vulnerability detector similar to Bed or a fuzzer of some kind. I would be ecstatic if this helped in such endeavors, but if it helped only one person learn a bit more about sockets, I'd have fulfilled my mission in writing this.
Without further ado, here is the code. I looked over this several times but if there are any bugs that skipped my attention please let me know:
struct addrinfo hints, *servinfo, *p;
/*
addrinfo: this struct is called in getaddrinfo. A short synopsis of its contents are as follows:
hints.ai_family (is it IPv6 (AF_INET6) or IPv4 (AF_INET))
hints.ai_socktype (SOCK_STREAM for TCP, SOCK_DGRAM for UDP)
(see beej guide for longer explaination of other contents)
*servinfo: if you look below, you will see that the servinfo addrinfo struct is going to be filled with destination host information
*p : do not be confused by this as I was. In a for loop below, servinfo is going to be assigned to p for conciseness of code
*/
// hostname is argv[1], see main() and function declaration above
if ((rv = getaddrinfo(hostname, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// This will make a socket with the first available host (from the list received from getaddrinfo
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
//You will need to write recv code in here if you care about a response.
return 0;
}
Cast your vote on this article 10 - Highest, 1 - Lowest
Comments: Published: 6 comments.
HackThisSite is the collective work of the HackThisSite staff, licensed under a CC BY-NC license.
We ask that you inform us upon sharing or distributing.