static int send_and_recv_tcp(krb5_socket_t fd, time_t tmout, const krb5_data *req, krb5_data *rep) { unsigned char len[4]; unsigned long rep_len; krb5_data len_data; _krb5_put_int(len, req->length, 4); if(net_write (fd, len, sizeof(len)) < 0) return -1; if(net_write (fd, req->data, req->length) < 0) return -1; if (recv_loop (fd, tmout, 0, 4, &len_data) < 0) return -1; if (len_data.length != 4) { krb5_data_free (&len_data); return -1; } _krb5_get_int(len_data.data, &rep_len, 4); krb5_data_free (&len_data); if (recv_loop (fd, tmout, 0, rep_len, rep) < 0) return -1; if(rep->length != rep_len) { krb5_data_free (rep); return -1; } return 0; }
void accept_loop(int soc){ char hbuf[NI_MAXHOST], sbuf[NI_NUMERICSERV]; struct sockaddr_storage from; int acc; socklen_t len; for(;;){ len = (socklen_t) sizeof(from); if((acc =accept(soc, (struct sockaddr *) &from, &len)) == -1){ if(errno != EINTR){ perror("accept"); } } else { getnameinfo((struct sockaddr *) &from, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); fprintf(stderr, "accept:%s%s\n", hbuf, sbuf); recv_loop(acc); close(acc); acc = 0; } } }
static int send_and_recv_http(krb5_socket_t fd, time_t tmout, const char *prefix, const krb5_data *req, krb5_data *rep) { char *request = NULL; char *str; int ret; int len = base64_encode(req->data, req->length, &str); if(len < 0) return -1; ret = asprintf(&request, "GET %s%s HTTP/1.0\r\n\r\n", prefix, str); free(str); if (ret < 0 || request == NULL) return -1; ret = net_write (fd, request, strlen(request)); free (request); if (ret < 0) return ret; ret = recv_loop(fd, tmout, 0, 0, rep); if(ret) return ret; { unsigned long rep_len; char *s, *p; s = realloc(rep->data, rep->length + 1); if (s == NULL) { krb5_data_free (rep); return -1; } s[rep->length] = 0; p = strstr(s, "\r\n\r\n"); if(p == NULL) { krb5_data_zero(rep); free(s); return -1; } p += 4; rep->data = s; rep->length -= p - s; if(rep->length < 4) { /* remove length */ krb5_data_zero(rep); free(s); return -1; } rep->length -= 4; _krb5_get_int(p, &rep_len, 4); if (rep_len != rep->length) { krb5_data_zero(rep); free(s); return -1; } memmove(rep->data, p + 4, rep->length); } return 0; }
int main(int argc, char *argv[]) { int host_sock; // enable logging openlog("carapi", LOG_PERROR | LOG_PID, LOG_USER); // enable signal handling if (setup_signals() < 0) { exit(EXIT_FAILURE); } // set up canstore canstore = canstore_init("can0"); canstore_start(canstore); // create and bind the host socket host_sock = get_host_socket("127.0.0.1", 1234); if (host_sock < 0) { exit(EXIT_FAILURE); } // start the receive loop if (recv_loop(host_sock) < 0) { exit(EXIT_FAILURE); } // close the host socket and exit close(host_sock); exit(EXIT_SUCCESS); return 0; }
static int iguana_init() { int recv_pipe[2], retval = 0; init_rec_buffer(); if (pipe(recv_pipe) != 0) { logprintf(LOG_ERR, "couldn't open pipe: %s", strerror(errno)); } else { int notify[2]; if (pipe(notify) != 0) { logprintf(LOG_ERR, "couldn't open pipe: %s", strerror(errno)); close(recv_pipe[0]); close(recv_pipe[1]); } else { hw.fd = recv_pipe[0]; child = fork(); if (child == -1) { logprintf(LOG_ERR, "couldn't fork child process: %s", strerror(errno)); } else if (child == 0) { close(recv_pipe[0]); close(notify[0]); recv_loop(recv_pipe[1], notify[1]); _exit(0); } else { int dummy; close(recv_pipe[1]); close(notify[1]); /* make sure child has set its signal handler to avoid race with iguana_deinit() */ read(notify[0], &dummy, 1); close(notify[0]); sendConn = iguanaConnect(hw.device); if (sendConn == -1) logprintf(LOG_ERR, "couldn't open connection to iguanaIR daemon: %s", strerror(errno)); else retval = 1; } } } return retval; }
static int send_and_recv_udp(krb5_socket_t fd, time_t tmout, const krb5_data *req, krb5_data *rep) { if (send (fd, req->data, req->length, 0) < 0) return -1; return recv_loop(fd, tmout, 1, 0, rep); }
int iguana_init() { int recv_pipe[2], retval = 0; init_rec_buffer(); if (pipe(recv_pipe) != 0) { logprintf(LOG_ERR, "couldn't open pipe: %s", strerror(errno)); } else { hw.fd = recv_pipe[0]; child = fork(); if (child == -1) { logprintf(LOG_ERR, "couldn't fork child process: %s", strerror(errno)); } else if (child == 0) { recv_loop(recv_pipe[1]); exit(0); } else { sendConn = iguanaConnect(hw.device); if (sendConn == -1) logprintf(LOG_ERR, "couldn't open connection to iguanaIR daemon: %s", strerror(errno)); else retval = 1; } } return retval; }