Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/osx/x86/src/test/server.c
Views: 11784
#include <stdio.h>1#include <stdlib.h>2#include <strings.h>3#include <unistd.h>4#include <pthread.h>56#include <sys/mman.h>7#include <sys/types.h>8#include <sys/socket.h>9#include <netinet/ip.h>1011void usage(char* argv0)12{13fprintf(stderr, "usage: %s [ -t ] [ -p <port> ]\n", argv0);14}1516int read_and_exec(int s)17{18int n, length;19int (*payload)(void);2021fprintf(stderr, "Reading length... ");22if ((n = recv(s, &length, sizeof(length), 0)) != sizeof(length)) {23if (n < 0)24perror("recv");25else26fprintf(stderr, "recv: short read\n");27return -1;28}29fprintf(stderr, "%d\n", length);3031fprintf(stderr, "Allocating buffer... ");32if ((payload = mmap(NULL, length, PROT_READ | PROT_WRITE | PROT_EXEC,33MAP_ANON | MAP_PRIVATE, -1, 0)) == (void*)-1) {34perror("mmap");35return -1;36}37fprintf(stderr, "0x%x\n", payload);3839fprintf(stderr, "Reading payload... ");40if ((n = recv(s, payload, length, 0)) != length) {41if (n < 0)42perror("recv");43else44fprintf(stderr, "recv: short read\n");45return -1;46}47fprintf(stderr, "read %d bytes\n", n);4849fprintf(stderr, "Executing payload...\n");5051(void*)(*payload)();5253return 0;54}5556void* read_and_exec_thread(void* arg)57{58return (void*)read_and_exec((int)arg);59}6061int create_read_and_exec_thread(int c)62{63int err;64pthread_t pthread;65void* return_value;6667if ((err = pthread_create(&pthread, NULL,68read_and_exec_thread, (void*)c)) != 0) {69fprintf(stderr, "pthread_create: %s\n", strerror(err));70return -1;71}7273if ((err = pthread_join(pthread, &return_value)) != 0) {74fprintf(stderr, "pthread_join: %s\n", strerror(err));75return -1;76}77}7879int main(int argc, char* argv[])80{81int c, s, val, threaded = 0;82socklen_t salen;83struct sockaddr_in saddr, client_saddr;84short port = 1234;8586while ((c = getopt(argc, argv, "tp:")) != EOF) {87switch (c) {88case 'p':89port = atoi(optarg);90break;91case 't':92threaded = 1;93break;94default:95usage(argv[0]);96exit(EXIT_FAILURE);97}98}99100if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0) {101perror("socket");102exit(EXIT_FAILURE);103}104105val = 1;106if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {107perror("setsockopt");108exit(EXIT_FAILURE);109}110111bzero(&saddr, sizeof(saddr));112saddr.sin_family = AF_INET;113saddr.sin_port = htons(port);114saddr.sin_addr.s_addr = INADDR_ANY;115116if (bind(s, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) {117perror("bind");118exit(EXIT_FAILURE);119}120121if (listen(s, 5) < 0) {122perror("listen");123exit(EXIT_FAILURE);124}125126if ((c = accept(s, (struct sockaddr*)&client_saddr, &salen)) < 0) {127perror("accept");128exit(EXIT_FAILURE);129}130131if (threaded)132exit(create_read_and_exec_thread(c));133else134exit(read_and_exec(c));135}136137138