Created
June 11, 2020 20:19
-
-
Save Voltra/bf510bfa361f597a9f953722ac92f8d0 to your computer and use it in GitHub Desktop.
C code sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/socket.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include "./sender.h" | |
| #include "../macros.h" | |
| #include "../utils.h" | |
| #include "../tools/addr.h" | |
| #include "../access.h" | |
| int sendVia(args_t* args, service_t* service, payload_ptr payload){ | |
| // memcpy(service->lastSent, msg(pdu(payload)), MSG_SIZE+1); | |
| IF_DEBUG({ | |
| printf("[sending] %s to %s:%d\n", msg(pdu(payload)), addrFromNumber(service->other.sin_addr.s_addr), service->other.sin_port); | |
| puts("[SENDING]"); | |
| printf("\tsrc: %s\n", addrFromNumber(src(payload))); | |
| printf("\tdest: %s\n", addrFromNumber(dest(payload))); | |
| puts("[/SENDING]"); | |
| }); | |
| IF_DEBUG({ | |
| puts("[SENDING]"); | |
| printf("\tparity: %s\n", isEmpty(payload) ? "Nothing" : "Something"); | |
| printf("\tack: %s\n", hasAck(payload) ? "Something" : "Nothing"); | |
| printf("\tsrc: %s\n", addrFromNumber(src(payload))); | |
| printf("\tis src: %s\n", isSource(payload, htonl(args->selfAddr)) ? "Yup" : "Nope"); | |
| printf("\tdest: %s\n", addrFromNumber(dest(payload))); | |
| printf("\tis dest: %s\n", isDestination(payload, htonl(args->selfAddr)) ? "Yup" : "Nope"); | |
| printf("\tmsg: %s\n", msg(pdu(payload))); | |
| puts("[/SENDING]\n"); | |
| }); | |
| return sendto( | |
| service->socket, //socket | |
| payload, //buffer | |
| PAYLOAD_SIZE, //buffer size | |
| 0, //flags | |
| getServiceAddr(service, ADDR_OTHER), //addr (dest) | |
| service->len //length of addr | |
| ); | |
| } | |
| payload_ptr requestInput(args_t* args, service_t* service, payload_ptr payload){ | |
| if(!isEmpty(payload)) | |
| return payload; | |
| puts("[sending] Would you like to send a message? (y/n)"); | |
| char c[2] = {0}; | |
| scanf("%1s", c); | |
| flushStdin(); | |
| if (c[0] == 'y') { | |
| addr_t sendToAddr = 0; | |
| puts("[sending] Do you want to send it to the next hop? (y/n)"); | |
| char nh[2] = {0}; | |
| scanf("%1s", nh); | |
| flushStdin(); | |
| if(nh[0] == 'y') | |
| sendToAddr = args->clientAddr; | |
| else{ | |
| puts("[sending] Where do you want to send your message? (IPv4 address)"); | |
| char* addr = calloc(16, sizeof(char)); | |
| scanf("%15s", addr); | |
| flushStdin(); | |
| sendToAddr = addrFromString(addr); | |
| free(addr); | |
| } | |
| printf("\n[sending] Enter your message: "); | |
| msg_ptr msg = calloc(MSG_SIZE+1, sizeof(byte)); | |
| // scanf(MSG_FMT, msg); | |
| fgets(msg, MSG_SIZE+1, stdin); | |
| // flushStdin(); | |
| reserve(payload); | |
| setSource(payload, htonl(args->selfAddr)); | |
| setDest(payload, htonl(sendToAddr)); | |
| setMsg(payload, msg); | |
| memcpy(service->lastSent, msg, MSG_SIZE+1); | |
| } | |
| return payload; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment