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/unixasm/fndsockclient.c
Views: 11766
/*1* fndsockclient.c2* Copyright 2006 Ramon de Carvalho Valle <[email protected]>3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA17*18*/1920#include <stdio.h>21#include <stdlib.h>22#include <string.h>23#include <sys/types.h>24#include <sys/socket.h>25#include <netinet/in.h>26#include <arpa/inet.h>27#include <netdb.h>28#include <sys/select.h>29#include <unistd.h>30#include <errno.h>3132#if defined(_AIX)33#include "aix-power-fndsockcode.c"34#elif defined(__bsd__) && defined(__i386__)35#include "bsd-x86-fndsockcode.c"36#elif defined(__linux__) && defined(__powerpc64__)37#include "lin-power-fndsockcode64.c"38#elif defined(__linux__) && defined(__powerpc__)39#include "lin-power-fndsockcode.c"40#elif defined(__linux__) && defined(__i386__)41#include "lin-x86-fndsockcode.c"42#elif defined(__osx__) && defined(__i386__)43#include "osx-x86-fndsockcode.c"44#elif defined(__solaris__) && defined(__i386__)45#include "sol-x86-fndsockcode.c"46#else47#error "Unsupported operating system and/or architecture."48#endif4950int51hexdump(char *buf, int len)52{53int i, j;5455for (i=0; i<len; i++) {56for (j=0; j<16; j++) {57if (i+j >= len)58printf("%3s","");59else60printf("%02x ", (unsigned char)buf[i+j]);61}6263printf("%3s","");6465for (j=0; j<16; j++) {66if (i+j >= len)67printf("%1s","");68else69if (buf[i+j]>'\x1f' && buf[i+j]<'\x7f')70printf("%c", buf[i+j]);71else72printf(".");73}7475i += 15;7677printf("\n");78}7980return 0;81}8283int84main(int argc, char **argv)85{86char *addr = "0.0.0.0";87int port = 1234;88int c, s;89int debug = 0, verbose = 0;90struct sockaddr_in sin;91struct hostent *he;92socklen_t sin_len = sizeof(sin);93int count;9495while ((c = getopt(argc, argv, "a:dp:v")) != -1) {96switch (c) {97case 'a':98addr = optarg;99break;100case 'd':101debug = 1;102break;103case 'p':104port = atoi(optarg);105break;106case 'v':107verbose = 1;108}109}110111if (debug || verbose)112printf("using %s:%d\n", addr, port);113114if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {115perror("socket");116exit(EXIT_FAILURE);117}118119memset(&sin, 0, sizeof(sin));120sin.sin_family = AF_INET;121sin.sin_port = htons(port);122if ((sin.sin_addr.s_addr = inet_addr(addr)) == -1) {123if ((he = gethostbyname(addr)) == NULL) {124errno = EADDRNOTAVAIL;125perror("gethostbyname");126exit(EXIT_FAILURE);127}128memcpy(&sin.sin_addr.s_addr, he->h_addr, 4);129}130131if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {132perror("connect");133exit(EXIT_FAILURE);134}135136if (debug || verbose)137printf("connected to %s:%d\n", addr, port);138139if (getsockname(s, (struct sockaddr *)&sin, &sin_len)) {140perror("getsockname");141exit(EXIT_FAILURE);142}143144#if defined(__LP64__)145fndsockcode64[FNDSOCKPORT] = (unsigned char)((ntohs(sin.sin_port)>>8)&0xff);146fndsockcode64[FNDSOCKPORT+1] = (unsigned char)(ntohs(sin.sin_port)&0xff);147148if ((count = send(s, fndsockcode64, sizeof(fndsockcode64)-1, 0)) == -1) {149perror("send");150exit(EXIT_FAILURE);151}152153if (debug)154hexdump(fndsockcode64, sizeof(fndsockcode64)-1);155156#else157fndsockcode[FNDSOCKPORT] = (unsigned char)((ntohs(sin.sin_port)>>8)&0xff);158fndsockcode[FNDSOCKPORT+1] = (unsigned char)(ntohs(sin.sin_port)&0xff);159160if ((count = send(s, fndsockcode, sizeof(fndsockcode)-1, 0)) == -1) {161perror("send");162exit(EXIT_FAILURE);163}164165if (debug)166hexdump(fndsockcode, sizeof(fndsockcode)-1);167168#endif169170if (debug || verbose)171printf("%d bytes sent\n", count);172173sleep(4);174175write(s, "uname -a\n", 9);176while (1) {177fd_set fds;178int count;179char buf[1024];180181FD_ZERO(&fds);182FD_SET(0, &fds);183FD_SET(s, &fds);184if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) == -1) {185if (errno == EINTR)186continue;187perror("select");188exit(EXIT_FAILURE);189}190if (FD_ISSET(0, &fds)) {191if ((count = read(0, buf, sizeof(buf))) < 1) {192if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)193continue;194else195break;196}197write(s, buf, count);198}199if (FD_ISSET(s, &fds)) {200if ((count = read(s, buf, sizeof(buf))) < 1) {201if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)202continue;203else204break;205}206write(1, buf, count);207}208}209210exit(EXIT_SUCCESS);211}212213214215