 All right Everyone Let's make sure everything's working technology Still showing Tiffany's video and not me Okay All right, you have a nice restful weekend what we mean. No, it wasn't because of me. Oh Well, I don't care about your other classes. I mean in relation to this class Yeah, you didn't have any assignments to work on this weekend It was a gift to all of you. Thank you. You are welcome Next time you'll have more so thank you But I wasn't fast enough Clearly need to learn about kissing up. Okay Cool moving forward so we will have to not keep you in this limbo where you're spending so much time on quote other classes We'll have a new module releasing today after class It's gonna be putting together what we've learned before of understanding HGP and Building a web server. So we're gonna be building a web server in x8664 yes After class today That's important you're not asking when you can start working on it We do Tuesday next Tuesday, so eight days and Then we can look at this is a nice graph of the last module So is the grades a daily grade in the last module at some type of midnight? We don't know what time zone midnight. This is This line here is the 70% line So you can see the none is people who haven't even started or solved one challenge, right? We're not even starting a challenge, right? Yeah, so it's not even a zero is you at least started a challenge and couldn't solve that one That's here This none is didn't even start So we can see this as we get closer Boom boom boom boom boom and what's this roughly so somewhere between these is roughly the grades right on the last module The last three bars are accurate All rough what maybe they didn't work past the deadline these are these students are dedicated to their assignments So yeah, pretty good. I think there's a good distribution. I was happy to that five percent. Just don't bother. Yes, that is correct That is how it goes Cool. Okay So if this doesn't tell you I don't know if you're in the on the left side of this line now a There's the late policy. So you get 50% credit to try to move over the line Be start early, right? So I think yes, this doesn't show it, but I'm certain the people that started like here Got to the far right by the time Assignment was due. All right Now let's talk about So we actually already covered system calls, right? Right remembering what we did on Wednesday in class We had class Wednesday. You watch the lectures for sure What did we talk about stuff? Yeah with one slide in okay We Let me double check that. I thought we went farther. Did we not? We got to here didn't we get to like all this table? Yeah Yeah, okay We talked about a lot of system calls Cool, okay, so we talked about all these system calls now we're gonna put it in action So I'll go over demo the very first module because or the demo of the very first level because it's honestly very easy And the assignment literally tells you how to do it. Okay. What's the first thing I should do? challenge run Okay In this series of challenges, you'll be writing assembly. Hey, we like assembly to interact with your environment Hey, we're learning how to interact with the environment And ultimately build a web server. Hey, we've using web clients now We get to build a web server In this challenge, you will exit a program the most fascinating of programs Okay, so to use this we're gonna do slash challenge run and give it the path to a web server And actually this is actually setting you up, but let's ignore this for now I'll start with this as a placeholder So let's start up with a server I'm gonna call You probably will not have this on your machine Okay S I hate lowercase s, but that's fine. We'll go with it. Okay, so I'm gonna delete this stuff So we know we're gonna have to do a system call. Yeah, correct It will be deployed after glasses the point is for you to pay attention to start You can do it later cool Okay, so we have some assembly code and it's telling us how to assemble What how is this one different than what we were doing last time? So we're using as to what is AS do? It assembles our assembly file to what? Yeah, an elf object file and then LD the loader turns that object file into what an executable that we can run Cool, so now we should have something dot server and segmentation of all core dumps, okay Let's run it with the challenge first and then we'll Cool, so it tells us again what our goal is and it's tongue It's what it expects in the format that we're gonna talk about in a second s trace So s trace is going to be your friend It traces the system calls that your process makes this actually is incredibly handy Even knowing that you can do this There were definitely some cases where you're trying to track down a bug and trying to see what's going on You can actually run s trace on any program And it will tell you all the system calls. So we have something like LS We can run s trace on LS And it's gonna tell us every single system call that the LS process made And you can see there's a lot. Why is there so much? Yeah, there's well, there's only that so there's it retrieving things about the directory Let's do it again and do LS L.A. Libby slash temp So that will run and now we can look for slash temp So we can actually see just the system calls that were related to slash temp So it's stat you can look up the man page for every single one of these system calls to understand exactly what's going on But stat tells you information about that it then tries to open slash temp It then calls f stat to get information about that Which this get dense? I think this gives all the directory information and then it's translating that and writing that out But this is actually just the start of the program There's actually all this other junk that has to happen And this is because programs are complex and things need to happen. So this is the first exec exec Vee LS Then all this stuff has to do the loader has to load it has to load in all the libraries that the LS process uses This is all that stuff, but you can see literally everything that a single process does. Let's check Okay, let's go back to challenge run So how do we parse this it says that we were expected to do an exec Vee of the exec Vee args This past so we can say yep, that's good, and then we're supposed to Call exit zero Okay, we didn't call exit zero. Why didn't we call exit zero louder? Yeah, I didn't write out what my let's look at the code, right? It's just a Sis call. So what system call is being called? How can I figure that out? Yeah, I could debug it I can see what's in the register or I have this program here. I can run S trace and It's telling me that it does an exec Vee and then it calls a read. So read zero null zero What was the somebody put it up the x86? System call table. I like the Chapman one So it was the read so we're calling read So read means which value had to have been an rx Zero and what was the file the script I passed in? Zero and what was rsi? No, and what was rdx? Also zero Right. So the seg fault is because we tried to read from null. I think although it's weird Why did it seg fault of it? Oh probably because there was nothing after it. Maybe that's what we want Okay So this is the wrong system call. Which system called do we want to call? Was it exit We want to call exit and we want to call exit zero so Let's go back in here. So if I want to call exit I'm gonna look for sis exit is 60. Thank you Move into what register? All right, X 60 and What do I want to exit on? What's the error code? So let's run it again Yeah, going back up to here So it's expects that I call exec Vee or sorry exit and then I pass it the argument zero Right so you can think of this as essentially C code right of the arguments So you can say okay the argument to exit must be zero I can look in the sis call table and say this This first table is RDI is the argument. So sis exit. So the error code goes in RDI So I should be able to then say Move into RDI zero so move zero into RDI Do the sis call Run it How can I check? How do I know before I run the challenge that I can check if I was successful? That's trace. How can I get a side fault here? Yeah, it was not because of that but because I exit the program the program literally stopped execution Nothing else happened. What happened before when I wasn't calling exit Was that what happens? So you call a sis call and then the next instruction gets executed So the problem was the next instruction after this was some garbage data that was attempted to be executed Cool Okay, now we can see with this we can see we have an exit zero here, which is great This is exactly what we wanted now if we run this challenge run Hey We get the flag so we can see the exit zero and success questions Yeah, yeah, so this is there's This is essentially a check that it's executing correctly So this isn't something so we can actually run the S trace and see this I want to say I believe this is an artifact of S trace that it does this and shows this system call because it's technically not happening from your program itself Right your program itself doesn't call an exec VE and execute execute another process So this is just a check in here that this is happening correctly So this should always pass and you shouldn't Worry about it, but this also means that it should be easy on the map what it expects to your trace of S trace Okay, now I were to go over to level two So one shortcut is I can just pass in my last program into here And we'll say in this challenge you'll create a socket so expected output the exec VE a socket now and Then an exit So we did The oh, it doesn't know that the exit was correct interesting We did an exec VE in the exit, but we failed because we're missing the socket call So how do we figure out how to make a socket call look on the syscall chart figure out what system call is is Socket put that in rix What would it be the next thing? Yeah, the parameters. This is where it gets slightly more tricky Because so when you write it in C code, you can actually just pass in If I net Sox stream IP proto IP. Why is that? Yeah, there's libraries and there's macros that are defined that defined exactly what these things are And I cannot remember where they are on here, but let's just grab there's several different ways we can do this We can Google for a if I net Linux We can actually literally look at the system call. Yeah, there we go include Linux socket H So we can look at the source code of Linux. Look at what it defines these values as What did I say AFI net? And this would be to You do have to be slightly careful that you're looking at the right thing BSD is another family of operating systems that implements a socket interface Right the whole point of using macros like this is your your C code doesn't is not hard coding the value to it's hard Coding this macro depending on what systems you're using it and what header files you're pointing it to it will work for your operating system So these values may be different. Let's say on a Mac versus a free VSD system versus a Linux system I believe we should be able to find this Here Let's see. This is just to find it because I don't know off the top my head Dash R want to sorry. I'm Usually I'm supposed to talk about what I'm doing. I want to When I'll put the file name that matches I think it's camera for such L. Yeah, lowercase l files with match So I want to laugh. Oh, I did dash L. Oh cool Okay, I'm getting a bunch of garbage no such file whatever so I can redirect Standard error the dev null Okay, I'm clearly my regular expression is busted Oh, but there I think is Do you know where it is? Yeah, I would go literal dots user lib Python user adluby Ruby Python 38 There's a lot of junk. Hey, there we go. Okay Cool, so let's see this matches up include Linux socket H So here we get complicated like this is defined as another one. So PFI net is defined as AFI net So PFI net Is also to yeah, cool. So we got here in both ways so you can For a lot of these you can look them up on your local file system using this file that we just found together You can also do things like S trace is good because it will show you what that name is if it knows it So when we call socket if it knows the exact argument It will give it to us in the text format so we can double-check this why you always want it If you're super confident, I guess you can just run it with the challenge Otherwise run S trace compare make sure it looks the same as it should and then you can run it against the challenge cool Okay, but let's look at the other system calls that we need to make so we did this We did this So we looked at open read and write system calls that allow us to interact with the file system now. We're gonna look at System calls that let us talk to the network So one of the key concepts that's used When using network programming is the concept of a socket later on will go into more in depth of what This means at the like TCP level But for now we can think of it so when we call open on a file, what if we get back from the operating system? Yeah, the file descriptor right and what was the file descriptor? Like if it told us this file descriptor three does I mean it was the third file on the system? Yeah, describe well It's I wouldn't necessarily say it describes the file Although it kind of does it tells it allows us to talk to the operating system about what file it is Right, so we asked the operating system. Hey open this file. It says, okay. Here's a file descriptor In some integer and when you want to read it right from that file give me this file descriptor again And I will let you read it right from that file so the socket is a similar concept that We Tell the operating system. Hey, I'm gonna want to do some network stuff Here's the file descriptor that I want to use Or sorry Here's the week. Let's go look at the To yeah, it's too So we have the domain the type and the protocol So this is as we saw it's a little bit not here. We even have some examples here So this is because there's different types of sockets that we can have We may want to talk on all different types of protocols and the operating system needs to know that Are we trying to talk on I internet like IPv4 protocols? Is it IPv6? Is it I actually don't even know what half of these things are Bluetooth? I guess we can talk Bluetooth on there VMware a Vsoc thing something to do with VMware all types of things. So this is the The domain argument is one of these domains where we tell it. Hey, this is the kind of communication. I'm gonna do Then from there we give it the type And this is slightly more Defines how we interact with this socket We'll again go when we go more details down into the Networking stack, but we can send Sockets can be stream sockets where we can send as much data as we want read as much data as we want Whereas a datagram socket is like you can maybe like a postcard we just send some data fixed amount of data to the other side and We can do raw network access That's interesting. I wonder what that is for Anyway, so we can specify the type we'll be using sock stream for almost everything that we do But we will we get a networking we'll look at UDP and that will have datagram packets And then we have the type sorry the protocol so protocol is where okay the protocol specifies a a Particular protocol that should be used with the socket normally only a single protocol exists as for a specific particular socket type Within a given protocol family in which case protocol can be specified as zero However, it is possible that any protocols may exist and such a blah blah blah blah Anyways, okay, so complicated, but most of the time we can ignore that and it'll just be zero And so when we do this it's going to get us back and return the operating system We set everything up correctly. We'll get everything going get everything ready for us to then make the final To make other calls to this, but this doesn't actually do any connections or do anything It just tells the operating system. Hey, I'm gonna do some networking stuff And I give me a final descriptor to talk about that cool, and that's when we get a bind system calls the idea here is We need to tell the operating system Depending on what specific type we're using but we We want to assign an address to it and we'll see what this looks like in a second. This is actually Very kind of interesting thing. So this takes in a socket FD Where did we get the sock FD from? From yes from the return value of calling socket. So we called socket. We got a return value Then we're gonna bind that to an address Let's look at the bind system call We pass it a sock FD, which we already saw then we pass it this struct and An address length. So when a socket is created with socket it exists. Oh, this is exactly what's on the slides Traditionally this operation is called assigning a name to a socket It is normally necessary to sign a local address using bind Before we can receive connections The rules vary between families Consult the entries in section seven for detailed information for AF I net which what we're gonna be using So if you want to like dig into this even more You can do seven IP and I think seven is Like more not actual functions, but descriptions of things like this is the Linux IPv4 protocol implementation So this tells you how to get a TCP socket or UDP socket. Anyways This would tell you more about that, but we don't need to do that for our purposes So the actual structure pass. This is what's kind of interesting the actual structure pass depends on the address family Different types of addresses have different sizes for things. So for instance, I think we briefly touched on this but in You've seen IPv4 addresses Yeah, what do they look like? Yeah, like one nine two dot zero dot zero dot one So each of those dots how many numbers can be within those? Zero to what? Yeah, zero to 255 so 256. So each of those is how many bytes? Sorry, each thing of suffice that each Between the dot right so one nine two dot blank dot blank dot blank if each of those 256 possible values, how many bytes is that? One bite and there's four of them. So how many bytes in an IPv4 address? Four bytes. There's how many bits? 32 look at this Think about where you were like four weeks ago. Now you're just bits and bytes powers of two like crazy. So IPv4 addresses are 32 bits. So if you're If you're trying to bind to a specific IP address You would give it something that's roughly 256, I'm sorry four bytes or 32 bits, but IPv6, well, what's the problem with IPv4 addresses? Anyone know? We've technically I think already run out depending on how you count. There's no free like IPv6 address space for YouTube use Where are you getting your IP addresses from they will usually have Extras that they can give you and So when they created an IPv6, don't ask me what happened IPv5. I don't know but when they created IPv6 They said okay, we want enough IP addresses that we're never gonna run out. So what did they do? So they double it Yeah, no, they quadrupled it. So IPv6 addresses are a hundred and twenty eight bits or 16 bytes So that's a lot. That's why they're They actually use hex hexadecimal Like this. Well, no, that's wrong. That's an Ethernet address. This does not have an IPv5 This one This one Says it's link ether. Does that mean it's a MAC address though? I'm slightly worried Three four five six. Yeah, it should be eight octets if it's a right Four four four four sixteen. Yeah, it should be sixteen. Okay, there we go There's an IPv6 address. Oh, yeah, that's right because they use the two bytes and then they if Things are zero between there. They have an empty colon So this is just the format for IPv6 addresses. It's not actually important You don't need to know this but it's cool to see this So anyways, it's just all getting the point across that like when you're talking to the operating system depending on what type of Socket you have you may have different sizes of address, right? And so that gets us to bind cool So when we bind another fun fact about this is that You can oh interesting Is this actually causes I found on operating systems this causes a lot of problems Having data structures where you have to specify. Hey, there's this address and this is the length Sometimes they'll get a structure and assume it's an IPv6 structure like a big structure But you passed it a small structure and so it ends up reading memory outside of where it was assigned or the reverse you say it's a You or another way you give it a small structure and it copies the big structure on there That's how you get an overwrite. So this causes like massive problems. Anyways, so We can dig further into this struct socked adder a Sock adder. Okay, so we got to start digging in Parsing these C structures because this is how we talk about these things. So this is a sock adder you first have this essay family and This tells it What type of sock adder this is and it's so it's the waiter Parsis is a you in so an unsigned int 16 is how many bits So how many bites is that to? Start testing you more Let's go It's okay, it happens. Yeah, so eight eight bits is a bite So you int eight is the same as a character like you used to thinking about one bite You in 16 two bites. So the first two bites says the family and the rest of it is how How to interpret this essay data. So one key question is what's the total size of the struct sock adder in Bites so how many bites for the essay family to and then how many for the rest 14 single bites. So a total of 16 bites. Yeah, cool. So sock adder underscore in so the underscore in is not like incoming. It's for internet like Abbreviation there and so You still have the same family 16 Bits so two bites for the family and then a port and then a 32 bit address and this corresponds exactly with what we're talking About with how big our IPv4 addresses four bites Which is how many bits? 32 which is exactly this s in adder. So socket internet address What are the ports for when you're talking about and learning about talking web, what port that you use? 80? Yeah, where there weighs why 80? Yeah, did you have to use 80? No, there was ways in the URL to specify different ports, right? So similarly here when we're binding to an address, we're gonna bind to a port and an IP address And specifically when we're doing a server, we're gonna bind because that's what we're gonna list it on for incoming connections So all the servers that you talked to on your browser All of them had to literally call socket to get a file descriptor with the right arguments And then call bind and pass in a sock adder of what they want to do pass in Okay, so what we're gonna want this Struct sock adder in to look like Is we're gonna want the The family the first two bites to be AFI net because this is part of the specification It's because we want anyone who looks at this to interpret this as an internet address and Then we have so why can't we use 80 here? For the port so we talked about port 80. Why do we want for 80? It's the default for HTTP. Yeah, so I get back into Endiness so you remind us what end in this means Yeah, so if the most significant bite is on the left or the right of a multi bite Address in memory and I kind of mentioned this I think when we talked about end in this that the CPUs use little end in order But network protocols use big end in order. So this H HH tons or Host to networks Will flip the bytes around for us and it also is a well This is a C wrapper that Will figure out if we are a little Indian or big Indian Nothing for little Indian flips the bytes around correctly So we'll need to make sure that when we do 80 that it's actually the two bites are in the right order Then we need 32 bits that represent The IP address of 127 dot zero dot zero dot one This is a special actually technically a range of IP addresses This is a special IP address that means local host and so that will be the sin adder and then the padding So we have eight bytes left over so if you what we say it was 16 bytes total for this sock adder in or serve for the Sock adder and so this should be two bytes for the family two bytes for the port another four bytes Or four four bytes for the address eight and then another eight is 16 which is exactly what the other one was so the size of those structures are the same Awesome, and then we can see this in memory of what this should look like. So this should be 16 bytes The first two bytes should be 0 to 0 0 Because we saw a affine it is to The next two bytes should be 80 I guess in hexes 50, but if it was normal 80 would be 50 0 0 But because it's big Indian those bytes are flipped around 0 0 5 0 and Then finally these are the if you do the translation. Oh, actually you can do this translation very easily how There's no Z's but 7f is what you mess up. Yeah And each of those right we said is a bite so you can easily convert each of those They call it dotted decimal format into a hexadecimal value We can just double check that Because who likes leaving things unspecified 127 converted there 7f zero converted to hexes and One converted to hexes Yeah, very easy. Thank you Cool. So we have 7f 0 0 1 and Then we finally have these eight bytes of zero So if we were like laying this out in memory, this is exactly how it looks like and so if we pass this as the If we pass the memory address here as the argument to bind It would then say okay, you have a socket I'm gonna start listening on local host 1 2 7 0 0 1 port 80 Well, I get the bind end listen. Oh, yeah, that's saying that you want to actually start listing the things, right? because bind says okay bind is address it checks to see because you You fundamentally can't have two different ports on To you can't be listening on two different ports of two different programs Because whenever your operating system gets a packet destined for that port it has to send it to some process So it needs to know where it goes So we bind Thank you We bind and then we start so then we've reserved that IP address and that port and Then we want to listen This said that okay, we are ready to start accepting incoming connections and Finally so we can look at what so the sock fd. We know that's the sock That's the file is super. We got back from socket backlog. Let's go check what the backlog is The backlog argument defines the maximum length to which the queue of pending connections may grow if a connection request arrives When the queue is full the client may receive an error Well, my bug fits in the request may be ignored so I later attempt at connection receives Guess I've never know what Okay, aha socket Bound to a local address with bind listen and then accept Okay, perfect So if the backlog argument is greater than whatever is in this value then it's silently truncated into that value so great So we can just put a very large value in there and then it doesn't matter and we'll just use that and we could ignore it Cool just large value in there And then finally This is everything to tell the operating system. Hey, this is I want to listen to this type of communication I want to listen on this specific IP address and port and then I'm ready to listen and then finally we can Call the accept system call Where we give it a file descriptor and It then will return a new okay So it extracts the first connection request on the queue of pending connections So I believe if we look it up, it'll say it's a blocking call So this means that your program sleeps until a request comes in Creates a new connected socket and returns a new file descriptor referring to that socket So how many after we get a connection? How many sockets do we have to what's the first one? the listening one yeah the socket that we set up to tell the OS that we wanted to bind and listen and then what's the second one the return value of accept So then accept is going to then return to us a new file descriptor That then we can we'll use that as we'll see to read and write To that file descriptor and that gets sent to the other side of the connection and we read from that file descriptor We are reading from the network connection. So this is how we can use this in order to listen to networks connections so we can walk through this whole process and Then we're yeah, we still got a half hour Cool. So to accept a network request. We first call socket. So This is how we get an IPv4 Socket that we care about AFI net Sockstream IP proto IP We call that the operating system finds in the file descriptor table of our process that oh file descriptor 3 is not being used Great, I'm gonna set up this socket We then bind called bind pass it in the file descriptor we got back and Pass it a pointer to that object that we just saw in memory Right, so this will then say So what's the difference between the previous examples I showed in this example in terms of the Sock adder well the address will death. Yes, which address we need to be more specific. There's memory addresses. There's Yeah, the IP address so zero zero zero zero is a very specific address This well, what would that be in bytes? So we know it's four bytes. What are those values gonna be? Yeah zero, right 32 32 bits all zeros But that's why it was in your computer. I thought it was your computer Oh, that's what yeah, something was beeping and I thought it came from this direction. I thought it was you This is bizarre Okay, just go on twitch and you can see my screen I'm slightly kidding, but we'll get it. We success like increase Okay, don't do that again bad system Okay, so we're talking about the IP address so This is actually a way of restricting Believe it's access to what IP addresses can access your socket. So what you're listening on So when you're listening on zero zero zero zero, it means that anybody can make a connection to you Whereas when you're listening from one two seven zero zero one This means that only requests that come from you from the local hosts will be Will be sent on this seems like a very trivial very generic Description, but it turns out this has a lot of implications. So if you are let's say Running a database on your local machine like because you're doing some web development stuff locally and you're running a mySQL server and You have a you can run the database and you can tell you this exact argument what IP address to use here If you use zero zero zero zero, it means that anyone on your local network could talk to your mySQL database If you don't have authentication set up because hey, it's your dev machine You don't want to specify all that stuff then anybody can connect to your database if you had customer data on there Anybody can access that similar things happen when you're developing a web server or sorry developing a web application locally you can specify this host like this the IP address and That will either allow other people to access it or restrict it. So this actually ends up being very important Okay, but we can do this bind. So do we pass the right number of bytes as the third argument here to bind? Yeah The 16 bytes that's what we said. We just counted the bytes right in each of those formats We can then call listen. I guess we can just pass zero here and it will set it to the max value. Okay Listen on the same file descriptor now. We've told the operating system. We are ready to listen We then call accept. Oh, we didn't talk about the other arguments to accept, but let's look at those real quick so this is a Struct socked adder pointer and a sock length pointer address length So sock length is just like an int. So why is it a pointer that we're passing it? But if we're just passing it in then why would we pass it a pointer to an int and I'll just pass the int in itself Say it again or so we can Correct. We want it to be modified, but we don't want to modify it. Who's gonna modify it? Yeah, the operating system the function whoever's calling this is one way when you're looking at like a Arguments to a function. This is how you reason about even without looking at the documentation. What is this for? Right, this is actually for the operating system to communicate back to us And to write out the values because if we just pass in an int there's no way for it to write out Remember, we said that the return value returns the file descriptor so What we'll see is what it's actually doing is it will give us a an address of the IP address of the other side of the connection This is how we can check what IP address that is where that's coming from But in order to do that we need to know how big that is we need to pass it a pointer here And this is just how to reason about this. I hope this will back me up Otherwise, this was like a hole the argument adder is a pointer to a sock adder structure The structure is filled in with the address of the peer socket as known to the communication layer So the peer being the other side of our communication The exact format of the dress returned is determined by these sockets address family Which we specified and the respective protocol man page when adder is null Nothing is filled in in this case adder length is not used and should also be null The adder length argument is a value result argument. The caller must initialize it to contain the size Interesting. So we actually are passing in stuff there The caller must initialize it to contain the size and bytes of the structure pointed to by address This way the operating system knows how many bytes to write in and that it's not going to write in more bites Don't we gave it because if we have an address structure of only eight bytes It is trying to write 16 bytes in they want me wants to know that On return it will contain the actual size of the peer address. This is interesting So we need if we were using this we need to know okay. I need to actually specify a value here I can't have it be a pointer to zero because that it won't rightly hang there. It means be a pointer to the actual size. Oh I guess well, let's see the blocking If no pending connections are present on the queue and the socket is not marked as non-blocking Except blocks the caller until a connection is present this means the operating system will literally like freeze our program and just Save it wait until there's a connection if the socket is marked non-blocking and no pending connections are present Then it fails with these specific error messages. So anyways by digging into these Documents we could learn exactly if we wanted to build an operating system that or a sorry A network server that doesn't lock on except it just checks every 10 seconds or something We could do that with this we have to figure out how to tell the OS that we want a non-blocking socket I'm sure by digging into those other man pages. We could find this valuable information Okay But for our purposes we can just call accept pass in three and Set the values as null and null meaning we don't care about the return values here. So when this returns it will return On the system call table file the script or for the operating system will set up in our process Okay for maps to this socket, which is now this peer connection. So this is the connection to the other side That we can then talk to Okay, so Let's do this. Okay before we dig in there. I want to talk about how we can actually pass in this sock adder Struct so we have this in C and I don't want to do something that's too close to the level. So let's say I'm gonna run this Okay, it wants me to call socket. I do really want to call socket though We'll use a different type of socket maybe Okay, because what I want to show is calling bind and I need a socket for that. So Somebody remind me what's the syntax here for comments? Is it semicolon or is it hash? Okay Okay, so that's the exit zero and now I want to call let's say bind AF Inet Six because this time I want an IPv6 because I'm not solving all these levels for you There's only 11. So if I just did one I did another one we're getting like up there and all the things that you'll be having fun with oh No, I need to call socket. Not mine. See we're already okay So I want to call socket on that. Let's see socket IPv6 Linux Cool. So I have another man page in here IPv6 There you go. I want AF inet six sock stream zero. So this is the system call that I'm going to try to make so I'll need to put something into our ex. How do I know the something? Look it up Look in the you said it's 41 Should we trust you? Oh? There you go. All right good good good Move 41 to our ex Move so now I need this AF inet six So I'm gonna open up a new terminal. What would we say that was? Can I do half a level? Yeah, I'm kind of doing it and here it just depends on how you look it's in the bits Include x86 64 then this actually makes sense if you think about it of like Why is there so there's a user include Linux and then a user include x86 64 Linux GNU and This AR 64 which have you looked at that that would actually be ARM based Linux Why those are different files because the exact layout of the bits and bytes varies between Architectures or or can so that's why we need to be there anyways. I net six is pf. I net six Which is 10 there we go All right, so what was it RDI? I think was 10 So that should be and the other thing I like to do as I'm doing this is document Like what these values are these random numbers that I'm just typing in here So that when I look at it later like you're doing a later challenge or you're modifying something You actually know what this value is and not just like you just did it for this one time Okay, and now I need the sock stream. Oh No sock stream. Okay, so I Cool, let's do another grip There we go and our bits socket type dot H. So socks sock stream equals one Let's go look at this file just to see what else is in there because we are Curious people here. We have different ones sock stream D gram raw RDM secret packet cool. So this was I actually can't remember what the another parameter is So this was the protocol So RDI RSI RDX Move into RDX to Is that correct or what did I just make that up? It is what so it's our ass I RDI RSI RDI RSI there you go. Thank you move into So it's RDX zero. Should I add a comment that this is zero? No, it's useless. Don't do that Okay, so this is correct. I Should run this with S trace and see this exact socket call socket with AFI net six sock stream zero I wanted to run it with S trace. I thought I did No, sis call. Thank you None of you saw that when I was typing this out, so it's great when you're in front of you because then you can just always say it was a Teaching moment, okay calling socket AFI net six sock stream. What's this IP proto IP? It probably is zero like we don't know we didn't look it up I'm sure we could look it up and that just happens to be zero. We pass zero in for there It said we can just do zero so It's asking us for zero. It's totally fine If we really wanted to double check we could double check that but I'm very confident How do we know that this socket call was correct? What's not zero the equals three this specifically means the return value of the system call was three If we looked in the man page, we would see that any negative value indicates a error. So Where in my assembly code, where can I get this return value X? Yeah, just like that's what we talked about with system calls Okay, cool So now I have to remember. What was the whole point of this diversion? It was to call bind right You don't know you're just here Okay, because I want to call bind Now I'm in a little bit of a weird place because I'm not doing the same thing. I'm using an IPv6 So my struct, I think it's be a struct sock adder in six. Let's see. Yeah, there we go sick. Okay So this is what we're gonna need to do. So this is a family and then a port number So the port number should be the same size. I think this still should be 16 bits. So two bytes and Then a flow info. I actually don't remember what this is and then a struct in adders in six adder which is 16 characters how many bytes is a character? one and So it's an IPv6 addresses. How many bytes? 16 which is what we talked about earlier, right? So it went from four bytes for IPv4 to 16 bytes for IPv6 Okay, so let's look here a little bit sin six family is set to AFI net six Port is the protocol port see sin port in IPv6. So it's the same. It's gonna be two bytes Sin six flow info is the IPv6 flow identifier. Let's just put that as zero I actually don't know what that is, but we'll just put it as zero for now Sin six adder is the IP is the 128 bit IPv6 address Sin six scope ID is an ID depending on the scope of the address. Let's also set that to be zero It is new in Linux 2.4. Thank you Supports it only for link local addresses in which case it contains an interface index. All right Cool, the port space is shared with IPv4 and v6. That's great. Okay, so we can work with this So that's what we're trying to do So we're gonna bind to let's go back to the slide We're gonna call bind Where's our good friend bind? There we go. We're gonna be a bind on Oops. Well, hello, I don't use them. So I have no idea what that was Okay, let's say something like this FD equal socket and so that I can reuse that in my comments bind FD I get a pointer to this adder. So Let's call it IPv6 the address of IPv6 sock adder and And let's go back and figure out the size So the family is two bytes the ports two bytes the flow info is 32 which is four. So two two four four eight another four was that 12 plus 16 is 28 So probably 28 bytes. We'll see if that's wrong or right, but I think that's correct Okay now so First things first Need to figure out the bind. What's the bind? syscall number 49. Thank you. I already messed up. Wow. How did I mess up? Yeah, because right here after this syscall the FD is in RAX But the very first thing I did here was overwrite RAX and now I will never get back that syscall number. So Yeah, let's do that here Let's use I don't know Into RBX let's say RAX so now we're storing that into RBX Now I can safely remove it and now if so, let's come back to this RDI and Just a big socket thing and now we want to move our SI the third argument we're gonna move 28 into there Wait, that's wrong. Okay. This should be our SI This should be our DX That's the address there and into RDI. Which what do I want to be in the first argument? RBX so FD this will be the address of IPv6 And what do I got to end with which I messed up last time, but I remembered syscall Okay, should this work? So something online is on twitch is asking if there's any other source we can find the value that goes in if net The answer is no you have to use the header files There may actually exist a something out there Well, let's go. Let's move zero into here because this is definitely this should not work Compile it it compiles. It's kind of shocking. Okay So What does this at least tell us? Yeah, we called bind correctly. We correctly passed in the file descriptor We did correctly pass in null. We said zero today there. We set in 28 But what is the return value did bind execute correctly? No, because a return to negative one and if we want to check that we can check that in our code in Eax But we wanted to point to that giant structure. So how do we get this giant structure into our program? What are some options? What was that? The stack we can use the stack and put the bytes on to the stack and lay them out correctly What else? Yeah, we can use our programs memory, right? We can actually have a data section that we can put Think of it as global memory right global memory to our program so the stack is also useful to our program, but We actually know exactly what values we want to put in there Although I guess we didn't talk about what kind of IPv6 sock adder we want to do. So let's do that right now Linger a linger structure. That's interesting. Why was that needed? Okay. Yeah, so we did it like this So let's look at I'm gonna use another So what I want is so to go with that last thing. So I'm looking for a sock adder in six that has the af inet six as the family so the first two bytes and Then the port let's do somebody give me another port number 81 Some 80 80. Okay. Perfect. I like no. I also don't like that because then oh, yeah, that'll work Okay, I was worried if the host network would be weird, but Okay, great. So flow info zero Then we need our unsigned So this is our IPv6 address. So we need 128 bits. Let's do something like this 0 0 1 1 2 2 3 3 5 6 6 7 7 8 Let's say 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Do I need another one? These addresses are way too large There we go, that makes more sense I actually think it's more like this, but whatever. All right, we'll do like that So we can easily translate this into bytes. These are hex values. So I'm just using this as shorthand to say I want the numbers to be from zero zero to ff zero zero one one as the Digits. All right, so that's the address and now I need a scope ID So my scope ID is gonna be zero And then finally that should be it. Okay, so that's our goal One side by side Okay So we can use that data So if we were to lay this out in memory We'd first want So the first was how many bytes? Two bytes. So our goal is what? Two bytes It should be what if I net six which we had here, which is 10. So it should be 10 two bytes 10 Okay, and if we I actually can't remember the assembler syntax six Directive so the same document. Yeah, there we go the same document that has all the directives The Rept n and stuff like that should have Yeah, so data grade There's a way to tell the assembler exactly which ones we want. Oh No, we're almost done a short is the same. Okay, great. Let's use dot short Cool. Okay. So we can use dot short Ten so this means I want as a short so a short is two bytes I have that and then I have the dot short the next short is going to be the bytes So this should be Let's see so 8080 in hex is 1f90 what do I have to be careful about here little Indians? I'm gonna flip them around The next one is 32 bits. So that's yeah D word. I think maybe L words How many words oh man this word thing is horrible, okay Yeah It's not in the docks. I'm not gonna show it Okay dot space. Oh, okay emits size bytes each of value fill Okay, that's not cool. Okay space. This probably is not the best way to do this, but Zero so four bytes of zero was this We'll do this one next, but let's just call it 12 ff and Dot space for zeros and I need to give this a label Call it big one and I think I should be able to just move Not like that. Oh my god that compiled. Oh, so close. Okay. I don't know. I guess I'll figure out Is it because I didn't do the address of it Oh There we go, oh just when you're leaving you thought it couldn't be done right at the buzzer SA family if I net-sick pork it hosts a never-short 8080 Whatever this thing is No, I did do all negative ones because I didn't want to do the actual stuff, but Sick all right we're done