/* * Background thread for playing stream. */ THREAD(Scanner, arg) { TCPSOCKET *sock; RADIOSTATION *rsp; uint8_t rs; uint32_t rx_to = 10000UL; NutThreadSetPriority(128); NutSleep(10000); for (;;) { for (rs = 0; rs < MAXNUM_STATIONS; rs++) { NutSleep(2000); if (rs == radio.rc_rstation || rs == radio.rc_cstation) continue; rsp = &station[rs]; if (rsp->rs_ip == 0 || rsp->rs_port == 0 || radio.rc_off) { continue; } /* Delay if this isn't the first connection. */ if (rsp->rs_name) { printf("%lu bytes free\n", NutHeapAvailable()); NutSleep(30000); } /* Create a socket. */ if ((sock = NutTcpCreateSocket()) == 0) { break; } NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to)); /* Connect the stream server. */ printf("[Scan %s:%u]\n", inet_ntoa(rsp->rs_ip), rsp->rs_port); if (NutTcpConnect(sock, rsp->rs_ip, rsp->rs_port) == 0) { /* Process header from server. */ if (ScanStreamHeader(sock, rsp) == 0) { if (rsp->rs_scantitle) { free(rsp->rs_scantitle); rsp->rs_scantitle = 0; } if (rsp->rs_metaint) { if ((rsp->rs_scantitle = ReadMetaTitle(sock, rsp->rs_metaint)) != 0) { printf("%03u: %s\n", rs, rsp->rs_scantitle); rsp->rs_scandead = 0; } else rsp->rs_scandead = 1; } else rsp->rs_scandead = 0; } else rsp->rs_scandead = 1; } else { rsp->rs_scandead = 1; printf("[SERR=%d]\n", NutTcpError(sock)); } NutTcpCloseSocket(sock); } } NutSleep(30000); }
int TcpHostConnect(TCPSOCKET *sock, const char * host, uint16_t port) { uint32_t ip; /* Get remote host IP address. */ ip = inet_addr(host); if (ip == (uint32_t)-1 || ip == 0) { /* Doesn't look like an address, try host name. */ ip = NutDnsGetHostByName((uint8_t *)host); if (ip == 0) { /* Give up. */ return -1; } } /* Got a valid address, connect to it. */ return NutTcpConnect(sock, ip, port); }
static int io_connect (lua_State *L) { char addr[22]; char *port; const char *remote = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); FILE **pf = newfile(L); TCPSOCKET *sock; strncpy(addr, remote, sizeof(addr) - 1); if ((port = strchr(addr, ':')) != NULL) { *port++ = '\0'; } if (port && (sock = NutTcpCreateSocket()) != NULL) { if (NutTcpConnect(sock, inet_addr(addr), (uint16_t)atoi(port)) == 0) { *pf = _fdopen((int) sock, mode); } } return (*pf == NULL) ? pushresult(L, 0, remote) : 1; }
/*! * \brief Start an POP3 session. * * Applications may use the following basic sequence to retrieve an email: * * \code * #include <pro/pop3c.h> * * POP3CLIENTSESSION *pop3; * char *line; * * pop3 = NutPop3Connect(ip, 110); * if (pop3) { * NutPop3Login(pop3, "luser", "secret"); * if (NutPop3RetrieveMsg(pop3, 1) == 0) { * do { * line = NutPop3ReceiveResponse(pop3); * } while (line && strcmp(line, ".")); * } * NutPop3Disconnect(pop3); * } * \endcode * * \param ip IP address of the host to connect. * \param port Port number to connect. Typically port 110 is used by POP3. * * \return A pointer to a newly create \ref POP3CLIENTSESSION structure, * if the server is connected and ready to accept commands. * Otherwise a NULL pointer is returned. */ POP3CLIENTSESSION *NutPop3Connect(uint32_t ip, uint16_t port) { POP3CLIENTSESSION *si; si = calloc(1, sizeof(*si)); if (si) { si->pop3_sock = NutTcpCreateSocket(); if (si->pop3_sock && NutTcpConnect(si->pop3_sock, ip, port) == 0) { uint32_t tmo = POP3_TIMEOUT; NutTcpSetSockOpt(si->pop3_sock, SO_RCVTIMEO, &tmo, sizeof(tmo)); si->pop3_stream = _fdopen((int) ((intptr_t) si->pop3_sock), "r+b"); if (si->pop3_stream) { const char *rsp = NutPop3ReceiveResponse(si); if (rsp) { char *cp = strchr(rsp, '<'); if (cp) { si->pop3_stamp = strdup(cp); if (si->pop3_stamp) { cp = strchr(si->pop3_stamp, '>'); if (cp) { *++cp = '\0'; } else { free(si->pop3_stamp); si->pop3_stamp = NULL; } } } return si; } } } NutPop3Disconnect(si); free(si); } return NULL; }
int send_message(u_long ip, u_short port, u_long *metaint){ int rc; FILE *stream; u_char *line; u_char *cp; TCPSOCKET *sock; u_long baud = DBG_BAUDRATE; // u_long radio_ip = inet_addr(RADIO_IPADDR); u_short tcpbufsiz = TCPIP_BUFSIZ; u_long rx_to = TCPIP_READTIMEOUT; u_short mss = TCPIP_MSS; puts("create a TCP socket"); /* * Create a TCP socket. */ if ((sock = NutTcpCreateSocket()) == 0) { puts("Error: Can't create socket"); for (; ;); } puts("set socket options"); /* * Set socket options. Failures are ignored. */ if (NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss))) printf("Sockopt MSS failed\n"); if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))) printf("Sockopt TO failed\n"); if (NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz))) printf("Sockopt rxbuf failed\n"); /* * Connect the TCP server. */ printf("Connecting %s:%u...","83.128.250.123" , 8080); if ((rc = NutTcpConnect(sock, inet_addr("83.128.250.123"), 8080))) { printf("Error: Connect failed with %d\n", NutTcpError(sock)); return 0; } puts("OK"); if ((stream = _fdopen((int) sock, "r+b")) == 0) { puts("Error: Can't create stream"); return 0; } /* * Send the HTTP request. */ printf("GET %s HTTP/1.0\n\n", "/api/telegram"); fprintf(stream, "GET %s HTTP/1.0\r\n", "/api/telegram"); fprintf(stream, "Host: %s\r\n", "83.128.250.123"); fprintf(stream, "User-Agent: Ethernut\r\n"); fprintf(stream, "Accept: */*\r\n"); fprintf(stream, "Connection: close\r\n"); fputs("\r\n", stream); fflush(stream); /* * Receive the HTTP header. */ line = malloc(MAX_HEADERLINE); while (fgets(line, MAX_HEADERLINE, stream)) { /* * Chop off the carriage return at the end of the line. If none * was found, then this line was probably too large for our buffer. */ cp = strchr(line, '\r'); if (cp == 0) { puts("Warning: Input buffer overflow"); continue; } *cp = 0; /* * The header is terminated by an empty line. */ if (*line == 0) { break; } printf("%s\n", line); } putchar('\n'); free(line); return 0; }
/*! * \brief Connect to a radio station. * * \param sock TCP socket for this connection. * \param ip IP address of the server to connect. * \param port Port number of the server to connect. * * \return Stream pointer of the established connection on success. * Otherwise 0 is returned. */ FILE *ConnectStation(TCPSOCKET *sock, u_long ip, u_short port, u_long *metaint, RADIO_STREAM rStream) { int rc; FILE *stream; u_char *line; u_char *cp; /* * Connect the TCP server. */ // printf("radio ip: %d", rStream.ip) printf("Connecting %s:%d...", rStream.radio_ip, rStream.radio_port); if ((rc = NutTcpConnect(sock, inet_addr(rStream.radio_ip), rStream.radio_port))) { printf("Error: Connect failed with %d\n", NutTcpError(sock)); return 0; } puts("OK"); if ((stream = _fdopen((int) sock, "r+b")) == 0) { puts("Error: Can't create stream"); return 0; } /* * Send the HTTP request. */ printf("GET %s HTTP/1.0\n\n", rStream.radio_url); fprintf(stream, "GET %s HTTP/1.0\r\n", rStream.radio_url); fprintf(stream, "Host: %s\r\n", rStream.radio_ip); fprintf(stream, "User-Agent: Ethernut\r\n"); fprintf(stream, "Accept: */*\r\n"); fprintf(stream, "Icy-MetaData: 1\r\n"); fprintf(stream, "Connection: close\r\n"); fputs("\r\n", stream); fflush(stream); /* * Receive the HTTP header. */ line = malloc(MAX_HEADERLINE); while (fgets(line, MAX_HEADERLINE, stream)) { /* * Chop off the carriage return at the end of the line. If none * was found, then this line was probably too large for our buffer. */ cp = strchr(line, '\r'); if (cp == 0) { puts("Warning: Input buffer overflow"); continue; } *cp = 0; /* * The header is terminated by an empty line. */ if (*line == 0) { break; } if (strncmp(line, "icy-metaint:", 12) == 0) { *metaint = atol(line + 12); } printf("%s\n", line); } putchar('\n'); free(line); return stream; }
void clientConnect(TCPSOCKET *sock){ //sock = NutTcpCreateSocket(); if (NutTcpConnect(sock, inet_addr("173.194.67.94"), 80)) { LogMsg_P(LOG_INFO, PSTR("Fout bij connecten socket")); } }