+gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
+ const char *host,
+ unsigned short port,
+ /* OUT */ gras_socket_t *sock){
+
+ gras_error_t errcode;
+
+ m_host_t peer;
+ gras_hostdata_t *hd;
+ gras_trp_sg_sock_data_t *data;
+ gras_sg_portrec_t pr;
+
+ /* make sure this socket will reach someone */
+ if (!(peer=MSG_get_host_by_name(host))) {
+ fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
+ return mismatch_error;
+ }
+ if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
+ RAISE1(mismatch_error,
+ "can't connect to %s: no process on this host",
+ host);
+ }
+ errcode = find_port(hd,port,&pr);
+ if (errcode != no_error && errcode != mismatch_error)
+ return errcode;
+
+ if (errcode == mismatch_error) {
+ RAISE2(mismatch_error,
+ "can't connect to %s:%d, no process listen on this port",
+ host,port);
+ }
+
+ if (pr.raw && !sock->raw) {
+ RAISE2(mismatch_error,
+ "can't connect to %s:%d in regular mode, the process listen "
+ "in raw mode on this port",host,port);
+ }
+ if (!pr.raw && sock->raw) {
+ RAISE2(mismatch_error,
+ "can't connect to %s:%d in raw mode, the process listen "
+ "in regular mode on this port",host,port);
+ }
+
+ /* create the socket */
+ if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
+ RAISE_MALLOC;
+
+ data->from_PID = MSG_process_self_PID();
+ data->to_PID = hd->proc[ pr.tochan ];
+ data->to_host = peer;
+ data->to_chan = pr.tochan;
+
+ sock->data = data;
+ sock->incoming = 1;
+
+ DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
+ MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
+ sock->raw?"RAW":"regular",host,port,data->to_PID);
+
+ return no_error;