How to get an IP address from a host name? (Example in C)

  Переглядів 10,459

Jacob Sorber

Jacob Sorber

11 місяців тому

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
How to get an IP address from a host name? // This video shows you how to resolve host names or domain names in your C programs, using the getaddrinfo() function.
Related Videos:
Networks/Network Programming videos: • Network Programming
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]

КОМЕНТАРІ: 20
@Acorn_Anomaly
@Acorn_Anomaly 11 місяців тому
Also, one detail that was slightly incorrect in the video, at 3:30. "We also have server name." While it's outside the scope of this video(which was just about resolving the IP from a domain name), that parameter isn't server name, it's service name. For anyone curious why, you can use this parameter to have getaddrinfo() fill in the required port numbers in the returned structures, as well as the address, family, and protocol info. servname takes a string that is either a numeric port number expressed as a string("80"), or a defined service name("http"). It will fill in the corresponding port number in the returned sockaddr structs, so you can pass them straight to connect() or bind().
@Acorn_Anomaly
@Acorn_Anomaly 11 місяців тому
15:48 "Not sure why it's 2" Because UNIX sockets came first :P AF_UNSPEC(unspecfied) is 0, AF_UNIX is 1. AF_INET came next.
@nunyobiznez875
@nunyobiznez875 11 місяців тому
A video on DNS would be great. Also, unrelated, it would also be great if you did a video sometime on message queues, as well. It's the only POSIX IPC that you haven't covered. I figured you'd get to it eventually, but it's been a long time now. So, perhaps it got overlooked? I've noticed a lot of books on IPC overlook message queues as well. Is there a reason for that? Either way, some examples where message queues are a better choice than pipes or shared memory, would be great to see.
@matiasm.3124
@matiasm.3124 11 місяців тому
Or how to get TXT records or others like MX, NS etc..
@noodlish
@noodlish 11 місяців тому
I came across a very similar program recently in Beej's Guide to Network Programming. Well worth looking up for anyone interested in sockets APIs.
@trumanbeal5668
@trumanbeal5668 11 місяців тому
In W. Richard Steven's books on tcp/ip, he was saying ntop was network to presentation, and conversely the reverse for pton- Always stuck with me as it's human presentable.. This was a good video, it's something that usually spins people out a bit. I picked up a whole lot from beej's guide forever ago, like somewhere in the late 90s..
@eddaly4160
@eddaly4160 11 місяців тому
Great video. Yes, interested in UDP video
@npc_code
@npc_code 11 місяців тому
A Video about UDP would be great. In a lot of books, videos and material are only going in to detail with TCP (bcs. Making your own htttp server and stuff like that). So I think it would be great to also learn about UDP.
@michaczerwonka8720
@michaczerwonka8720 11 місяців тому
UDP and io_uring + reuseport
@anon_y_mousse
@anon_y_mousse 11 місяців тому
And UDP is great for games, so it's definitely a good idea to know how to handle it.
@SassyToll
@SassyToll 9 місяців тому
Thank you I learn alot
@anon_y_mousse
@anon_y_mousse 11 місяців тому
Maybe for a video you could reimplement traceroute and show how it works. That would be fun and informative. Although, admittedly I have strange ideas of what's fun.
@c0d_0x16
@c0d_0x16 11 місяців тому
Amazing content...
@Kelv_Nganga
@Kelv_Nganga 11 місяців тому
how about a reverse IP tool? perhaps one that is coded in go language and can work with CMD
@asssheeesh2
@asssheeesh2 11 місяців тому
In what case will the IP be different for same family(say IPV4)? Why would IP be different for socket and udp? I thought IP layer is agnostic of it. What am I missing?
@Acorn_Anomaly
@Acorn_Anomaly 11 місяців тому
The resolved IP shouldn't be different for different types. I think you're asking why he gets multiple return values for the different types as well as families? Even though the IP addresses are the same? The reason is because getaddrinfo() doesn't return IP addresses. It returns sockaddr structs, that are ready to be sent in to the socket() function to create a socket off of them. And prepared sockaddr structs have the socket type, address family, and IP address filled in. Which means, if you don't specify to getaddrinfo() which type or family you want, it has to return a separate one for all possibilities. For that matter, getaddrinfo() can also fill in the port in the sockaddr struct, as well, so you can pass the socket straight into bind() or connect(). That's what the *servname parameter is for. (Jacob is slightly incorrect in the video; it doesn't stand for "server name", it stands for "service name". You can pass either a numeric port as a string("80"), or a defined service name("http"), and getaddrinfo() will fill in the correct port in the returned structures.)
@asssheeesh2
@asssheeesh2 11 місяців тому
@@Acorn_Anomaly Thanks!
@user-vb8bm8tq3f
@user-vb8bm8tq3f 11 місяців тому
Interesting.
@kryptoid2568
@kryptoid2568 10 місяців тому
The same is roughly applicable in Win32 tho
@Tachi107
@Tachi107 10 місяців тому
11:24 this is the wrong way of getting a name from an addrinfo struct. You should use the getnameinfo(3) function. I always thought that using the "old" inet_ntop(3) function was the only way to print an IP address returned by getaddrinfo(3), but it turns out (by reading OpenBSD's inet_ntop(3) man page) that actually the correct and easy way to transform any version of IP address is to use getnameinfo(3) like this: char addrstr[INET6_ADDRSTRLEN]; getnameinfo(addr->ai_addr, addr->ai_addrlen, addrstr, sizeof addrstr, NULL, 0, NI_NUMERICHOST); Super easy, right? You just have to know how to do it! As always, we should always read the fantastic manual ;)
Binary data exercise: how to tell if a file is a jpeg?
17:48
Jacob Sorber
Переглядів 12 тис.
What is a semaphore? How do they work? (Example in C)
13:27
Jacob Sorber
Переглядів 280 тис.
😱СНЯЛ СУПЕР КОТА НА КАМЕРУ⁉
00:37
OMG DEN
Переглядів 1,5 млн
How to get hostname from IP using ping command #Troubleshootingsteps
2:05
16. How to Find the Number of Subnets  Valid Hosts
8:04
System Engineer
Переглядів 1,4 млн
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Переглядів 260 тис.
How a DNS Server (Domain Name System) works.
6:05
PowerCert Animated Videos
Переглядів 4,7 млн
How do I access a single bit?
11:07
Jacob Sorber
Переглядів 18 тис.
Find ANYONE's GPS Location Using IPLogger
10:20
Maythom
Переглядів 350 тис.
What is subnetting and why to subnet
8:47
Berry Smith
Переглядів 254 тис.