Last active
June 25, 2020 23:54
-
-
Save ysingh/d3bab59bd225141bb25f871281a80400 to your computer and use it in GitHub Desktop.
This file contains 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/types.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#define MAX_ENAME 133 | |
#define BUF_SIZE 254 | |
#define PORT_NUM 5055 | |
static char *ename[] = { | |
/* 0 */ "", | |
/* 1 */ "EPERM", "ENOENT", "ESRCH", "EINTR", "EIO", "ENXIO", | |
/* 7 */ "E2BIG", "ENOEXEC", "EBADF", "ECHILD", | |
/* 11 */ "EAGAIN/EWOULDBLOCK", "ENOMEM", "EACCES", "EFAULT", | |
/* 15 */ "ENOTBLK", "EBUSY", "EEXIST", "EXDEV", "ENODEV", | |
/* 20 */ "ENOTDIR", "EISDIR", "EINVAL", "ENFILE", "EMFILE", | |
/* 25 */ "ENOTTY", "ETXTBSY", "EFBIG", "ENOSPC", "ESPIPE", | |
/* 30 */ "EROFS", "EMLINK", "EPIPE", "EDOM", "ERANGE", | |
/* 35 */ "EDEADLK/EDEADLOCK", "ENAMETOOLONG", "ENOLCK", "ENOSYS", | |
/* 39 */ "ENOTEMPTY", "ELOOP", "", "ENOMSG", "EIDRM", "ECHRNG", | |
/* 45 */ "EL2NSYNC", "EL3HLT", "EL3RST", "ELNRNG", "EUNATCH", | |
/* 50 */ "ENOCSI", "EL2HLT", "EBADE", "EBADR", "EXFULL", "ENOANO", | |
/* 56 */ "EBADRQC", "EBADSLT", "", "EBFONT", "ENOSTR", "ENODATA", | |
/* 62 */ "ETIME", "ENOSR", "ENONET", "ENOPKG", "EREMOTE", | |
/* 67 */ "ENOLINK", "EADV", "ESRMNT", "ECOMM", "EPROTO", | |
/* 72 */ "EMULTIHOP", "EDOTDOT", "EBADMSG", "EOVERFLOW", | |
/* 76 */ "ENOTUNIQ", "EBADFD", "EREMCHG", "ELIBACC", "ELIBBAD", | |
/* 81 */ "ELIBSCN", "ELIBMAX", "ELIBEXEC", "EILSEQ", "ERESTART", | |
/* 86 */ "ESTRPIPE", "EUSERS", "ENOTSOCK", "EDESTADDRREQ", | |
/* 90 */ "EMSGSIZE", "EPROTOTYPE", "ENOPROTOOPT", | |
/* 93 */ "EPROTONOSUPPORT", "ESOCKTNOSUPPORT", | |
/* 95 */ "EOPNOTSUPP/ENOTSUP", "EPFNOSUPPORT", "EAFNOSUPPORT", | |
/* 98 */ "EADDRINUSE", "EADDRNOTAVAIL", "ENETDOWN", "ENETUNREACH", | |
/* 102 */ "ENETRESET", "ECONNABORTED", "ECONNRESET", "ENOBUFS", | |
/* 106 */ "EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS", | |
/* 110 */ "ETIMEDOUT", "ECONNREFUSED", "EHOSTDOWN", "EHOSTUNREACH", | |
/* 114 */ "EALREADY", "EINPROGRESS", "ESTALE", "EUCLEAN", | |
/* 118 */ "ENOTNAM", "ENAVAIL", "EISNAM", "EREMOTEIO", "EDQUOT", | |
/* 123 */ "ENOMEDIUM", "EMEDIUMTYPE", "ECANCELED", "ENOKEY", | |
/* 127 */ "EKEYEXPIRED", "EKEYREVOKED", "EKEYREJECTED", | |
/* 130 */ "EOWNERDEAD", "ENOTRECOVERABLE", "ERFKILL", "EHWPOISON" | |
}; | |
void errExit(char message[]) { | |
// errExit | |
// prints error text corresponding to errno | |
// plus error description as returned by strerror() | |
// also terminate program by calling exit() | |
// TODO(yudhi): Add EF_DUMPCORE and learn about dumpfiles | |
int savederrno = errno; | |
char errText[BUF_SIZE]; | |
snprintf(errText, BUF_SIZE, "[%s %s]", | |
(savederrno > 0 && savederrno < MAX_ENAME) ? | |
ename[savederrno]: "?UNKNOWN?", strerror(savederrno)); | |
puts(message); | |
puts(errText); | |
exit(EXIT_FAILURE); | |
} | |
int main(void) { | |
struct sockaddr_in6 serverAddr, clientAddr; | |
int socketDescriptor; | |
ssize_t numBytes; | |
socklen_t len; | |
char buf[BUF_SIZE]; | |
char clientAddrStr[INET6_ADDRSTRLEN]; | |
socketDescriptor = socket(AF_INET6, SOCK_DGRAM, 0); | |
if (socketDescriptor == -1) { | |
errExit("Socket"); | |
} | |
memset(&serverAddr, 0, sizeof(serverAddr)); | |
serverAddr.sin6_family = AF_INET6; | |
// Wildcard Address | |
serverAddr.sin6_addr = in6addr_any; | |
serverAddr.sin6_port = htons(PORT_NUM); | |
if (bind(socketDescriptor, (struct sockaddr *) &serverAddr, sizeof(struct sockaddr_in6)) == -1) { | |
errExit("Bind"); | |
} | |
for(;;) { | |
len = sizeof(struct sockaddr_in6); | |
numBytes = recvfrom(socketDescriptor, buf, BUF_SIZE, 0, (struct sockaddr *) &clientAddr, &len); | |
if (numBytes == -1) { | |
errExit("recvFrom"); | |
} | |
if (inet_ntop(AF_INET6, &clientAddr.sin6_addr, clientAddrStr, INET6_ADDRSTRLEN) == NULL) { | |
printf("Couldn't convert client address to string\n"); | |
} else { | |
printf("Server received %ld bytes from (%s %u)\n", (long) numBytes, clientAddrStr, ntohs(clientAddr.sin6_port)); | |
} | |
if (sendto(socketDescriptor, buf, numBytes, 0, (struct sockaddr *) &clientAddr, len) != numBytes) { | |
puts("sendTo"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment