static int pscom_psm_connect(pscom_con_t *con, int con_fd) { int arch = PSCOM_ARCH_PSM; pspsm_con_info_t *ci = pspsm_con_create(); pspsm_info_msg_t msg; pspsm_info_msg_t my_msg; if (pspsm_init() || !ci) goto dont_use; if (pspsm_con_init(ci)) goto dont_use; ci->con = con; /* We want talk psm */ pscom_writeall(con_fd, &arch, sizeof(arch)); /* step 1 */ if ((pscom_readall(con_fd, &arch, sizeof(arch)) != sizeof(arch)) || (arch != PSCOM_ARCH_PSM)) { goto err_remote; } /* step 2 : recv connection id's */ if ((pscom_readall(con_fd, &msg, sizeof(msg)) != sizeof(msg))) { goto err_remote; } /* step 3: send my connection id's */ pspsm_con_get_info_msg(ci, &my_msg); pscom_writeall(con_fd, &my_msg, sizeof(my_msg)); /* Connect */ if (pspsm_con_connect(ci, &msg)) { /* ToDo: bad! How to inform the peer about the error? */ DPRINT(0, "Psm pspsm_con_connect() failed!"); goto err_local; } pscom_psm_con_init(con, con_fd, ci); return 1; /* --- */ err_local: err_remote: dont_use: if (ci) { pspsm_con_cleanup(ci); pspsm_con_free(ci); } return 0; }
static int pscom_psm_accept(pscom_con_t *con, int con_fd) { int arch = PSCOM_ARCH_PSM; pspsm_con_info_t *ci = pspsm_con_create(); pspsm_info_msg_t msg; if (pspsm_init() || !ci) goto out_nopsm; if (pspsm_con_init(ci)) goto dont_use; /* step 1: Yes, we talk psm. */ pscom_writeall(con_fd, &arch, sizeof(arch)); /* step 2: Send Connection id's */ pspsm_con_get_info_msg(ci, &msg); pscom_writeall(con_fd, &msg, sizeof(msg)); /* step 3 : recv connection id's */ if ((pscom_readall(con_fd, &msg, sizeof(msg)) != sizeof(msg))) { goto err_remote; } /* Connect */ if (pspsm_con_connect(ci, &msg)) { /* ToDo: bad! How to inform the peer about the error? */ DPRINT(0, "Psm pspsm_con_connect() failed!"); goto err_local; } pscom_psm_con_init(con, con_fd, ci); return 1; /* --- */ err_local: err_remote: if (ci) pspsm_con_cleanup(ci); dont_use: if (ci) pspsm_con_free(ci); return 0; /* --- */ out_nopsm: arch = PSCOM_ARCH_ERROR; pscom_writeall(con_fd, &arch, sizeof(arch)); return 0; /* --- */ }
static void pscom_psm_handshake(pscom_con_t *con, int type, void *data, unsigned size) { switch (type) { case PSCOM_INFO_ARCH_REQ: { pspsm_info_msg_t msg; pspsm_con_info_t *ci = pspsm_con_create(); con->arch.psm.ci = ci; con->arch.psm.reading = 0; if (pspsm_con_init(ci, con)) goto error_con_init; /* send my connection id's */ pspsm_con_get_info_msg(ci, &msg); pscom_precon_send(con->precon, PSCOM_INFO_PSM_ID, &msg, sizeof(msg)); break; /* Next is PSCOM_INFO_PSM_ID or PSCOM_INFO_ARCH_NEXT */ } case PSCOM_INFO_PSM_ID: { pspsm_info_msg_t *msg = data; assert(sizeof(*msg) == size); if (pspsm_con_connect(con->arch.psm.ci, msg)) goto error_con_connect; pscom_precon_send(con->precon, PSCOM_INFO_ARCH_OK, NULL, 0); break; /* Next is EOF or ARCH_NEXT */ } case PSCOM_INFO_ARCH_NEXT: /* Something failed. Cleanup. */ pscom_psm_con_cleanup(con); break; /* Done. Psm failed */ case PSCOM_INFO_EOF: pscom_psm_init_con(con); break; /* Done. Use Psm */ } return; /* --- */ error_con_connect: error_con_init: pscom_psm_con_cleanup(con); pscom_precon_send_PSCOM_INFO_ARCH_NEXT(con->precon); }